nginx+drupal revisited

Posted by: on Aug 15, 2009 | No Comments

nginx

Recent nginx updates support try_files and internal location directives. These features make nginx more flexible as a web server for Drupal.

  • try_files checks for existence of files in order, and returns the first file that is found. In Drupal’s logic, try_files enables the server to check Boost-generated cache, imagecache images, and Drupal installation in order.
  • @location syntax for internal locations. Internal locations are not exposed directly via nginx. They are accessible by try_files, customized 40x messages, and rewrites.

drupal

Using try_files and @location syntax together provides an easier way to run Drupal.

1
2
3
4
5
6
7
8
9
10
11
12
location / {
  try_files $uri $uri/ @drupal;
}
location @drupal {
  rewrite ^/(.*)$ /index.php?q=$1 last;
}
location ~ \.php$ {
  fastcgi_pass    127.0.0.1:3456;
  fastcgi_index   index.php;
  fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include         fastcgi_params;
}

Most FastCGI parameters are in fastcgi_params which comes by default in nginx installation.

security and performance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# protection for sensitive info
location ~ (/\..*|settings\.php$|\.(htaccess|engine|inc|info|install|module|profile|pl|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(Entries.*|Repository|Root|Tag|Template))$ {
  deny all;
}
# turn off access logs for stylesheets and scripts
location ~ \.(css\js)$ {
  access_log off;
}
# performance for images
location ~* \.(jpg|jpeg|png|gif|ico)$ {
  expires 45d;
  access_log off;
}
# fix imagecache issue because the image configuration above
location ~ /imagecache/ {
  try_files $uri @drupal;
  expires 45d;
}

Supported by Webinit Consulting