flask + apache + mod_wsgi – not working [solved]

Gosh! it took me a whole day to identify what the issue was….

Basically what is happening:

  • when using WSGIScriptAlias directive on non-root url (i..e you’re not matching “/”), mod_wsgi strip the url of the matched part

Here is an example:

in htttpd.conf:

WSGIDaemonProcess credmanager user=myuser group=mygroup threads=5 python-home=/path/to/virtualenv
WSGIScriptAlias /api /var/www/credmanager/api/credmanager.wsgi
WSGIProcessGroup credmanager
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On

<Directory /var/www/credmanager>
Options FollowSymLinks
Require all granted
</Directory>

In above example, we are only handling via python uris starting with “/api”.
Let’s say requested url: http://www.mydomain.com/api/toto
=> url is starting with “/api” so it is matched and handled by mod_wsgi
=> python application called by mod_wsgi (flask in my case) is receiving a stripped url, i was expecting it to receive “/api/toto” but instead it is receiving “/toto”
which makes all routing rules not working…

Luckily there is an easy workaround for this! Simply append matched pattern (“/api” in this case) to .wsgi script location:

WSGIScriptAlias /api /var/www/credmanager/api/credmanager.wsgi

becomes:

WSGIScriptAlias /api /var/www/credmanager/api/credmanager.wsgi/api

Restart apache, and now it should work!

important point – when providing url pattern to match (“/api” in that case), make sure there is no trailing slash (i.e. match “/api” and not “/api/”) since trailing slash will not make it work

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...