Lighttpd server with reverse proxy to Django

Needed a server setup so that I could run LightTPD and proxy the Django specific requests. This allows me to server static content with LightTPD and use Django for the dynamic stuff. It requires running 2 web servers, with the LightTPD forwarding the requests to the Django server when needed.

First, need to install LightTPD and Django on the systems. I’m using Windows, but it works on Linux too. I have LightTPD installed at:

d:\lighttpd

First, edit the LightTPD (d:\lighttpd\conf\lighttpd.conf on my system) configuration to server pages on port 8100:

## bind to port (default: 80)
server.port = 8100

Then, enable mod_proxy by uncommenting the “mod_proxy” line in the server.modules directive:

server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_cgi”,
…………
“mod_proxy”
)

Next, we need to tell our LightTPD server which URLs need to be sent to Django and not served locally, this is done with the following – Note that I’m using http://localhost/django to indicate the request is for Django:

This says any url matching regex “^/django” should be forwarded to the server running on the same host on port 8000 (remember LightTPD is running on 8100).

Now, anything going to http://localhost/pmo will get forwarded to Django. The next issue we run into is that Django sees the url as http://localhost/django because we are not re-writing the urls. We need to setup the urls in Django so that it responds to /pmo instead of /. This is done by setting up your urls like this instead of the default:

Now, run your Django server and go to the URL http://localhost:8100/django and your Django site will work as advertised.

After this is working, we need to setup our static files to be served out of /static from LightTPD. In your htdocs directory, create a folder called static. This is where we will put all of our static files that Django needs.

Next, go to Django and modify your settings.py to be like so:

STATIC_URL is the URL that is created from the {% static %} tag in Django. Note that by setting it to /static (and not /django/static) Django will not serve this file. It will be served by LightTPD (it does not match the pattern to proxy the request).
STATIC_ROOT is the directory that the manage script will assemble all your static resources in. You could use a temp directory and then copy them to your web server, but since my server is local I just use the local directory.
STATICFILES_DIRS is the usual Django list of directories where your static files reside.

Now, we run:

python manage.py collectstatic

This will cause Django to copy all static files from STATICFILES_DIRS and the default Django static files to your STATIC_ROOT so that LightTPD will have them to serve.

Once that is done, start your Django and LightTPD server and navigate to http://localhost:8100/django/admin/ and you should see your Django site!

Here is a link to the django app:

Leave a Reply

Your email address will not be published. Required fields are marked *