I currently have this configuration for a reverse proxy in nginx
server {
listen 8888 default_server;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri @proxy;
}
location = / {
try_files $uri @proxy2;
}
location = /redirect.html {
try_files $uri @proxy2;
}
location ~ /user/(.*)$ {
try_files index.html @proxy2;
}
location @proxy2 {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:6079;
max_ranges 0;
}
location @proxy {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:6081;
max_ranges 0;
}
location = /websockify {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:6081;
}
}
My problem is that I have a url of the type http://example.com:8888/user/username
and I need to redirect it to localhost:6079
. Unfortunately, what is listening on port 6079 wants the URL without the /user/username
part. It is my understanding that when I try to use this configuration, I get a flask generated 404 (the application at port 6079 is a flask app). I tried pretty much everything for the whole afternoon, including rewrite of the url, hardcoding the actual url and other tricks from the documentation of nginx, but I could not find a working solution.
Observe that if I connect directly to http://localhost:6079/ the application works fine, so it really seems to be this nginx setup.
Edit:
I tried to modify as suggested and add the / at the proxy2 proxy_pass line, but it complains as explained here
As for the logs, I don't see anything unexpected, except that if I connect to localhost:8888/user/username it shows a 404, and if I connect to localhost:8888 I see the flask app.
Keep into account that the nginx is on a docker image, but I browse from the host machine, so 127.0.0.1 in the nginx is the docker instance, and when I browse 127.0.0.1 it's the host machine and the port goes through the bridge that I setup when I started the container.
Edit 2
Logs don't tell much.
172.17.42.1 - - [10/May/2016:22:14:05 +0000] "GET /user/sborini/redirect.html?width=1375&height=695 HTTP/1.1" 200 212 "http://localhost:8888/user/sborini/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"
172.17.42.1 - - [10/May/2016:22:14:05 +0000] "GET /user/sborini/vnc_auto.html HTTP/1.1" 404 166 "http://localhost:8888/user/sborini/redirect.html?width=1375&height=695" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"
172.17.42.1 - - [10/May/2016:22:14:57 +0000] "GET /user/sborini/vnc_auto.html HTTP/1.1" 404 209 "http://localhost:8888/user/sborini/redirect.html?width=1375&height=695" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"
172.17.42.1 - - [10/May/2016:22:14:58 +0000] "GET /user/sborini/vnc_auto.html HTTP/1.1" 404 209 "http://localhost:8888/user/sborini/redirect.html?width=1375&height=695" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"
172.17.42.1 - - [10/May/2016:22:18:59 +0000] "GET / HTTP/1.1" 200 454 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"
See how I get something if I connect to the plain /
entry
In any case, I tried a simplified case:
server {
listen 8888 default_server;
root /usr/share/nginx/html;
index index.html index.htm;
location = /user/sborini/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:5555;
max_ranges 0;
}
}
and a netcat listening on 5555. I get a bad gateway. Investigating.
/user/sborini/vnc_auto.html
, can you please changelocation = /user/sborini/ {
tolocation /user/sborini/ {
?