20

I have multiple ruby apps running on the on the same host:

~/app1
~/app2
~/app3

And I want to have nginx proxy these apps using sub-directories like:

   http://example.com/app1
   http://example.com/app2
   http://example.com/app3

I'm curious if nginx supports me being able define these locations in multiple files, so that I could keep each configuration with the app, instead of having one monolithic config file for all of the apps:

~/app1/nginx.conf
~/app2/nginx.conf
~/app3/nginx.conf

My naive attempt of defining the server with a single location directive in each of the 3 config files led to conflicting server name "example.com" on [::]:80, ignored with a configs that look like this:

upstream app1 { server 127.0.0.1:4567; }
server {
  listen      [::]:80;
  listen      80;
  servername  example.com
  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;
  }
}

Is there a way to organize the configs this way?

2 Answers 2

8

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;
}
4
  • 5
    Does this actually work? Doesn't the upstream module need to be outside the server module block?
    – Curley
    Oct 23, 2014 at 19:06
  • 7
    Looks like location directive is not allowed outside the server block. At least for me, nginx reports "location" directive is not allowed here. Oct 25, 2016 at 15:08
  • 1
    Yeah...I don't see a simple way of doing that properly without multiple files per application: one that would work inside a server block, one that would work outside. I guess for flexibility and to make it clean a wildcard could be used: include /etc/nginx/above_server.d/* and include /etc/nginx/in_server.d/*
    – jeteon
    Mar 9, 2017 at 21:04
  • 2
    This answer is wrong Jul 22, 2017 at 8:28
17

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;
}
1
  • 2
    The downside would be that you can't define multiple upstreams within the server block but I think the OP's use case is well served by this answer.
    – jeteon
    Mar 9, 2017 at 21:06

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.