On 10 February 2011 12:07, bc <[email protected]> wrote: > Can anyone explain how SCRIPT_NAME is calculated? I'm using > mod_rewrite to do per-developer cgi directories, with sometimes > amusing results in SCRIPT_NAME. I've seen in the forums that > SCRIPT_NAME is the wsgi 'mount point', but this is clearly a > simplification, because there's not always a straight-forward mapping > to the mount point. > > E.g., with rewrite rules something like ~foo/cgi-bin/bar -> /mount/ > cgi-bin-foo/bar, with wsgi mounted at /mount, how would SCRIPT_NAME be > determined? If I dump it from python, I get inconsistent results. > Sometimes it's ~fo, sometimes it's ~foo/, sometimes part of the path > is repeated. It's very weird. Looks like some kind of pattern match > that's going wrong. > > > Is SCRIPT_NAME being set by apache, or mod_wsgi? In the mod_wsgi code > it looks like it's only doing some minor cleanup on the value > calculated by apache.
Are you using Django by chance? It is broken when it comes to situation where rewrite rules are applied. See: http://code.djangoproject.com/ticket/12464 The raw unmodified SCRIPT_NAME should always be correct, however, in the face of rewrite rules, it is the notional mount point of the target of the redirect within Apache and potentially no relationship to the original URL that was requested before the rewrite rules were applied. Overall, rewrite rules are a PITA and there is no automatic way for mod_wsgi to calculate an adjusted SCRIPT_NAME which has some relationship to an original URL. Ultimately that sort of fixup can only be done in WSGI middleware that has embedded knowledge of how SCRIPT_NAME should be modified based on what rewrite was being done. There is an example in the mod_wsgi wiki documentation but Google code sites seems to be dead right now. The example is: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /site.wsgi/$1 [QSA,PT,L] In this case SCRIPT_NAME would end up as '/site.wsgi' instead of ''. Thus for that known case where know script always being executed via the rewrite, the WSGI script does: def _application(environ, start_response): # The original application. ... import posixpath def application(environ, start_response): # Wrapper to set SCRIPT_NAME to actual mount point. environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME']) if environ['SCRIPT_NAME'] == '/': environ['SCRIPT_NAME'] = '' return _application(environ, start_response) This is in the ConfigurationGuidelines.wiki document on the mod_wsgi wiki. Anyway, to comment further, would need to know whether you are using a framework like Django which is known to stuff up SCRIPT_NAME when rewrites occurring, or otherwise whether you in your own code are trying to fiddle it. Graham -- You received this message because you are subscribed to the Google Groups "modwsgi" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.
