404 flask when using SERVER_NAME

In my Flask configuration, I set SERVER_NAME to a domain, such as "app.example.com". I do this because I need to use url_for with _external URL. If SERVER_NAME is not set, Flask considers the server to be 127.0.0.1βˆ—000 (it actually works behind a reverse proxy) and returns an external URL, for example http: http://127.0.0.1:5000/location .

So far so good. But here is my problem: when setting SERVER_NAME, each URL returns 404. I see that each request goes to the server, to the correct URL, but Flask responds to 404. If you disable SERVER_NAME, the correct page will return.

I use Apache with ProxyPass and ProxyPassReverse (I know I would prefer to use Nginx, but this is a requirement). Here are the headers I add:

 Header add Host "app.example.com" RequestHeader set Host "app.example.com" Header add X-Forwarded-Host "app.example.com" RequestHeader set X-Forwarded-Host "app.example.com" 

Any ideas?

+8
python flask apache
source share
1 answer

Just found the answer. Apache has the ProxyPreserveHost option. Once it is set to β€œOn,” everything works as expected.

Further information here: http://flask.pocoo.org/mailinglist/archive/2011/3/14/problem-with-apache-proxy-and-canonical-urls/

+11
source share

All Articles