3

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

https://stackoverflow.com/questions/21662940/proxy-pass-cannot-have-uri-part-in-location/21714937#21714937

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.

  • Do you need to proxy or could you forward? – Tim May 10 '16 at 20:43
  • @tim I need the proxy. – Stefano Borini May 10 '16 at 20:44
  • In your simplified case, since the request, as per the log, is /user/sborini/vnc_auto.html, can you please change location = /user/sborini/ { to location /user/sborini/ { ? – Pothi Kalimuthu May 11 '16 at 8:05
2

Add a trailing slash to your proxy_pass directive.

proxy_pass http://127.0.0.1:6079/;

Source

Documentation

Fingers crossed this works... took me 30 seconds on Google to work it out.

| improve this answer | |
  • i tried that as well, did not work. – Stefano Borini May 10 '16 at 21:05
  • Please show the matching access logs for one request to Nginx and then proxied server. I'd like to see what request actually hits port 6079. – Tim May 10 '16 at 21:14
  • ok, let me check.. – Stefano Borini May 10 '16 at 21:41
  • Ok, it's quite complicated. I revise my question to add more. – Stefano Borini May 10 '16 at 21:47
  • 1
    ok. flask logs I need to find them. However, I managed to obtain something, so I am on the right track. Thank you for the rubber ducking. I'll write some more as soon as I find something definitive with the actual answer. Now I have to go to sleep because it's quite late here. – Stefano Borini May 10 '16 at 23:28

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.