I am currently running two webservers on the same machine, one using Django through Gunicorn, which is my original site, the other which acts as the online shop using the same domain, this one using Nestjs (Nodejs).
Both servers have an /admin path with the original being at example.com/admin and the other I am wanting to be at example.com/store/admin. However whenever I enter the second URL into my browser (i.e example.com/store/admin) it returns the other admin page, example.com/admin (without the /store prefix).
Here is the config snippet I believe needs reworking:
server {
server_name example.com www.example.com;
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /store {
proxy_pass http://127.0.0.1:3000/;
}
location = /store/admin {
proxy_pass http://127.0.0.1:3000/admin/;
}
...
}
I have tried a fair few combinations of the /store and /store/admin location blocks but just can't seem to get it to direct me to the store's server admin site. It works on my local development machine when testing using the nodejs server. Going to http://example.com/store returns what I expect to see from the Nestjs server.
The docs states that:
... To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. ...
So you need only two location blocks:
The first :
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
And the other with the rewrite:
location /store {
rewrite ^/store(.*) $1 break;
proxy_pass http://127.0.0.1:3000;
}
This means it will rewrite every URL starts with /store and remove it from the URL before passing to the upstream. and this also includes the /admin, since it is the same.
Also note there is no suffix / at the end of the proxy_pass - which instruct NGINX to take the user supplied URI.