12

I have a Flask app running with gunicorn on http://127.0.0.1:4000:

gunicorn -b 127.0.0.1:4000 webapp:app

Now I would like to use nginx as a reverse proxy and forward http://myserver.com/webapp to http://127.0.0.1:4000 in a way that every http://myserver.com/webapp/subpath goes to http://127.0.0.1:4000/subpath.

The proxy/redirect works nicely when not using a subpath:

upstream app {
    server 127.0.0.1:4000 fail_timeout=0;
}

server {
    listen 80 default;
    client_max_body_size 4G;
    server_name _;

    location / {
       proxy_pass http://app;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $http_host;       
    }
}

How can I set

location /webapp {
    #go to my gunicorn app, translate URLs nicely
}

This tip from the Flask developers didn't work: http://flask.pocoo.org/snippets/35/

SOLVED: The snippet http://flask.pocoo.org/snippets/35/ works! I had a few absolute URLs in my templates (e.g. /task/delete) and had to change everything to url_for().

Stupid ... but now it works like expected, I have my app on 'http://myserver.com/subpath'

Martin Preusse
  • 9,151
  • 12
  • 48
  • 80

1 Answers1

13

I solved my problem: The snippet http://flask.pocoo.org/snippets/35/ does work, I was so stupid to have absolute URLs in my templates. I changed that to url_for() and now it works like charm.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
Martin Preusse
  • 9,151
  • 12
  • 48
  • 80
  • 5
    The URL doesn't seem to work anymore. Could you please provide the solution? I am facing the same issue. – Hrishikesh Sep 09 '19 at 20:24
  • 1
    FWIW - the content from the original link can be found here (as of today at least): https://web.archive.org/web/20131129080707/http://flask.pocoo.org/snippets/35/ – Mike N. Aug 22 '20 at 21:02
  • May I ask you how does handle the `next` args in path? – FavorMylikes Feb 07 '21 at 09:00