IT

SMF + PrettyURL module running on nginx

Introduction

As I was looking for a VPS hosting for my two domains, I realised that to run Apache / PHP / MySQL on a host with more than 500 unique visitors per day I need to by at least 512Mb or dedicated RAM, if not 1024Mb (costly!).

I decided to investigate the possibility of running SMF (simplemachines forum) along with PrettyURL module for SEO optimisation (using Apache mod_rewrite plugin) inside nginx which everyone talks about as being lightweight and stable.

Converting PrettyURL rules:

# Pretty URLs mod
# http://code.google.com/p/prettyurls/
# .htaccess file generated automatically on: September 4, 2007, 21:46
RewriteEngine on
# Rules for: boards
RewriteRule ^([-_!~*'()$a-zA-Z0-9]+)/?$ ./index.php?pretty;board=$1.0 [L,QSA]
RewriteRule ^([-_!~*'()$a-zA-Z0-9]+)/([0-9]*)/?$ ./index.php?pretty;board=$1.$2 [L,QSA]
# Rules for: topics

RewriteRule ^([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/?$ ./index.php?pretty;board=$1;topic=$2.0 [L,QSA]

RewriteRule ^([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*|msg[0-9]*|new)/?$ ./index.php?pretty;board=$1;topic=$2.$3 [L,QSA]

After some trial and error, I came up with these rules that have to be added in nginx.conf file:

server {
...
location /smf/ {
index index.php;
rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2.0 last;
rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2.$3 last;
rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2%3Btopic=$3.0 last;
rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*|msg[0-9]*|new)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2%3Btopic=$3.$4 last;
}
...
}

Update on 23 Jan, 2009:

Here are the settings I’ve now been using for almost one year:

location /community/
{
index index.php;
rewrite ^(/community)/([-_!~*'()$a-zA-Z0-9]+)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2.0 last;
rewrite ^(/community)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2.$3 last;
rewrite ^(/community)/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2%3Btopic=$3.0 last; rewrite ^(/community)/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*|msg[0-9]*|new)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2%3Btopic=$3.$4 last;
}

Conclusions

So far, it works! The memory footprint for nginx, php5 (running as fastcgi server) is around 100Mb (while the system itself, at boot, takes about 30Mb). MySQL database is hosted on the main system [in fact, I am running everything inside a coLinux hosted Ubuntu 6.06 – with nginx compiled from source code as I haven’t found a package for Ubuntu 6.06]. Database resides on Windows XP right now, outside the coLinux environment.

On my Toshiba L10-202, with a CeleronM at 1600Mhz, with coLinux set up to use maximum 256Mb, I still get the SMF page rendered in less than 0.12s. Which is pretty impressive!

I will keep this post updated, as I am still investigating how to make WordPress MU (which I integrated with SMF using a sweet module from earthorbit.com) and still have everything working toghether.

PS: Another option for lightweight ‘hosting package’ was lighttpd, but I gave up on it mainly because even if advertisied as fast and easy, but it has its drawbacks for people wanting to jump ship from Apache. To make the rewrite rules work, I had to: learn how to configure lighttpd, learn how use mod_magnet plugin because you need to learn LUA scripting language; and after all these, in the end, I hat to figure out how to convert Apache rules to LUA.

Also, there’s a big warning on lighttpd wiki:

“Keep in mind that the magnet is executed in the core of lighty. EVERY long-running operation is blocking ALL connections in the server. You are warned. For time-consuming or blocking scripts use mod_fastcgi and friends.”

which I don’t know how to interpret but sounds very scary [even if down below, they say the script is cached]. But I sure do not want to have all the connections to the servers blocked!

Please use the forum if you need aditional information, I’ll try to answer as soon as possible.

Leave a Reply