IPv6 support in nginx

nginx logoTo enable IPv6 support in nginx, we need to check whether it has been compiled with --with-ipv6 flag. To check, fire up the terminal and type in this command :

[code language=”bash”]nginx -V[/code]

The results should be something like this :

[code language=”bash”]nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –pid-path=/var/run/nginx.pid –lock-path=/var/lock/nginx.lock –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/body –http-proxy-temp-path=/var/lib/nginx/proxy –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –with-debug –with-http_stub_status_module –with-http_flv_module –with-http_ssl_module –with-http_dav_module –with-http_gzip_static_module –with-http_realip_module –with-mail –with-mail_ssl_module –with-ipv6 –add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair[/code]

Pre-compiled Debian/Ubuntu packages already has IPv6 support built-in.

Now we need to edit the configuration file to tell nginx to bind to IPv6 addresses(as well as IPv4 addresses).

[code language=”bash”]sudo nano /etc/nginx/sites-available/default[/code]

Depending on your configuration, this file might be located somewhere else. (Check /usr/local/nginx/conf/nginx.conf if you compiled nginx manually)

Search for listen directives and change them as follows:
[code]listen [::]:80;[/code]
This will make nginx bind to both IPv6 and IPv4 addresses.

In order to bind to IPv6 addresses only (no IPv4), use the following:
[code]listen [::]:80 default ipv6only=on;[/code]

In order to bind to a specific IPv6 address:
[code]listen [2001:6f8:1c00:16d::2]:80;[/code]

When you are done, reload the nginx configuration file by typing in :
[code language=”bash”]nginx -s reload[/code]

Now we have to check whether nginx is listening to both IPv6 and IPv4 requests:
[code language=”bash”]netstat -tulpna | grep nginx[/code]
You’ll get something like this :
[code]tcp6 0 0 :::80 :::* LISTEN 1891/nginx[/code]

You have successfully configured nginx to respond to IPv6 requests.

Nginx reduces page load time, increases Googlebot activity

nginx logoAfter a small incident with a shared host, I moved my phpBB forum and a few other static sites to a VPS running on nginx. Nginx is a small, lightweight but very efficient web server created by Igor Sysoev, originally developed for www.rambler.ru, Russia’s second-largest web site. Due to it’s light-weightedness and efficiency, it is used by a lot of high traffic sites like WordPress, Hulu, Github, Ohloh, SourceForge, TorrentReactor, etc. After moving to Nginx, I noticed a significant improvement in page load times. The pages on the site load in around 0.0X seconds now, as reported by the phpBB’s measurement system. The pages also seem to be snappier as compared to the time when I was on Litespeed.

I recently noticed that apart for the decrease in page loading time, the activity of Googlebot has increased manyfold. Here’s a screen-shot of the report from Google Webmaster Tools :

Google Webmaster Tools NginxNot bad, eh?

More recently, I moved this blog to the VPS running on Nginx, and I have not been disappointed. This blog is running on WordPress 3.0.1, with no caching plugins. The posts seem to load faster than ever, as evident from this Google Webmaster Tools report :

google webmaster tools nginx techtitbits

I couldn’t be more happy.

PS: Of course, some of the speed improvement can be attributed to the fact that I moved from a shared server to a VPS, but that doesn’t undermine the awesomeness that is Nginx.

Enable directory listing in nginx

nginx logoEnabling directory listing in a folder in nginx seems simple enough with just an autoindex on; directive inside the location directive. However, for some reason, it didn’t work for me.

I finally got it to work by moving the root directive out of location.
So, if you have something like this :

[code]server {
listen 80;
server_name domain.com www.domain.com;
access_log /var/………………………;
location / {
root /path/to/root;
index index.php index.html index.htm;
}
location /somedir {
autoindex on;
}
}[/code]

Change it to :

[code]server {
listen 80;
server_name domain.com www.domain.com;
access_log /var/………………………;
root /path/to/root;
location / {
index index.php index.html index.htm;
}
location /somedir {
autoindex on;
}
}[/code]

Directory indices should show flawlessly now.
(A live example can be found here.)

Www/no-www rewrite rules for nginx

nginx logoThere are many ways to rewrite www urls to their non-www versions in nginx. Here one that’s Igor-approved and works well on my setup :

WWW to Non-WWW:

[code]#301 redirect www to non-www
server {
listen [::]:80;
server_name www.domain.com;
rewrite ^ http://domain.com$request_uri? permanent;
}
server {
listen [::]:80;
server_name domain.com;
…………………………………..
…………………………………..
}[/code]

Non-WWW to WWW:

[code]#301 redirect non-www to www
server {
listen [::]:80;
server_name domain.com;
rewrite ^ http://www.domain.com$request_uri? permanent;
}
server {
listen [::]:80;
server_name www.domain.com;
…………………………………..
…………………………………..
}[/code]

Unexpected email and Alexa bot

I got a surprise today when I found that I have an email in my inbox from webmaster@animorphsfanforum.com. The email had subject “Subject” and has content “Content”. My first instinct was that someone has hacked into my VPS. After a few minutes of mind-racking, I remembered that I had created a PHP file on the VPS named ‘mail.php’ to test whether emails were being sent. But, no one knew of it’s existence except me. Someone must’ve stumbled upon it my chance.

I logged into my VPS and checked the access logs.
[bash]cat animorphsfanforum.access.log | grep ‘mail.php'[/bash]
[code]174.129.237.157 – – [20/Jul/2010:05:32:40 +0000] "GET /mail.php HTTP/1.0" 200 0 "-" "ia_archiver (+http://www.alexa.com/site/help/webmasters; crawler@alexa.com)"[/code]

I have a Firefox plugin to check the Alexa rank, and the URL must’ve been sent to Alexa when I executed that script for the first time. The Alexa bot sent the email while crawling the URL.

Much ado about nothing?