17

All of the examples I have seen, used "include something.conf" in the http block.

Is it possible to use "include something.conf" in the server block? If yes, where is the best practice to save the conf?

For example:

server {
    listen 80;
    server_name example.com;

    include (where)/somethingcool.conf

}
1

3 Answers 3

26

You can put the file wherever you want, it's up to you.

If you use a relative path, however, then it will be relative to the nginx configuration directory (e.g. /etc/nginx on Linux, /usr/local/etc/nginx on BSD, etc.).

I personally create a directory /etc/nginx/includes and place these configuration bits in there. For example:

server {
    server_name deploy.example.com;

    include includes/listen-80;
    include includes/letsencrypt;

    return 301 https://$host$request_uri;
}

Where /etc/nginx/includes/listen-80 contains:

listen 80;
listen [::]:80;

And /etc/nginx/includes/letsencrypt contains:

location /.well-known/acme-challenge/ {
    root /var/www;
    try_files $uri =404;
}

Of course I have many other such configuration bits.

1

@michaelhampton answered the question that, at first read, I thought you had asked, too. As his example shows, you can use the include directive in a server block. You can actually use it in any block, so using it in the server block is fine, too.

-1

You can also conditionally enable the include only if it exists by using a nice little syntax trick that I saw somewhere on SE once upon a time:

include /var/www/sites/includes/adminer[.]conf;

The trick is to name your submodules with the .conf filename suffix, and then, use [.] in your include line as the example above... this tells Nginx to load the submodule only if the file exists.

We do this in SlickStack for certain optional submodules.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .