I recently heard about Nginx. It's a web server with a great track record in Russia that's starting to spread out to the rest of the world. According to a recent podcast I heard, Zed seems to be using it with his production Rails apps, which is always a good sign. At RailsConf I got to eat with Adam Keys and his enthusiasm for it's speed, ease of configuration, and stability caught my attention.
The greatest attraction for me is the ease of configuration. I've wrestled with Apache conf files on and off though the years, but I've never felt like I really "got it". Apache can do nearly anything, but you've got to really invest the time to become an Apache specialist to do anything useful with it. And I never really felt the motivation to become an Apache specialist.
When I set up this web site, I tried to get my Mongrel cluster up and running with Apache but couldn't get it working. So I gave up and dropped back to Pound and Mongrel. Pound load balanced and Mongrel served everything, including the html, Javascripts, CSS, and graphics. A complete reload of the main page took about 30 seconds. Nearly all the time was spent loading graphics, two at a time. And once the browser had the graphics cached, it ran great.
But today I switched to Nginx and a clean reload is taking me 8 seconds. Granted, Mongrel wasn't designed to serve your static content, but wow... that's an improvement!
Here are some Nginx links to get you started.
And here's my configuration file:
worker_processes 2;
events {
worker_connections 1024;
}
http {
include conf/mime.types;
default_type application/octet-stream;
upstream mongrel {
server 127.0.0.1:7000;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
server {
listen 80;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/access.log main;
rewrite_log on;
location ~ ^/$ {
if (-f /index.html){
rewrite (.*) /index.html last;
}
proxy_pass http://mongrel;
}
location / {
if (!-f $request_filename.html) {
proxy_pass http://mongrel;
}
}
location ~ .html {
root /usr/local/nginx/html;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar
|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
root /usr/local/nginx/html;
}
}
}
Update: Apparently this call request.host_with_port gets confused when Nginex proxies for Mongrel. If you're reading this via the RSS feed, then you probably noticed that all my URLs were wrong for a few hours. It's fixed now. I should wrap that with a test!