Detailed tutorial on using Drupal7 with Varnish and integrating the drupal module

Source: Internet
Author: User
Tags anonymous character set centos create database drupal drupal cache port number varnish

Prepare the environment

First, install a brand new Drupal. We recommend that you use drush to install Drupal quickly.

First, create a database and remember the user and password.

### Enter mysql
Mysql-uroot-p
Create database 'mydb' character set utf8 COLLATE utf8_general_ci;
Grant all on 'mydb'. * TO 'username' @ localhost identified by 'password ';
#### Exit mysql

Install Drupal7 using Drush:

Drush dl drupal-7.x; # -- select is used to select a version
Drush site-install standard -- account-name = admin -- account-pass = admin -- db-url = mysql: // YourMySQLUser: RandomPassword @ localhost/YourMySQLDatabase

The latest version of drupal7 is installed by default. To select a version, add the parameter-select.

After the installation is successful, download another varnish module.

# Go to the drupal Directory
Drush dl varnish

Install Varnish

The default CentOS is used as an example. Because the Varnish version in the CentOS repository is earlier, you need to import a new repo. Then, upgrade the yum software library.
For more information, see this link: https://www.varnish-cache.org/installation/redhat

#### Varnish 4.0:
Rpm -- nosignature-I https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
Yum install varnish
 
#### Varnish 3.0:
 
### RHEL 5 or a compatible distribution, use:
 
Rpm -- nosignature-I https://repo.varnish-cache.org/redhat/varnish-3.0.el5.rpm
Yum install varnish
 
### RHEL 6 and compatible distributions, use:
 
Rpm -- nosignature-I https://repo.varnish-cache.org/redhat/varnish-3.0.el6.rpm
Yum install varnish
 
# If the installed version is incorrect, update it.
# Yum update

Run the following command to check whether the installation is successful:

$ Usr/sbin/varnishd-V
Varnishd (varnish-3.0.5 revision 1a89b1f)
Copyright (c) 2006 Verdens Gang
Copyright (c) 2006-2011 Varnish Software

Set VCL of Varnish

Copy the following code to/etc/varnish/default. vcl:

The code is as follows: Copy code
Backend default {
. Host = "127.0.0.1 ";
. Port = "80 ";
}
 
