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


You can put the file wherever you want.

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/incldues/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.

| improve this answer | |

@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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.