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'
Nginx and Gunicorn work togetherGunicorn translates requests which it gets from Nginx into a format which your web application can handle, and makes sure that your code is executed when needed. They make a great team! Each can do something, which the other can't.
gunicorn-proxy is a Docker turnkey reverse proxy for gunicorn. To use it you only need to set one piece of configuration—The hostname and port of your gunicorn server.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With