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:

#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;
        .........................................
        .........................................
}

Non-WWW to WWW:

#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;
        .........................................
        .........................................
}

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.

cat animorphsfanforum.access.log | grep 'mail.php'
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)"

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?

Nginx: Resolving “No input file specified” error

nginx logoIf you are using nginx with php-cgi and have followed the standard procedure to set it up, you might often get the “No input file specified.” error. What happens here is that, when nginx receives the request to serve a non-existent php file, it passes the request to php-cgi. Php-cgi, while trying to processs the request, finds that the php file does not exist at all. Hence it sends a “No input file specified.” message with a “404 Not Found” header.

Although, it’s not technically an error, we can catch the request for the non-existent php file and show a custom 404 page.

First, find out the version of nginx you are using.

nginx -V

You’ll get an output like this :

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-2~bpo50+1-i386-1taiV6/nginx-0.7.65/modules/nginx-upstream-fair

If php-cgi is running on port 9000, you’ll have something like this in your vhosts file :

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

Since nginx versions less than 0.6.36 doesn’t have the try_files directive, we’ll have two versions of the code.

For nginx 0.6.36+
We’ll use try_files here to catch and display an error page.

location ~ \.php$ {
try_files  $uri  /path/to/404.htm;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME ....
...................................
...................................
}

For older versions :

location ~ \.php$ {
fastcgi_intercept_errors on;
error_page  404  /path/to/404.htm;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME ....
...................................
...................................
}

This will display the chosen 404 page for non-existent php files instead of the unhelpful “No input file specified.” message.

Finding the Lubuntu 10.04 torrent

I’ve been meaning to try out Lubuntu for quite some time, but was thwarted by the lack of a torrent. If you didn’t know, Lubuntu is an Ubuntu variant using the lightweight LXDE desktop environment, like Kubuntu uses KDE or Xubuntu uses XFCE. It hasn’t been officially approved as a part of the Ubuntu family yet, but judging from the favourable reviews it has been getting, that day might not be far away. Lubuntu is supposed to be much lighter than Xubuntu, which I’m currently using and seems like it is all that.

Anyway, the official Lubuntu.net page doesn’t show a torrent or metalink download option. Search for a torrent serves up the Lubuntu beta2 torrent or custom Lubuntu distributions created by enthuciasts. I finally got the official torrent from the mailing list archive.

Without further ado, here’s the torrent downlad link :

http://people.ubuntu.com/~gilir/lubuntu-10.04.iso.torrent

Hope this saves you a little frustration.

Storing and reusing Ubuntu updates

Suppose you have two computers running the same version of Ubuntu and you have downloaded and installed the updates on one of the computers. Since you are having a rather expensive or maybe, a limited internet connection, you do not wish to download the updates again for the other computer.

Updates downloaded by aptitude are stored in the directory /var/cache/apt/archives. You’ll find the package files in .deb format(Debian software package format). If these updates are copied to the same directory on the other computer, aptitude will automatically recognise those files and use them as necessary while updating. You can copy the files using a flash drive or write them to a CD if you wish to.
You can copy the packages using this command :

sudo cp /var/cache/apt/archives/* /path/to/flash/drive

Now connect the flash drive to the other computer and copy the files to the /var/cache/apt/archives/ directory there.
After this, you need to update your package information and install the updates found.
Run :

sudo apt-get update && sudo apt-get upgrade

All the required updates will be installed from the /var/cache/apt/archives/ directory(if available). You can also install new packages, and they will not be downloaded if they are available locally.