New Nginx.conf with optimizations

Posted by ezmobius Wed, 03 Jan 2007 22:28:00 GMT

I have been working on optimizing my nginx.conf file as I use it on more and more sites. Thanks to folks on the nginx and mongrel mailing lists for some of these fixes. Main improvements is in gzip of dynamic and other content as well as setting the proxy buffers to 0 and a few improvemets in the proxy header settings. This conf also includes a second vhost for ssl that points to the same mongrel cluster so you can hanlde ssl and non ssl with the same cluster and rails request.ssl? helper will work correctly.

Another big improvement is with static file handling. The credit on this one goes to Zed Shaw. He notcies that the rewrites and regex tests for rails cached pages were getting run for every request including static images. So a request for /images/foo.jpg woudl get served in this manor:

/images/foo.jpg/index.html  # no file found continue
/images/foo.jpg.html  # no file found continue
/images/foo.jpg # good send it!

This is obviously a performance hit for static files as it has two extra regexes and two extra file stats for every static image. Suck, I wish I found this sooner. To fix this all we need to do is add this test right at the top before the other rewrite tests:

      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }
In my benchmarks this does increase the speed of static files about 8%. Also allowing gzip on HTTP 1.0 requests made sure that the proper static assets are gzipped when possible. Also we force all proxied requests to be gzipped if they are the right content type. I also added more gziped content types and corrected a wrong one for javascript.

This is an important lesson about your production environment folks. With regards to what you accept as gospel from someone like me. “Trust but verify”. Zed was trying to track down how to do a custom rewrite for alternate cache directories and so he at my behest turned on the debug logging and consequently saw the rewrites happening on every static file. I haven’t been in the rewrite log for a while since I thought my config was good. So when you get config files from people like me off the net, please trust but verify on your own systems.

This new config is running close to 100 instances of nginx at Engine Yard. Nginx has proven to be an excellent choice for a front end proxy balancer for mongrel. It beats apache in all my tests now as far as non gzipped proxy requests, static file requests and it ties with apache for gzipped proxy requests. And its about 30 times more lightweight then apache.

If you were holding off to try nginx until it was proven out by others then let me tell you it has proven itself to me time and time again already as a very easy to manage, high performance options for rails deployment on mongrel. I don’t personally use apache2.2 right now for anything other than mod_svn for subversion over HTTP. But there is stirring of upcoming support for nginx and svn over webdav so stay tuned. When that happens I will happily drop apache from our rotation entirely. Of course take this with a grain of salt as always, I don’t have to support any php apps or legacy stuff right now, just rails. I haven’t personally used php with nginx yet but I see others getting fantastic results so I am not worried for when I may need it.

All in all this new config file performs significantly better then my old one. Somewhere on the order of 20% total improvement overall over the last config file. I have updated the link to poitn from the old article to the right confi file and I am linking it here again for your enjoyment:

New Nginx.conf

Tags , , , ,  | 17 comments

New Nginx Conf with Rails Caching

Posted by ezmobius Tue, 12 Sep 2006 16:02:00 GMT

[UPDATE] The config file has been updated and commented so it is easier to figure out. It also sets the right header when it proxies to mongrel and doesn;t choke on the foo.js?394732323 urls that rails generated for static assets.

OK this is very sweet. We have a new Nginx conf file that works perfectly with rails page caching. nginx serves all static files and all rails cached file. Fast

I want to thank Alexy Kovyrin and James Cox for their help in getting this config perfected. This makes nginx truly one of the best options for fronting a cluster of mongrels.

Might as well get the latest version while we’re at it.

curl -O http://sysoev.ru/nginx/nginx-0.4.0.tar.gz
tar -xvzf nginx-0.4.0.tar.gz
cd nginx-0.4.0
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
make
sudo make install

Now for the new config file. Here you go folks, get it while its hot!

nginx.conf

Tags , , , ,  | 28 comments

Nginx, my new favorite front end for mongrel cluster.

Posted by ezmobius Wed, 23 Aug 2006 20:27:00 GMT

I have been on the lookout for a better front end for mongrel_clusters. I wanted one that had the flexibility of pound when it comes to proxying requests. But I also want something to serve static and cached files fast. Apache2.2/mod_proxy_balancer does a good job of this but it is way overkill if you don’t need apache for other sites with php or whatever. Lighttpd has always been one of my favorites but the current release has a broken mod_proxy that will not re-enable backends without a restart if they go down and back up again. Lighty will have a much better mod_proxy_core when 1.5 is released but thats a ways off it looks like and is not currently usable.

So I am very happy that I recently found a lightweight webserver that can serve static files very fast and that also has very complete proxy and rewrite modules. Its called nginx(which I have been pronouncing in my head as ‘engine x ;). I am very impressed with its performance and features. It is also super lightweight, taking up only a few Mb of ram when fully loaded while benchmarking. Nginx also has mod_fcgi. So it will work for externally spawned dispatch.fcgi’s or php-fcgi scripts. It does balancing and failover for fcgi as well. I haven’t tested the fcgi support yet but will do so in the near future. The only drawback I have found is that it is developed in Russia and while there are some english translations of the docs, they are not complete and don’t cover the proxy and rewrite modules yet.

But don’t fret, I have done most of the hard work for you and figured out most of the configuration needed to use nginx as a great front end for mongrel clusters. Nginx is my new favorite front end for my rails apps and has been very stable during my testing and abuse of it. It is actually faster then lighty for static files and the proxy and rewrite modules are way more complete. In my benchmarks on my macbookpro, nginx serves static files at 13+k requests/sec. And it performs better for proxied requests then pen, pound or apache2.2. It also handles ssl and vhosts like pound does, including regex conditionals and other niceties.

Click throuhg for the full details.

Read more...

Tags , , , ,  | 48 comments