Skip to main content

Drupal 7.x, Varnish 3.x and Apache 2.x configuration - all in one post

Drupal ver.: 

In this blog post i’ll shortly describe how to configure Drupal 7.x, Varnish 3.x, Apache 2.x and also expose some possible issues.

Below you can check the points you should go through to get Varnish cache work with your Drupal installation:

  1. Install Varnish 3.x
  2. Update /etc/varnish/default.vls
  3. Update /etc/default/varnish
  4. Configure Apache
  5. Configure Drupal
  6. Possible issues
  7. Is it working?

Install Varnish 3.x

I believe the best Varnish installation tutorial published on the official website. This is a quote for your convenience:

Installation on Ubuntu

$ curl | sudo apt-key add -
$ echo "deb precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install varnish

Update /etc/varnish/default.vcl

Make back-up and override your /etc/varnish/default.vls by the Fourkitchens default.vcl.

Update /etc/default/varnish

Open /etc/default/varnish file

$ sudo nano /etc/default/varnish

Uncomment Alternative 2, and change the listener port from 6061 to 80. The configuration should match the next snippet:

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

Configure Apache

Set Apache to listen on port 8080:
$ sudo nano /etc/apache2/ports.conf
Update port settings in in the default virtual host file:
$ sudo nano /etc/apache2/sites-available/default

Do the same for all other vhosts as well.

Restart Apache and Varnish

$ sudo service apache2 restart
$ sudo service varnish restart

Configure Drupal

Download and install Varnish HTTP Accelerator Integration module

$ drush dl varnish
$ drush en varnish -y

Add the next code to the end of your settings.php file. Please check if relative path to and secret files are correct:

$conf['cache_backends'] = array('sites/all/modules/contrib/varnish/');
$conf['cache_class_cache_page'] = 'VarnishCache';
$conf['page_cache_invoke_hooks'] = FALSE;
$conf['reverse_proxy'] = TRUE;
$conf['cache'] = 1;
$conf['cache_lifetime'] = 0;
$conf['page_cache_maximum_age'] = 21600;
$conf['reverse_proxy_header'] = 'HTTP_X_FORWARDED_FOR';
$conf['reverse_proxy_addresses'] = array('');
$conf['omit_vary_cookie'] = TRUE;
$conf['varnish_control_key'] = substr(file_get_contents('/etc/varnish/secret'),0,-1);

Set non-zero value on admin/config/development/performance for "Expiration of cached pages"

Possible issues

Be awarded that Varnish caching doesn’t work:

  • on pages with messages
  • if session or other cookies have being set
  • if zero time for "Expiration of cached pages".

Is it working?

To check if Varnish caching works as expected please take a look on Headers:

String ‘X-Varnish-Cache: HIT’ means that caching works.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.