A Drupal site with several extra modules installed can leave quite a large memory footprint, especially when served through the beast that is Apache. Don't get me wrong, I think Apache is a great webserver and have used it for years without any real problems, but sometimes you need to squeeze a little more performance than Apache is willing to give. I recently moved a lot of my Drupal sites over to a 500MB Slicehost (which I would highly recommend). With only 500MB or RAM to play with, there is not much room for wastage and I found that Apache was just too much of a memory hog to be viable. So I turned to Nginx. Nginx?Nginx (pronounced "engine X" much to my surprise!) is a relatively new and little known about web server, written by a Russian by the name of Igor Sysoev. It's lightweight, fast, flexible, straight-forward to configure and is rapidly gaining popularity in the web world. Here are some facts about it (taken from nginx.net): That's quite impressive. But what's more impressive is how it performs. I'm not running any particularly high traffic web sites, but I have definitely noticed a performance gain with all of my Drupal sites since I switched. And, for my situation where I have a relatively small amount of RAM to play with, Nginx wins hands down. Joe Williams did a pretty good comparison of Apache and Nginx on his blog. The guys over at Slicehost have already written some excellent articles about setting up and configuring Nginx and FastCGI so I'm not going to cover that here. What I am going to do is share my Nginx virtual host configuration which works beautifully with Drupal. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | server {
listen 80;
server_name mydomain.com;
rewrite ^/(.*) http: //mydomain .com permanent;
}
server {
listen 80;
server_name www.mydomain.com;
access_log /path/to/mydomain .com /log/access .log;
error_log /path/to/mydomain .com /log/error .log;
root /path/to/drupal/root/ ;
index index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index .php?q=$1 last;
}
error_page 404 index.php;
location ~* .(engine|inc|info| install |module|profile|po|sh|.*sql|theme|tpl(.php)?|xtmpl)$|^(code-style.pl|Entries.*|Repository|Root|Tag|Template)$ {
deny all;
}
location ~* ^ /files/backup_migrate {
deny all;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
}
|
This configuration does several things: - Forwards all requests for mydomain.com to www.mydomain.com
- Installs an appropriate url rewrite confiruration for Drupal
- Routes all requests for .php files through fastcgi
- Serves static files directly
- Protects important Drupal core files (normally done by .htaccess)
- Protects directory used by the backup_migrate module (normally done by .htaccess)
Obviously you need to substitute the path names to make them correct for your own system. I don't want to take credit for any of this as it was cobbled this together from several other resources, mainly from a thread on drupal.org entitled Nginx, Fastcgi, PHP, rewrite config for Drupal. |