Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using several django projects running on the same apache instance through mod_wsgi, configured with virtualhost for each site, see the httpd.conf here. For one of the sites I want to use static-cache (staticgenerator), so I set up a directory with .htaccess file which contains:

RequestHeader unset X-Forwarded-Host
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*) http://127.0.0.1:3456/$1 [P]

where 3456 is the django port on the server. Using this rewrite rule, the request is always forwarded to the mod_wsgi handler, even if the file or directory exists, and if the file index.html exists the request shows as request-path/index.html. I tried another setup:

RequestHeader unset X-Forwarded-Host
RewriteEngine on
RewriteBase /
RewriteCond $1 !-d
RewriteCond $1index.html !-f
RewriteRule ^(.*) http://127.0.0.1:3456/$1 [P]

but got almost the same results. All requests are transferred to the mod_wsgi handler, but the request path is now the original one. To sum it up:

  1. What is the correct RewriteCond to use here?
  2. How do you transfer a request to the mod_wsgi handler? Is it the right way?
  3. If that's not the way to do it, then how do you serve static files from a directory when they exist, and when they don't you serve from apache/mode_wsgi?

Thanks for your help.

share|improve this question
    
You haven't shown how you configured mod_wsgi (if that is what you are actually using) and where you configured it. If configured in main Apache configuration file with WSGIScriptAlias, then likely .htaccess file would never be consulted as mounting of WSGIScriptAlias would take precedence. Have a read of 'code.google.com/p/modwsgi/wiki/…;. Also note that WSGI is a specification. If you mean mod_wsgi for Apache, you should really refer to it as Apache/mod_wsgi and not WSGI else people may not know what you are talking about. –  Graham Dumpleton Sep 4 '09 at 0:37
    
Edited to reflect Graham's notes. Will read the suggested site. –  user19328 Sep 4 '09 at 11:13
add comment

2 Answers

Without actually testing, use something like:

<Virtualhost *:28512>

ServerName site1.com
ServerAlias www.site1.com

DocumentRoot /home/mehome/webapps/djangoprojects/site1

<Directory /home/mehome/webapps/djangoprojects/site1>
Order allow,deny
Allow from all
AddHandler wsgi-script .wsgi
Options ExecCGI

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

</VirtualHost>

Put your Django WSGI script file as:

/home/mehome/webapps/djangoprojects/site1/site.wsgi

It should contain fixup as documented in mod_wsgi wiki, so it says something like:

import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()

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)

Then have static file generator dump files in appropriate location based on URL it must match under:

/home/mehome/webapps/djangoprojects/site1

What should happen is that the rewrite rule will check if Apache found a static file for URL and if not redirect it into the Django application.

This sort of complex setup is much better being asked about on the official mod_wsgi mailing list. This example has been presented a number of times in the past and is in the mailing list archives as well as the basis for it being explained in the documentation.

share|improve this answer
    
Is there a possibility, with the suggested setup, to somehow use WSGIProcessGroup and WSGIDaemonProcess in order to control the apache process? –  user19328 Sep 5 '09 at 22:58
    
Yes, you can still use WSGIDaemonProcess and WSGIProcessGroup. I left them out because the mechanism works with or without them, so not really relevant to example. –  Graham Dumpleton Sep 6 '09 at 5:57
add comment

I wanted to avoid the Directory directive, also, I prefer WSGIScriptAlias instead of AddHandler wsgi-script .wsgi:

<VirtualHost *:80>
    ...
    DocumentRoot /home/username/www/domain.tld/htdocs
    ...

    # %{DOCUMENT_ROOT} is needed because outside of directories,
    # REQUEST_FILENAME is the same as REQUEST_URI,
    # i.e '/foo', not '/physical/path/to/foo'
    #
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /site.wsgi/$1 [QSA,L,PT]

    WSGIScriptAlias /site.wsgi /home/username/www/domain.tld/deploy/site.wsgi
</VirtualHost>

This will pass through requests to static files and let WSGI handle the rest.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.