1

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

5
  • This spans 2 servers at port 80 so I expect you have some error message about it Commented Feb 15, 2022 at 14:59
  • @user39950 There will be no errors, but since the server_name directive isn't specified for both, the first one will act as the default server processing any request and the second one will never be used at all. Commented Feb 15, 2022 at 15:05
  • thanks both - @IvanShatsky I'm a bit of a novice with nginx so i appreciate the guidance - whats the best way to approach this then? as I expect numerous config files to be used... If I add a server name it works - but whats the best practice for this? as everything will be served off a single domain and proxied based on path Commented Feb 15, 2022 at 15:25
  • @JoshW Usual approach is to use a single file for each individual site (in apache terms it can be called vhost). You can use some common code that can be shared across different sites as a separate file and then include it wherever needed using the include directive, but don't place those files inside the sites-enabled directory - everything being placed in that directory is included under the http context, not the server one. Commented Feb 15, 2022 at 15:33
  • @IvanShatsky - makes sense - where should they reside then? /etc/nginx/conf.d/ ? What about the clashing of ports comment and the server_name - I think I've tried serving them from this directory and had the same issue - but perhaps that was the server_name issue again... Commented Feb 15, 2022 at 15:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.