Since some time now, I’m looking into the python based django framework. This is working out really good. There is a little thing, that it does not provide, the storage of the static media files, you still need a webserver for this.

But ususally you only have on IP address and run already an apache2 on the port 80 with lot of named based virtual hosts on it. Handing out urls with non standard portnumbers is also not an option for you.

So here is a small snippet of apache config, how to get the two systems together.

You need to enable the proxy module in apache2, this varies in the different flavour of linux distribution. For debian based you need to do the following:

cd /etc/apache2/mods-enabled
ln -s ../mods-available/proxy.load
ln -s ../mods-available/proxy_http.load
ln -s ../mods-available/proxy_ftp.load
ln -s ../mods-available/proxy.conf

The content of the proxy.conf file looks like this:

#turning ProxyRequests on and allowing proxying from all may allow
#spammers to use your proxy to send email.
# Keep it OFF for reverse proxying!
ProxyRequests Off
AddDefaultCharset off
Order deny,allow
Allow from all
# Enable/disable the handling of HTTP/1.1 "Via:" headers.
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)
# Set to one of: Off | On | Full | Block
ProxyVia On

Now you still need to enable another module, the mod_rewrite:

cd /etc/apache2/mods-enabled
ln -s ../mods-available/rewrite.load

Now we need to edit the configuration of the named based virtual host in your apache config. You need to add the following lines to it, assuming your django server listens on localhost port 8080:

# Switch on the Rewrite Engine
RewriteEngine on
# Get everybody to use the domain with www. in front, this is search engine friendliness
RewriteCond %{HTTP_HOST} ^test\.de [NC]
RewriteRule ^/(.*)$1 [L,R]
# Surpress the TRACE and TRACK methods
RewriteRule .* - [F]
# get the admin css and img through django
RewriteRule ^/media/css/(.*)$$1 [P]
RewriteRule ^/media/img/(.*)$$1 [P]
# the /media is the place, where django saves all the uploaded media, make it available through apache
RewriteRule ^/media/ - [L]
# the /error are the apache error pages, make it available through apache
RewriteRule ^/error/ - [L]
# If the request is NOT an existing file or directory
# fetch it from the django server through reverse proxying
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$$1 [P]