Re: [flask] url_for() problem in apache + fastcgi
- From:
- Armin Ronacher
- Date:
- 2011-07-23 @ 10:43
Hi,
On 7/23/11 12:30 AM, Erle Carrara wrote:
> Hi,
>
> I have a problem, url_for() return "dispatch.cgi" prefix when app
> running in apache + fastcgi enviroment.
There are many fixes to that problem.
> My .htaccess file:
>
> <IfModule mod_fcgid.c>
> AddHandler fcgid-script .fcgi
> <Files ~ (\.fcgi)>
> SetHandler fcgid-script
> Options +FollowSymLinks +ExecCGI
> </Files>
> </IfModule>
If you are setting up fastcgi from .htaccess you have to use the
mod_rerwite hack which will always produce a wrong setting for Flask.
However thankfully this is easy to fix with a custom WSGI middleware:
class ScriptNameStripper(object):
to_strip = '/index.fcgi'
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
path_info = environ.get('PATH_INFO', '')
assert path_info.startswith(self.to_strip)
environ['PATH_INFO'] = path_info[len(self.to_strip):]
return self.app(environ, start_response)
# and on the production environment activate this middleawre, make sure
# to not do that during development with the integrated server.
app.wsgi_app = ScriptNameStripper(app.wsgi_app)
The proper solution is to use ScriptAlias which this middleware is not
necessary, this however is not possible from within a .htaccess file as
far as I know:
<VirtualHost *>
ServerName example.com
ScriptAlias / /path/to/dispatch.fcgi/
</VirtualHost>
Regards,
Armin