Drupal

Nginx configuration for Drupal sites

I have created a preferred configuration for hosting Drupal sites on the excellent nginx web server. I tend to prefer nginx over Lighttpd and/or Apache for at least over a year now and I'm extremely happy with its reliability and performance.

Apart from configuring a little more and getting used to the fact that no run-time configurations are available through .htaccess files, nginx with php-fpm is really a viable option for hosting Drupal sites.

'Nuff said, you can head over to GitHub to check out my configuration. The files have many smart ideas taken from yhager's nginx config, however I added more features and tailored the configuration for my needs.

It has the following features:

Anyway feel free to add patches or comments: Drupal hosting on nginx

Setting up cookieless fake CDNs for Drupal

So you want your Drupal site perform as quick as possible and you have set up CDN and patched imagecache and everything is working fine. Then you realize that any asynchronously generated static resource (e.g. image, stylesheet, etc.) will naturally send a session cookie to your visitors.

The very promising Advanced CSS/JS Aggregation and ImageCache both handle requests in a unique way: if the requested resource is not generated yet, it will run through Drupal, create the resource and place it in the path exactly where the request points to. This way, subsequent requests does not need to go through Drupal and PHP again, it can be served as a static resource.

However since the first request runs through Drupal, it will start a session (Drupal 6 by default starts sessions for anonymous users) This is okay if you use a single domain to serve files from. Things get annoying when you start using alternate host names to speed up page loading times, and you realize that these precious alternate host names start sending out session cookies to your visitors, and you are basically doubling (or with 3 cname domains, that is quadrupling) your session rows if a visitor happens to hit a static resource for the first time.

CDNs - even if they are just fake CNAME records pointing to the same webroot - should operate cookieless to keep the http request headers as short as possible for subsequent requests.

One could (eliminate anonymous sessions)[http://2bits.com/articles/reducing-server-load-eliminating-anonymous-sessions-drupal-6x.html] easily from the entire site, but that might not be your cup of cake because anonymous sessions are needed so much say on an Ubercart site to track cart contents.

My solution is to use an alternate session handler which in fact does not handle sessions at all. Configure this session handler to be fired only at your cdn domains and you're set to go.

Using Drupal Imagecache with Lighttpd proxied Apache

Using Lighttpd as a proxy for Apache is a popular solution to achieve smaller web server memory footprint and to speed up websites. However things can get messy when it comes to Drupal and the brilliant Imagecache module. Imagecache transforms images using presets and then caches them.

An imagecache url is something like: http://domain.tld/sites/default/files/imagecache/presetname/photo.jpg This will take the the image placed at sites/default/files/photo.jpg and transforms it according to presetname. After succesful operation the image will be available as a static file at sites/default/files/imagecache/presetname/photo.jpg

No need to say, this functionality breaks when the frontend web server only forwards requests to php files so imagecache images won't show up and will not be generated.

One solution is: set up a separate subdomain to serve these images and redirect all imagecache images to this subdomain. This needs some simple LUA scripting, Lighttpd will forward an imagecache url if it hasn't created yet, and will serve it directly if it's already there.