Nginx location match working only when matching root
I'm trying to configure nginx so I can have two locations. One for my node API and the other for my Jenkins CI.
http://my_ip/api
should point to my node server and http://my_ip/jenkins
should point to my jenkins CI
I have this server block.
server {
listen 80;
server_name my_ip_address;
location /api {
proxy_pass http://127.0.0.1:1234;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /jenkins {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8080;
proxy_read_timeout 90;
}
}
That config is not working. But if I use any of those locations matching to root it works.
This works with no problem. And the same if I use the location block of my node api with /
only.
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8080;
proxy_read_timeout 90;
}
But if I change /
to /jenkins
it doesn't work anymore.
I know I'm missing some basic pice of knowledge here, but I can't figure this out since everywhere I look seems to suggests that should be working ok.
Well, after some more research on the subject I've noticed that the problem with that setup was that the location /api was not connecting to http://127.0.0.1:1234/
it was trying to connect to http://127.0.0.1:1234/api
and the same with jenkins.
So the solution is adding trailing slashes to both the location and proxy_pass.
location /api/ {
proxy_pass http://127.0.0.1:1234/;
....
location /jenkins/ {
proxy_pass http://localhost:8080/;
proxy_read_timeout 90;
....
So that did the trick.