[Flask] urls when running under gunicorn

S McGraw smcg4191 at mtneva.com
Tue Oct 13 19:26:25 CEST 2015

I new to flask and have a small first app working under flask's 
development server.  In the templates I used url_for(), under the 
impression that would adjust the app's urls when I later run it 
behind a web server.

I have now gotten it to run under gunicorn and set up a reverse
proxy to it in my Apache (2.4) web server.  It worked ok when 
configured as a virtual host.  However, I want to run my app under 
a url in my regular url namespace, eg: 
The trouble is that the urls in the rendered templates are still
"/login", "/main", etc rather than /myapp/login, /myapp/main, etc 
which of course result in a 404 when clicked on.  So I'm sure there 
is some step or piece of code I'm missing.

FWIW, I tried using the werkzeug ProxyFix middleware suggested in
the "Proxy Setups" section of the Flask docs, no effect.  I saw 
recommendations to use Flask Snippets #35, along with configuring 
the web server to send certain headers, but they all seemed to be 
for Nginx, and seemed an awfully heavy-weight answer.  Surely there 
is a simpler flask-only way to fix this?  Also I read about Apache's 
mod_proxy_html but that too seems a little overkill to me.)

I did get things to work by losing the url_for()'s in my code and
templates, replaced with eg, g.prefix+'/main', where g.prefix is set
to "/myapp" in a before_request() function. But I don't think that's
the recommended practice. ;-)

What am I missing?

More information about the Flask mailing list