Configure Varnish and your web server
The following sections discuss how to configure your web server and Magento to use Varnish:
- Configure your web server
- Modify the Varnish system configuration
- Modify
default.vcl
- Verify Varnish is working
- Install the Magento 2 software
- Verify HTTP response headers
Configure your web server
Configure your web server to listen on a port other than the default port 80 because Varnish responds directly to incoming HTTP requests, not the web server.
In the sections that follow, we use port 8080 as an example.
To change the Apache 2.2 listen port:
- Open
/etc/httpd/conf/httpd.conf
in a text editor. - Locate the
Listen
directive. - Change the value of the listen port to
8080
. (You can use any available listen port.) - Save your changes to
httpd.conf
and exit the text editor.
Modify the Varnish system configuration
To modify the Varnish system configuration:
- Open
/etc/sysconfig/varnish
in a text editor. -
Set the Varnish listen port to 80:
VARNISH_LISTEN_PORT=80
-
If necessary, comment out the following:
## Alternative 1, Minimal configuration, no VCL #DAEMON_OPTS="-a :6081 \ # -T localhost:6082 \ # -b localhost:8080 \ # -u varnish -g varnish \ # -s file,/var/lib/varnish/varnish_storage.bin,1G" ## Alternative 2, Configuration with VCL #DAEMON_OPTS="-a :6081 \ # -T localhost:6082 \ # -f /etc/varnish/default.vcl \ # -u varnish -g varnish \ # -S /etc/varnish/secret \ # -s file,/var/lib/varnish/varnish_storage.bin,1G"
- Save your changes to
/etc/sysconfig/varnish
and exit the text editor.
Modify default.vcl
This section discusses how to provide minimal configuration so Varnish returns HTTP response headers. This enables you to verify Varnish works before you configure Magento to use Varnish.
To minimally configure Varnish:
-
Back up
default.vcl
:cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak
- Open
/etc/varnish/default.vcl
in a text editor. -
Locate the following stanza:
backend default { .host = "127.0.0.1"; .port = "80"; }
-
Replace the value of
.host
with the fully qualified host name or IP address and listen port of the Varnish backend or origin server; that is, the server providing the content Varnish will accelerate.Typically, this is your web server.
-
Replace the value of
.port
with the web server鈥檚 listen port (8080 in this example).Example: Apache is installed on host 192.0.2.55 and Apache is listening on port 8080:
backend default { .host = "192.0.2.55"; .port = "8080"; }
If Varnish and Apache are running on the same host, we recommend you use an IP address or host name and not
localhost
. -
Save your changes to
default.vcl
and exit the text editor. -
Restart Varnish:
service varnish restart
If Varnish fails to start, try running it from the command line as follows:
varnishd -d -f /etc/varnish/default.vcl
This should display error messages.
If Varnish does not start as a service, you must configure SELinux rules to allow it to run. Consult the following resources:
Verify Varnish is working
The following sections discuss how you can verify that Varnish is working but without configuring Magento to use it. You should try this before you configure Magento.
Perform the tasks discussed in the following sections in the order shown:
Start Varnish
Enter service varnish start
If Varnish fails to start as a service, start it from the command line as follows:
-
Start the Varnish CLI:
varnishd -d -f /etc/varnish/default.vcl
-
Start the Varnish child process:
When prompted, enter
start
The following messages display to confirm a successful start:
child (29805) Started 200 0 Child (29805) said Child (29805) said Child starts
netstat
Log in to the Varnish server and enter the following command:
netstat -tulpn
Look for the following output in particular:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32614/varnishd
tcp 0 0 127.0.0.1:58484 0.0.0.0:* LISTEN 32604/varnishd
tcp 0 0 :::8080 :::* LISTEN 26822/httpd
tcp 0 0 ::1:48509 :::* LISTEN 32604/varnishd
The preceding shows Varnish running on port 80 and Apache running on port 8080.
If you don鈥檛 see output for varnishd
, make sure Varnish is running.
More information about netstat options
Install the Magento 2 software
Install the Magento 2 software if you haven鈥檛 already done so. When prompted for a Base URL, use the Varnish host and port 80 (for Varnish) because Varnish receives all incoming HTTP requests.
Possible error installing Magento:
Error 503 Service Unavailable
Service Unavailable
XID: 303394517
Varnish cache server
If you experience this error, edit default.vcl
and add a timeout to the backend
stanza as follows:
backend default {
.host = "127.0.0.1";
.port = "8080";
.first_byte_timeout = 600s;
}
Verify HTTP response headers
Now you can verify that Varnish is serving pages by looking at HTML response headers returned from any Magento page.
Before you can look at headers, you must set Magento for developer mode. There are several ways to do it, the simplest of which is to modify .htaccess
in the Magento 2 root. You can also use the magento deploy:mode:set
command.
Set Magento for developer mode
To set Magento for developer mode using its .htaccess
file:
- Log in to the Magento server as, or switch to, the Magento file system owner.
- Open
<your Magento install dir>/.htaccess
in a text editor. -
Uncomment the following line:
SetEnv MAGE_MODE developer
- Save your changes to
.htaccess
and exit the text editor.
Look at the Varnish log
Make sure Varnish is running then enter the following command on the Varnish server:
varnishlog
In a web browser, go to any Magento 2 page.
A long list of response headers display in your command prompt window. Look for headers like the following:
- BereqHeader X-Varnish: 3
- VCL_call BACKEND_FETCH
- VCL_return fetch
- BackendOpen 17 default(10.249.151.10,,8080) 10.249.151.10 60914
- Backend 17 default default(10.249.151.10,,8080)
- Timestamp Bereq: 1440449534.261791 0.000618 0.000618
- ReqHeader Host: 10.249.151.10
- ReqHeader Connection: keep-alive
- ReqHeader Content-Length: 86
- ReqHeader Cache-Control: max-age=0
- ReqHeader Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
- ReqHeader Origin: http://10.249.151.10
If headers like these do not display, stop Varnish, check your default.vcl
, and try again.
Look at HTML response headers
There are several ways to look at response headers, including using a browser plug-in like Live HTTP Headers (Firefox, Chrome), or a browser inspector.
The following example uses curl
. You can enter this command from any machine that can access the Magento server using HTTP.
curl -I -v --location-trusted '<your Magento base URL>'
For example,
curl -I -v --location-trusted 'http://192.0.2.55/magento2'
Look for headers like the following:
Content-Type: text/html; charset=iso-8859-1
X-Varnish: 15
Age: 0
Via: 1.1 varnish-v4
X-Magento-Cache-Debug: HIT
Find us on