I've one domain: example.com and multiple projects: proj1, proj2, proj3. Every project has its own API and INTERNAL panel. Could you please suggest me the right DNS/Nginx configuration for this situation?


server_name api.example.com
location /proj1/v1.0 {
  root /proj1/api/v1.0
location /proj2/v1.0 {
  root /proj2/api/v1.0
location /proj3/v1.0 {
  root /proj3/api/v1.0


server_name internal.example.com
location /proj1 {
  root /proj1/internal
location /proj2 {
  root /proj2/internal
location /proj3 {
  root /proj3/internal


server_name proj1.example.com
root /proj1

location /api/v1.0 {
location /internal {


[...] the same for the proj2 and proj3

Should I go for the multiple locations or multiple subdomains strategy? Any other solution?

  • Both options are fine and right on the paper. Now which one is fine for your specific case, it is basically impossible to reply, and kind of offtopic here. At least, in the first case it would be easier to move only a single application to a new box when you need it. Aside, look at stackoverflow.com/questions/389169/… for the question of putting the API version field in the URL. – Patrick Mevzek Aug 14 at 16:04
  • Both options might be right on paper, but I strongly recommend subdomains for proxying to unrelated (i.e. not part of the same code base or otherwise related to each other) applications. It makes everything much easier. – Michael Hampton Aug 14 at 16:31

You can use regular expressions to do this. Here's an example:

server_name api.example.com
location ~ ^/proj(\d+)/v1\.0 {
  root /proj$1/api/v1.0

The regexp in the location line means:

  • ^/proj a request starting with "/proj"
  • (\d+) immediately followed by one or more digits
  • /v1\.0 immediately followed by /v1.0

then the digits will be stored in the variable $1. That variable will then be used to determine the directory where the files are sored, as you see in the line starting with root

So if you access /proj4711/v1.0, the digits 4711 will be used to determine that the root directory should be /proj4711/api/v1.0

  • Could you please elaborate the answer? What does that mean? Thanks. – Jumpa Aug 18 at 6:56
  • This config allows you to simply create the new subdirectories, following your existing format of /project<id>/v1.0 to have it working in NGINX. That is, without having to manually add NGINX configuration for every new project. – Danila Vershinin Aug 18 at 10:05

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.