Posts Tagged ‘nginx’

nginx conf with alternate cache dir

Monday, January 7th, 2008

Its a good idea to use an alternate cache directory in rails (in your environment.rb):

config.action_controller.page_cache_directory = RAILS_ROOT + "/public/cache/"

This makes it easier to sweep. To setup nginx to pick up from a rails alternate cache directory like public/cache, I used:

if ($http_user_agent ~* "(iPhone|iPod)") {
  rewrite ^/$ /iphone break;
  proxy_pass http://mongrel-pt;
  break;
}
 
if (-f $request_filename) {
  break;
}
 
if (-f $document_root/cache/$uri/index.html) {
  rewrite (.*) /cache/$1/index.html break;
}
 
if (-f $document_root/cache/$uri.html) {
  rewrite (.*) /cache/$1.html break;
}
 
if (-f $document_root/cache/$uri) {
  rewrite (.*) /cache/$1 break;
}
 
if (!-f $request_filename) {
  proxy_pass http://mongrel-pt;
  break;
}

It was a little tricky to figure out to use the $uri variable. The first rewrite is used for iphone requests so it doesn’t get the non-iphone cached page.

nginx

Monday, September 3rd, 2007

I switched from apache + mod_proxy_balancer to nginx, which on my lame VPS is saving me around 14 precious MB of memory. There are so many reasons to use nginx. Ezra recommends the monit + nginx + mongrel_cluster stack, and my friend/co-worker has a great conf to start with. It took me about 5 minutes to setup, even with multiple hosts.

My nginx.conf and init script.

Nginx is hyper optimized…

1
2
3
4
./ngx_http_upstream.c:        if (h->value.len == 2) {
./ngx_http_upstream.c:            if (c0 == 'n' && c1 == 'o') {
./ngx_http_upstream.c:        } else if (h->value.len == 3) {
./ngx_http_upstream.c:            if (c0 == 'y' && c1 == 'e' && c2 == 's') {