Configuring Varnish for Drupal·续

Customizations#

The VCL file for Drupal contains two sections that might require some customization:

  • The backend definition
  • The access control list (ACL)

Here’s the backend definition:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

This backend definition makes Varnish connect to 127.0.0.1 on port 8080 when content needs to be fetched. Assuming that Varnish is hosted on the same server as your Drupal CMS, this value can be left unchanged.

If you’re hosting Drupal on another server or on another port, you’ll need to modify the .host and .port values.

The current values inside the purge access control list (ACL) all refer to the local machine, as you can see in the snippet below:

acl purge {
    "localhost";
    "127.0.0.1";
    "0.0.0.0";
    "::1";
}

Again, assuming that Drupal and Varnish are hosted on the same machine, these values can be left untouched. If your Drupal site is hosted on another machine, the right IP address needs to be added to the list.

This access control list is used to restrict access to the cache invalidation capabilities of the VCL file. If no ACL were in place, anyone would be able to remove items from cache, which is a security risk.

If invalidations happen from external locations, the IP addresses, the IP ranges, or the hostnames of these locations have to be added to the ACL.

6. Restart the services#

If you’re using Apache as a web server, you’ll run the following command to restart it:

sudo systemctl restart apache2

If you’re using Nginx instead, please run the following command to restart your web server:

sudo systemctl restart nginx

And finally, you’ll have to run the following command to restart Varnish:

sudo systemctl restart varnish

After the restart, your web server will accept traffic on port 8080, Varnish will handle HTTP traffic on port 80. The restart will also ensure the right VCL file is loaded, which will ensure that requests for your Drupal CMS can be properly cached.

7. Flushing the cache#

In the final step we will flush all Drupal caches to ensure a consistent state:

drush cr
drush p:invalidate everything