Sub vcl_recv {
If (req. restarts = 0 ){
If (req. http. x-forwarded-){
Set req. http. X-Forwarded-For = req. http. X-Forwarded-For + "," + client. ip;
    }
Else {
Set req. http. X-Forwarded-For = client. ip;
    }
  }
 
# Do not cache these paths.
If (req. url ~ "^/Status. php $" |
Req. url ~ "^/Update. php $" |
Req. url ~ "^/Ooyala/ping $" |
Req. url ~ "^/Admin/build/features" |
Req. url ~ "^/Info/. $" |
Req. url ~ "^/Flag/. $" |
Req. url ~ "^./Ajax/. $" |
Req. url ~ "^./Ahah/. $ "){
Return (pass );
  }
 
# Pipe these paths directly to Apache for streaming.
If (req. url ~ "^/Admin/content/backup_migrate/export "){
Return (pipe );
  }
 
# Allow the backend to serve up stale content if it is responding slowly.
Set req. grace = 6 h;
 
# Use anonymous, cached pages if all backends are down.
If (! Req. backend. healthy ){
Unset req. http. Cookie;
  }
 
# Always cache the following file types for all users.
If (req. url ~ "(? I). (png | gif | jpeg | jpg | ico | swf | css | js | html | htm )(? [Wd =.-] + )? $ "){
Unset req. http. Cookie;
  }
 
# Remove all cookies that Drupal doesn' t need to know about. ANY remaining
# Cookie will cause the request to pass-through to Apache. For the most part
# We always set the NO_CACHE cookie after any POST request, disabling
# Varnish cache temporarily. The session cookie allows all authenticated users
# To pass through as long as they're logged in.
If (req. http. Cookie ){
Set req. http. Cookie = ";" + req. http. Cookie;
Set req. http. Cookie = regsuball (req. http. Cookie, "; + ",";");
Set req. http. Cookie = regsuball (req. http. Cookie, "; (SESS [a-z0-9] + | NO_CACHE) =", "; 1 = ");
Set req. http. Cookie = regsuball (req. http. Cookie, "; [^] [^;] *", "");
Set req. http. Cookie = regsuball (req. http. Cookie, "^ [;] + | [;] + $ ","");
 
# Remove the "has_js" cookie
Set req. http. Cookie = regsuball (req. http. Cookie, "has_js = [^;] + (;)? ","");
# Remove the "Drupal. toolbar. collapsed" cookie
Set req. http. Cookie = regsuball (req. http. Cookie, "Drupal. toolbar. collapsed = [^;] + (;)? ","");
# Remove AdminToolbar cookie for drupal6
Set req. http. Cookie = regsuball (req. http. Cookie, "DrupalAdminToolbar = [^;] + (;)? ","");
# Remove any Google Analytics based cookies
Set req. http. Cookie = regsuball (req. http. Cookie, "_ utm. = [^;] + (;)? ","");
# Remove the Quant Capital cookies (added by some plugin, all _ qca)
Set req. http. Cookie = regsuball (req. http. Cookie, "_ qc. = [^;] + (;)? ","");
 
If (req. http. Cookie = ""){
# If there are no remaining cookies, remove the cookie header. If there
# Aren't any cookie headers, Varnish's default behavior will be to cache
# The page.
Unset req. http. Cookie;
Set req. http. X-Varnish-NoCookie = "TRUE ";
    }
Else {
# If there is any cookies left (a session or NO_CACHE cookie), do not
# Cache the page. Pass it on to Apache directly.
Set req. http. X-Varnish-NoCookie = "FALSE ";
Set req. http. X-Varnish-CookieData = req. http. Cookie;
Return (pass );
    }
  }
 
# Handle compression correctly. Different browsers send different
# "Accept-Encoding" headers, even though they mostly all support the same
# Compression mechanisms. By using lidating these compression headers
# A consistent format, we can reduce the size of the cache and get more .
# @ See: http: // varnish. projects. linpro. no/wiki/FAQ/Compression
If (req. http. Accept-Encoding ){
If (req. http. Accept-Encoding ~ "Gzip "){
# If the browser supports it, we'll use gzip.
Set req. http. Accept-Encoding = "gzip ";
    }
Else if (req. http. Accept-Encoding ~ "Deflate "){
# Next, try deflate if it is supported.
Set req. http. Accept-Encoding = "deflate ";
    }
Else {
# Unknown algorithm. Remove it and send unencoded.
Unset req. http. Accept-Encoding;
    }
  }
 
If (req. request! = "GET "&&
Req. request! = "HEAD "&&
Req. request! = "PUT "&&
Req. request! = "POST "&&
Req. request! = "TRACE "&&
Req. request! = "OPTIONS "&&
Req. request! = "DELETE "){
/* Non-RFC2616 or CONNECT which is weird .*/
Return (pipe );
  }
If (req. request! = "GET" & req. request! = "HEAD "){
/* We only deal with GET and HEAD by default */
Return (pass );
  }
# Unset Authorization header if it has the correct details...
# If (req. http. Authorization = "Basic # Unset req. http. Authorization;
#}
If (req. http. Authorization | req. http. Cookie ){
/* Not cacheable by default */
Return (pass );
  }
 
Return (lookup );
}
 
 
Sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want
# Have it set for all requests, make sure to have:
Set bereq. http. connection = "close ";
# Here. It is not set by default as it might break some broken web
# Applications, like IIS with NTLM authentication.
}
 
# Routine used to determine the cache key if storing/retrieving a cached page.
Sub vcl_hash {
If (req. http. X-Forwarded-Proto = "https "){
Hash_data (req. http. X-Forwarded-Proto );
  }
}
 
Sub vcl_ {
If (req. request = "PURGE "){
Purge;
Error 200 "Purged .";
  }
}
 
Sub vcl_miss {
If (req. request = "PURGE "){
Purge;
Error 200 "Purged .";
  }
}
 
# Code determining what to do when serving items from the Apache servers.
Sub vcl_fetch {
Set beresp. http. X-Varnish-CookieData = beresp. http. set-cookie;
# Don't allow static files to set cookies.
If (req. url ~ "(? I). (png | gif | jpeg | jpg | ico | swf | css | js )(? [A-z0-9] + )? $ "){
# Beresp = Back-end response from the web server.
Unset beresp. http. set-cookie;
  }
Else if (beresp. http. Cache-Control ){
Unset beresp. http. Expires;
  }
 
If (beresp. status = 301 ){
Set beresp. ttl = 1 h;
Return (deliver );
  }
 
# Doesn' t seem to work as expected
# If (beresp. status = 500 ){
# Set beresp. saintmode = 10 s;
# Return (restart );
#}
 
# Allow items to be stale if needed.
Set beresp. grace = 1 h;
}
 
# Set a header to track a cache /MISS.
Sub vcl_deliver {
If (obj. > 0 ){
Set resp. http. X-Varnish-Cache = "";
  }
Else {
Set resp. http. X-Varnish-Cache = "MISS ";
  }
}
 
