Using nginx static caching with phpBB and Drupal
• ∞
At rainmeter.net, we serve over 500000 pages per day. To handle the load on our cheap VPS with 512MB of RAM, we decided to make the jump to nginx. The switch from Apache alone was a great improvement. To further improve performance, we harnessed the excellent static cache provided by the nginx HttpFcgi module. The relevant section of the configuration file is below:
fastcgi_cache_path /tmp/nginx levels=1:2 keys_zone=main:64m inactive=5m;
server {
# ...
location ~ \.php$ {
set $no_cache "";
if ($cookie_DRUPAL_UID != "") {
set $no_cache "1";
}
set $forum "";
if ($cookie_phpbb3_7726s_u != "") {
set $forum "1";
}
if ($cookie_phpbb3_7726s_u != "1") {
set $forum "${forum}1";
}
if ($forum = "11") {
set $no_cache "1";
}
fastcgi_cache_key "$host$request_uri";
fastcgi_cache main;
fastcgi_no_cache $no_cache;
fastcgi_cache_bypass $no_cache;
fastcgi_cache_valid 200 302 5m;
fastcgi_cache_min_uses 2;
fastcgi_ignore_headers Expires Cache-Control;
# more fastcgi stuff ...
}
}
In order to serve dynamic content, caching has to be disabled for authenticated users. For Drupal, this is as simple as checking for the existence of the DRUPAL_UID cookie. For phpBB, caching is disabled when the phpbb3_7726s_u cookie exists and is not set to 1. The cookie names will likely differ in your set-up.
Since the vast majority of our traffic is from anonymous users, nginx is able to serve most of the pages directly from the cache without interaction with PHP and MySQL. The overall improvement was massive — the initial load time was reduced from over 4 seconds (with Apache) to just 1 second at best (with nginx).