Tag Archives: nginx

Building Nginx 0.9.5 on Debian Lenny

[Nginx][nginx] is available in [Debian Lenny][lenny], but the version in stable is the old 0.6.x series. [Perusio maintains a useful repository][perusio] with development versions built for Lenny, but it requires libraries newer than those in stable.

*UPDATED: fixed ‘build-essential’ – thank you Carlos*

But it is easy enough to build a [deb][deb] from the Perusio package which uses the stable libraries. Here are my notes. N.B. Editing the apt sources and installing packages needs root privileges.

First, add the Perusio repository to `/etc/apt/sources.list`:

cat >> /etc/apt/sources.list <

Caching a Django app with Nginx + FastCGI

I just spent a stupid amount of time trying to figure why [Nginx][nginx] was failing to cache my [Django][django] app responses. The Django app is running as a FastCGI backend and I have Nginx using the [`fastcgi_cache` directive][cache] to cache responses.

The answer is that [Nginx since version 0.8.44 does not cache backend responses if they have a “Set-Cookie” header][0844]. This makes perfect sense because you don’t want a response which sets a cookie to be cached for subsequent requests, but I was stupid because I had totally forgotten that my Django app was using a POST form for all responses for non-authenticated clients (due to how [Django’s CSRF middleware][csrf] does its stuff).

The solution was to change the app so that it uses the GET method on the form in question, which in this case is fine from a security point-of-view.

The moral of this story is I should pay attention to my HTTP response headers and that I am badly short-sighted both figuratively and literally. With that fixed the site has gone from 15 requests per second to ~2000 requests per second!

[nginx]: http://nginx.org/
[django]: http://www.djangoproject.com/
[0844]: http://nginx.org/en/CHANGES
[csrf]: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/
[cache]: http://wiki.nginx.org/HttpFcgiModule#fastcgi_cache

Nginx and WordPress

My [Nginx][nginx] and [Wordpress][wp] configuration on [Debian Linux 5 (Lenny)][debian]. This has Nginx as the Web server using the fastcgi module to talk to php-cgi processes that run WordPress with pretty URLs.

The virtual private server I installed this on (from [John Companies][jc]) has a 256 megabyte slice, so I figured a regular Apache + mod_php setup might be in trouble seeing as one of the sites I am running gets several thousand visits a day. Up to now I have always used Apache with mod_php to run WordPress, and anyway it is fun to learn how unfamiliar software works ([Lotus Notes][notes] excepted).

On a side note, [SysV run-levels][sysv] and /etc/rcX.d directories are needlessly clever. [sysv-rc-conf][sysvrcconf] makes editing those easy.

server {
listen 80;
server_name example.com;

root /home/david/example.com;
index index.php index.html index.htm;
error_page 500 502 503 504 /50x.html;

location / {

}

location = /50x.html {
root /var/www/nginx-default;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
location ~ /\.ht {
deny all;
}

location /b/ {
if (!-e $request_filename) {
rewrite ^(.+)$ /b/index.php?q=$1 last;
}
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

# Rewrite www.example.com to example.com
server {
listen 80;
server_name www.example.com;
rewrite ^ http://example.com$request_uri?;
}

This configuration is for WordPress installed under http://example.com/b/ on my server.

[nginx]: http://nginx.org/
[wp]: http://wordpress.org/
[jc]: http://www.johncompanies.com/
[notes]: http://www.ibm.com/software/lotus/products/notes/
[sysvrcconf]: http://sysv-rc-conf.sourceforge.net/
[sysv]: http://www.redhat.com/docs/manuals/linux/RHL-9-Manual/ref-guide/s1-boot-init-shutdown-sysv.html
[debian]: http://wiki.debian.org/DebianLenny