Gunicorn: flask, gunicorn, nginx: app in subpath

Created on 17 May 2017  路  6Comments  路  Source: benoitc/gunicorn

I 'm trying to setup a flask webapp running with gunicorn reverse proxied on a nginx subpath.

relevant nginx:
location /gunicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS
# proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://localhost:8000/;
}

from here http://docs.gunicorn.org/en/stable/deploy.html

I can reach my app as intended, but links generated with url_for() in the app don't include the "gunicorn" part of the location.

I also found http://flask.pocoo.org/snippets/35/, but this is already a few years old. Is this the way to go? it seems a little bit odd for me, to add "code" for something, that looks more like "configuration". And with uwsgi, it is working as it is, no wrapper for wsgi_app needed. In uwsgi I could specify this with the "mount" option.

Can anyone enlighten me? I'm a little bit lost...

PS: actually, this should go to forum, but I couldn't find how to add label. Cann anyone tell me where this is?

Feedback Requested

Most helpful comment

@benotic sorry! and thank you!! now I got why tilgovi linked at the FAQ on SCRIPT_NAME...
I resolved my issue starting gunicorn with:

$ gunicorn -w 3 --env SCRIPT_NAME=/Example -b 127.0.0.1:8000 run:app

or via supervisor with the config file:

[program:example]
directory=/var/www/Example
command=/var/www/Example/Example/venv3/bin/gunicorn -w 3 -b 127.0.0.1:8000 run:app
user=www-data
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
environment=SCRIPT_NAME="/Example"

All 6 comments

There is an FAQ for this. See if it can help. http://docs.gunicorn.org/en/latest/faq.html#how-do-i-set-script-name

closing issue as answered

I have the same issue and I could not find an answer neither here nor in the links or FAQs. Still grappling.

@belzebuu the issue is closed, it's better to open a new issue in such case. Anyway setting the SCRIPT_NAME to the subpath should be enough :
https://www.python.org/dev/peps/pep-3333/#environ-variables

@benotic sorry! and thank you!! now I got why tilgovi linked at the FAQ on SCRIPT_NAME...
I resolved my issue starting gunicorn with:

$ gunicorn -w 3 --env SCRIPT_NAME=/Example -b 127.0.0.1:8000 run:app

or via supervisor with the config file:

[program:example]
directory=/var/www/Example
command=/var/www/Example/Example/venv3/bin/gunicorn -w 3 -b 127.0.0.1:8000 run:app
user=www-data
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
environment=SCRIPT_NAME="/Example"

For anyone who wants more detail about SCRIPT_NAME than what is found on the FAQ, I found an excellent resource that provides more information about what's going on: https://dlukes.github.io/flask-wsgi-url-prefix.html

If you're tearing your hair out searching google for permutations of {'flask', 'proxy', 'nginx', 'subdirectory', 'url_for', 'subpath', 'prefix'}, then this is for you. This stackexchange thread is also good.

Was this page helpful?
0 / 5 - 0 ratings