librelist archives

« back to archive

url_for() problem in apache + fastcgi

url_for() problem in apache + fastcgi

From:
Erle Carrara
Date:
2011-07-22 @ 22:30
Hi,

I have a problem, url_for() return "dispatch.cgi" prefix when app running in
apache + fastcgi enviroment.

My .htaccess file:

<IfModule mod_fcgid.c>
    AddHandler fcgid-script .fcgi
    <Files ~ (\.fcgi)>
        SetHandler fcgid-script
        Options +FollowSymLinks +ExecCGI
    </Files>
</IfModule>

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
</IfModule>

And index.fcgi file:

#!/usr/bin/python2.7
import site, sys

sys.path.insert(0, '/home/erle/workspaces/
pessoal/app/')
site.addsitedir('/home/erle/.virtualenvs/app/lib/python2.7/site-packages')

from flup.server.fcgi import WSGIServer
from app import create_app
from app.config import ProductionConfig

class MyDebugMiddleware(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        print environ
        return self.app(environ, start_response)


if __name__ == '__main__':
    app = create_app(ProductionConfig)
    app.wsgi_app = MyDebugMiddleware(app.wsgi_app)
    WSGIServer(app).run()


Someone has same problem?


*- - -
Erle Carrara
<carrara.erle@gmail.com>
*

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