How to Speed up Your Django Sites with NginX, Memcached, and django-compress
Posted on April 23rd, 2009 by Greg Allard in Django, Programming, Server Administration | Comments
A lot of these steps will speed up any kind of application, not just django projects, but there are a few django specific things. Everything has been tested on IvyLees which is running in a Debian/Ubuntu environment.
These three simple steps will speed up your server and allow it to handle more traffic.
Reducing the Number of HTTP Requests
Yahoo has developed a firefox extension called YSlow. It analyzes all of the traffic from a website and gives a score on a few categories where improvements can be made.
It recommends reducing all of your css files into one file and all of your js files into one file or as few as possible. There is a pluggable, open source django application available to help with that task. After setting up django-compress, a website will have css and js files that are minified (excess white space and characters are removed to reduce file size). The application will also give the files version numbers so that they can be cached by the web browser and won’t need to be downloaded again until a change is made and a new version of the file is created. How to setup the server to set a far future expiration is shown below in the lightweight server section.
Setting up Memcached
Django makes it really simple to set up caching backends and memcached is easy to install.
sudo aptitude install memcached, python-setuptools
We will need setuptools so that we can do the following command.
sudo easy_install python-memcached
Once that is done you can start the memcached server by doing the following:
sudo memcached -d -u www-data -p 11211 -m 64
-d will start it in daemon mode, -u is the user for it to run as, -p is the port, and -m is the maximum number of megabytes of memory to use.
Now open up the settings.py file for your project and add the following line:
CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
Find the MIDDLEWARE_CLASSES section and add this to the beginning of the list:
'django.middleware.cache.UpdateCacheMiddleware',
and this to the end of the list:
'django.middleware.cache.FetchFromCacheMiddleware',
For more about caching with django see the django docs on caching. You can reload the server now to try it out.
sudo /etc/init.d/apache2 reload
To make sure that memcached is set up correctly you can telnet into it and get some statistics.
telnet localhost 11211
Once you are in type stats and it will show some information (press ctrl ] and then ctrl d to exit). If there are too many zeroes, it either isn’t working or you haven’t visited your site since the caching was set up. See the memcached site for more information.
Don’t Use Apache for Static Files
Apache has some overhead involved that makes it good for serving php, python, or ruby applications, but you do not need that for static files like your images, style sheets, and javascript. There are a few options for lightweight servers that you can put in front of apache to handle the static files. Lighttpd (lighty) and nginx (engine x) are two good options. Adding this layer in front of your application will act as an application firewall so there is a security bonus to the speed bonus.
There is this guide to install a django setup with nginx and apache from scratch. If you followed my guide to set up your server or already have apache set up for your application, then there are a few steps to get nginx handling your static files.
sudo aptitude install nginx
Edit the config file for your site (sudo nano /etc/apache2/sites-available/default) and change the port from 80 to 8080 and change the ip address (might be *) to 127.0.0.1. The lines will look like the following
NameVirtualHost 127.0.0.1:8080 <VirtualHost 127.0.0.1:8080>
Also edit the ports.conf file (sudo nano /etc/apache2/ports.conf) so that it will listen on 8080.
Listen 8080
Don’t restart the server yet, you want to configure nginx first. Edit the default nginx config file (sudo nano /etc/nginx/sites-available/default) and find where it says
location / { root /var/www/nginx-default; index index.html index.htm; }
and replace it with
location / { proxy_pass http://192.168.0.180:8080; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } location /files/ { root /var/www/myproject/; expires max; }
/files/ is where I’ve stored all of my static files and /var/www/myproject/ is where my project lives and it contains the files directory.
Set static files to expire far in the future
expires max; will tell your users’ browsers to cache the files from that directory for a long time. Only use that if you are use those files won’t change. You can use expires 24h; if you aren’t sure.
Configure gzip
Edit the nginx configuration to use gzip on all of your static files (sudo nano /etc/nginx/nginx.conf). Where it says gzip on; make sure it looks like the following:
gzip on; gzip_comp_level 2; gzip_proxied any; gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
The servers should be ready to be restarted.
sudo /etc/init.d/apache2 reload sudo /etc/init.d/nginx reload
If you are having any problems I suggest reading through this guide and seeing if you have something set up differently.
Speedy Django Sites
Those three steps should speed up your server and allow for more simultaneous visitors. There is a lot more that can be done, but getting these three easy things out of the way first is a good start.
I use webfaction to host a lot of my django projects. It has an easy setup that will get you developing quickly and a great community of talented programmers. There is also a quick setup for rails, wordpress, and a lot more.
Related posts:
- How to Display Realtime Traffic Analytics Users of Presskit’n have been asking for traffic statistics on their press releases so I decided I would get them...
- Python Projects in Users’ Home Directories with wsgi Letting users put static files and php files in a public_html folder in their home directory has been a common...
- Static Files in Django on Production and Development Update 2009-03-25 I realize why this isn’t needed. If your production environment is set up correctly, django will never serve...