mod_wsgi and mod_userdir conflict

200 views
Skip to first unread message

olet...@gmail.com

unread,
Jan 23, 2011, 8:38:45 PM1/23/11
to mod...@googlegroups.com
Hi,

(this is a follow up on a conversion I had with Graham Dumpleton on irc)

I have set up mod_wsgi in a vhost to deploy an application at the root level on the vhost but I can't access the userdirs anymore on this vhost (still works fine in the others).

The line I used to enable my application are:
        WSGIDaemonProcess snowy user=www-data home=/home/_WWW/www.host.com
        WSGIProcessGroup snowy
        WSGIScriptAlias  / /home/_WWW/www.host.com/ci.wsgi

The userdir and wsgi module are loaded from a mods-enabled/{userdir,wsgi}.{conf,load} files (standard debian packages), so userdir is loaded outside the vhost (but even changing it to load inside doesn't change fix the problem)

This is using apache-mpm-itk 2.2.6  and mod_wsgi 2.5-1~lenny1

Graham Dumpleton

unread,
Jan 23, 2011, 9:01:46 PM1/23/11
to mod...@googlegroups.com

After some digging, seems that UserDir directive from mod_userdir not
overriding WSGIScriptAlias where WSGI application is mounted at root
of web site, ie., '/', is deliberate.

The reason it was done that way was to mirror what happens with the
Alias and ScriptAlias directives in Apache and for them, if they are
used for '/', they too override the UserDir directive.

Note that this isn't a problem when mounting something at a sub URL,
only at '/'.

The solution is to use the style of configuration described in:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive

So, instead of having:

UserDir public_html

WSGIScriptAlias / /usr/local/wsgi/scripts/site.wsgi

<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all
</Directory>

You would need to use:

DocumentRoot /usr/local/wsgi/scripts

UserDir public_html

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /site.wsgi/$1 [QSA,PT,L

Options ExecCGI Indexes

<Directory /usr/local/wsgi/scripts>
Options ExecCGI Indexes

AddHandler wsgi-script .wsgi

Order allow,deny
Allow from all
</Directory>

The WSGI script itself would need to include the fixup:

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

What will happen with this is that UserDir would be checked first and
then the contents of DocumentRoot. If either of those do not match a
resource corresponding to a static file, be that an actual static
file, or a mapped resource such as .cgi or .wsgi script, then the
rewrite rule is applied and the request internally redirect to
site.wsgi in the DocumentRoot directory.

In effect this allows the WSGI application to overlay the file system
based resources accessible by UserDIr and DocumentRoot with the WSGI
application only handling a request where resource couldn't otherwise
be found.

With this approach, instead of needing to do something like:

Alias /robots.txt /usr/local/wsgi/static/robots.txt
Alias /favicon.ico /usr/local/wsgi/static/favicon.ico

AliasMatch /([^/]*\.css) /usr/local/wsgi/static/styles/$1

Alias /media/ /usr/local/wsgi/static/media/

you would instead stick all those static media files in your new
DocumentRoot directory and they will be found automatically without
needing to add explicitly Alias directives.

Because though anything in your new DocumentRoot can be served up as a
static file, that DocumentRoot directory SHOULD be a subdirectory
which ONLY contains the WSGI script file and the static media files.

In other words, if using Django and you stuck the WSGI script file in
your site directory along with your settings file, you SHOULD NOT,
then make the site directory your DocumentRoot as it would make all
your site source code, including settings file with passwords,
downloadable as static files.

Graham

Olivier Le Thanh Duong

unread,
Jan 25, 2011, 5:50:39 PM1/25/11
to mod...@googlegroups.com
It works, thanks a lot.
For people trying to replicate this solution, you need to move the Rewrite stuff inside <Directory> or they won't match correctly.

Regards,
  Olivier

Graham Dumpleton

unread,
Jan 25, 2011, 6:00:20 PM1/25/11
to mod...@googlegroups.com
On 26 January 2011 09:50, Olivier Le Thanh Duong <olet...@gmail.com> wrote:
> It works, thanks a lot.
> For people trying to replicate this solution, you need to move the Rewrite
> stuff inside <Directory> or they won't match correctly.

Plus that once you do that you need FollowSymlinks with Options
directive else RewriteRule isn't allowed for some silly reason.

Graham

Reply all
Reply to author
Forward
0 new messages