Home > Mobile >  Using multiple nginx config files in sites-available with proxy pass
Using multiple nginx config files in sites-available with proxy pass

Time:02-16

I am wanting to use multiple files declared at

/etc/nging/sites-available/

symlinked to:

/etc/nginx/sites-enabled/

Now the files look similar to below - call this team1.conf:

server {
  listen 80;
  location /team1/app3/location/region {
  rewrite ^/team1/app3/location/region(.*) /path3/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team1/app4/location/region {
  rewrite ^/team1/app4/location/region(.*) /path4/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team1/app5/location/region {
  rewrite ^/team1/app5/location/region(.*) /path5/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
}

Call this team2.conf:

server {
  listen 80;
  location /team2/app3/location/region {
  rewrite ^/team2/app3/location/region(.*) /path3/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team2/app4/location/region {
  rewrite ^/team2/app4/location/region(.*) /path4/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team2/app5/location/region {
  rewrite ^/team2/app5/location/region(.*) /path5/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
}

I wanted to keep them separate - hence the separate files - however, with two files, nginx just seems to read the first one and 404 anything in the second one - so I suspect there is a conflict somewhere....

The files are pretty arbitrary - I just wanted to demonstrate - the paths etc, will vary between files..

Any help would be great - apologies if I've missed something obvious..

Cheers

CodePudding user response:

Too long for a comment, so I'm writing this as an answer.

where should they reside then?

Check your main /etc/nginx/nginx.conf configuration. I think something like

include /etc/nginx/conf.d/*.conf
include /etc/nginx/sites-enabled/*

is present there. Notice that those files are included under the http context. So you can use whatever you want, but not the /etc/nginx/conf.d/*.conf or any file inside the sites-enabled directory. Something like /etc/nginx/shared/... will be ok, I think. I don't use those sites-available/sites-enabled directories at all in favor of conf.d directory - read the Difference in sites-available vs sites-enabled vs conf.d directories (Nginx)? discussion on ServerFault.

What about the clashing of ports

You can have any number of server blocks listening the same port. Nginx will choose the right block according to the HTTP Host header value. If it isn't match any of the defined server names in any server block (or the Host header is missing at all from the request), the default server will be used to serve it. See the How nginx processes a request official documentation page or this answer for even more details.

CodePudding user response:

Thats great - this is all teaching me a lot! so thanks for that!

I have gone with the following structure:

within /etc/nginx/conf.d/main.conf:

server {
 listen 80;
 server_name  service.com;
 access_log           /var/log/nginx/test-service-access.log;
 error_log            /var/log/nginx/test-service-error.log;
 include              /etc/nginx/shared/*.conf;
}

Then within /etc/nginx/shared/:

I have both files:

team1.conf:

  location /team1/app3/location/region {
  rewrite ^/team1/app3/location/region(.*) /path3/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team1/app4/location/region {
  rewrite ^/team1/app4/location/region(.*) /path4/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team1/app5/location/region {
  rewrite ^/team1/app5/location/region(.*) /path5/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }

team2.conf:

  location /team2/app3/location/region {
  rewrite ^/team2/app3/location/region(.*) /path3/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team2/app4/location/region {
  rewrite ^/team2/app4/location/region(.*) /path4/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }
  location /team2/app5/location/region {
  rewrite ^/team2/app5/location/region(.*) /path5/healthcheck$1 break;
  proxy_pass https://this.is.the.backend.app.example;
  }

This seems to work ok - hopefully this is an accepted way of structuring it. @IvanShatsky

  • Related