# In the event of an error, show friendlier messages.
Sub vcl_error {
Set obj. http. Content-Type = "text/html; charset = utf-8 ";
Set obj. http. Retry-After = "5 ";
Synthetic {"
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<Html>
<Head>
<Title> "} + obj. status +" "+ obj. response + {" </title>
</Head>
<Body>
<H1> Error "} + obj. status +" "+ obj. response + {" <P> "} + obj. response + {" </p>
<H3> Guru Meditation: <P> XID: "}+ req. xid + {" </p>
<Hr>
<P> Varnish cache server </p>
</Body>
</Html>
"};
Return (deliver );
}



After the configuration is complete, start Varnish.

Test results

Open the browser, enter the URL of druapl7, and check whether Drupal7 is normal, and then add the port number.
For example, our test address is: http://drupal7.111cn.net
Then use the browser to open varnish address, as follows: http://drupal7.111cn.net: 6081/

Test results. If both sides are normal, both drupal and varnish are normal.
Cache Drupal pages with Varnish

Use Firebug to view varnish requests. If you see the X-Varnish mark in the http header, it indicates that varnish has taken effect. In this case, we need to determine whether varnish has cached the page.
How to judge: X-Varnish is followed by a number indicating that it is not a cache. X-Varnish is followed by two numbers, indicating that the cache is successful.

Here, we will find that all varnish does not the cache, so the problem is... (The excavator will not appear)
How to make varnish cache take effect:

// Tell Drupal it's behind a proxy.
$ Conf ['reverse _ proxy'] = TRUE;
 
// Tell Drupal what addresses the proxy server (s) use.
$ Conf ['reverse _ proxy_addresses '] = array ('2017. 0.0.1 ');
 
// Bypass Drupal bootstrap for anonymous users so that Drupal sets max-age & lt; 0.
$ Conf ['page _ cache_invoke_hooks '] = FALSE;
 
// Make sure that page cache is enabled.
$ Conf ['cache'] = 1;
$ Conf ['cache _ lifetime'] = 0;
$ Conf ['page _ cache_maximum_age '] = 21600;

Add the following content to the settings. php of Drupal, and refresh the browser. Then you can see that the number of X-Varnish is changed to two (multiple clicks ).

So far, Varnish can completely cache Drupal pages. As shown in the following figure:



So what is the Varnish module of Drupal used?

Simply put, Varnish clears varnish cache through the Drupal cache interface, such as page expiration.
In addition, through the Expire module, you can precisely control those pages and control the Expiration Time, which is more convenient.

Configure the Varnish module of Drupal.org

Start the Varnish module, read the official description of the varnish module: https://www.drupal.org/project/varnish
Add the following two lines to the settings. php of Drupal:

// Add Varnish as the page cache handler.
$ Conf ['cache _ backends '] = array ('sites/all/modules/varnish. cache. Inc ');
$ Conf ['cache _ class_cache_page '] = 'varnishcache ';

Go to the Drupal settings page. The path is admin/config/development/varnish.

For example:
Varnish Control Terminal: 127.0.0.1: 6082
Varnish Control Key: 86b2d660-9768-4a13-ab90-4b0736d6a4d1
Click Save to change status to green.
(Note: Copy the value of the Control Key to the/etc/varnish/secret file)

After the setting is complete, clear the Drupal cache and you will find that the cache value in Varnish will be refreshed, achieving the goal of clearing the cache instantly.

++ To this complete ++ (more questions to drupal University: http://drupal001.net ask oh !) ++

Oh, another case is that Varnish is installed on another server, because Varnish controls the background to listen to the local machine by default. Therefore, if you want to refresh the cache of another reverse proxy server, you must modify the configuration. There are two optional methods,

First, let the Varnish Management backend address use the intranet IP address (such as 192.168.1.x). Generally, this architecture is an intranet cluster, so it is reasonable to listen to the intranet.
Second, the local machine uses autossh to map the local port 6082 to the intranet machine.

$ Autossh-fN-L 6082: localhost: 6082

Complete

So far, Drupal7 + Varnish configuration has been completed, and Drupal and varnish are also fully integrated. Varnish cache is always faster than other caches, so it can replace Boost cache.
If you want to use Varnish to cache dynamic content and have more content to do, this article will no longer increase your reading volume (^_^ ).

++

Finally, let's talk about Apache's RPAF Module.

Varnish uses X-Forwarded-For as the address information of the remote IP by default, but this is not a standard protocol, sometimes we still use $ _ SERVER ['remote _ ADDR '] in PHP to obtain the IP address.
Apache has a module. The configuration file RPAF. conf is as follows, and the correct IP address can be set. (Not much to mention)

RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 192.168. 10.0.0.
RPAFheader X-Forwarded-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.