Varnish 3 on Nginx for Drupal 7

photo

After playing around Nginx, php-fpm for Drupal with AWS, today I tried Varnish – one of the best caching tool (see this) works great with Drupal 7.

Here’s the note on installing, configuring Varnish 3 for Drupal 7.

Installing Varnish

Install Varnish

$ apt-get install varnish

Check the version

$ varnishd -V

Edit VCL file:

$ nano /etc/varnish/default.vcl

Copy Mark’s configuration here: Configure Varnish 3 for Drupal 7, thanks Mark!

Edit startup settings,

$ nano /etc/default/varnish

Uncomment these and change the port to 80

DAEMON_OPTS="-a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256M"
Edit Nginx

$ nano /etc/nginx/sites-available/example.com

Add

listen 8080;
`$ /etc/init.d/nginx reload`

Restart services

$ service nginx restart; service varnish restart;

Check which ports are open

$ netstat -anp --tcp --udp | grep LISTEN

Should look like this

tcp 00 0.0.0.0:22 0.0.0.0:* LISTEN 604/sshd tcp 00 127.0.0.1:6082 0.0.0.0:* LISTEN 1105/varnishd tcp 00 127.0.0.1:3306 0.0.0.0:* LISTEN 1654/mysqld tcp 00 127.0.0.1:11211 0.0.0.0:* LISTEN 2072/memcached tcp 00 0.0.0.0:8080 0.0.0.0:* LISTEN 1137/nginx tcp 00 0.0.0.0:80 0.0.0.0:* LISTEN 1107/varnishd tcp6 00:::22:::* LISTEN 604/sshd tcp6 00:::80:::* LISTEN 1107/varnishd
#### Varnish on Drupal

Install varnish and enable it.

$ drush dl varnish; drush en varnish -y

Copy the varnish secret key.

$ cat /etc/varnish/secret

On your site, go to admin/config/development/varnish, paste the secret key. Then go to

admin/config/development/performance

Enable cache and set the life time!

Screen Shot 2013-04-01 at 下午7.49.00

Edit Drupal 7 settings.php

$ nano sites/default/settings.php

Make sure these are not commented.

$conf['reverse_proxy']=TRUE;$conf['reverse_proxy_header']='HTTP_X_CLUSTER_CLIENT_IP';$conf['reverse_proxy_addresses']=array('127.0.0.1');
Add this to the end
/* Varnish */$conf['cache_backends'][]='sites/all/modules/varnish/varnish.cache.inc';$conf['cache_class_cache_page']='VarnishCache';// Drupal 7 does not cache pages when we invoke hooks during bootstrap.// This needs to be disabled.$conf['page_cache_invoke_hooks']=FALSE;
Open an incognito window on Chrome, go to your site, check response header to see if there is:

X-Drupal-Cache:HIT

and

X-Varnish-Cache:HIT

Screen Shot 2013-04-01 at 下午7.51.47