Can you define a server's locations in multiple nginx config files?

Solution 1:

I believe, you could use this configuration:

server {
    server_name example.com;
    listen      [::]:80;
    listen      80;

    include /path/to/applications/*/nginx.conf;
}

and then in each application's directory configure the redirection like this:

location    /app1 {
    proxy_pass  http://app1;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Request-Start $msec;
}

Solution 2:

You can include external configs via include:

include /path/to/config1.conf;
include /path/to/config2.conf;
include /path/to/confdir/*.conf;

server {
    server_name example.com;
    listen      [::]:80;
    listen      80;
}

And inside separate config you can use any valid code blocks:

upstream app1 {
    server 127.0.0.1:8080;
}

location /app1 {
    proxy_pass http://app1;
}

Tags:

Nginx