-1

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?

OPTION 1

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
}

OPTION 2

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?

Patrick Mevzek
  • 9,273
  • 7
  • 29
  • 42
Jumpa
  • 111
  • 1
  • 5
  • 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 https://stackoverflow.com/questions/389169/best-practices-for-api-versioning for the question of putting the API version field in the URL. – Patrick Mevzek Aug 14 '19 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 '19 at 16:31

1 Answers1

1

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

Jenny D
  • 27,358
  • 21
  • 74
  • 110
Danila Vershinin
  • 4,738
  • 3
  • 16
  • 21
  • Could you please elaborate the answer? What does that mean? Thanks. – Jumpa Aug 18 '19 at 06:56
  • This config allows you to simply create the new subdirectories, following your existing format of `/project/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 '19 at 10:05