Install Varnish & Configure to work with NGINX

Configure the VCL

VCL is the Varnish Configuration Language. The VCL file holds most of the config.

Shell
sudo vi /etc/varnish/default.vcl 

Tell Varnish to communicate with the content server (Nginx) on port 8080.

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

With the above config, Varnish will only cache files without cookies. The following config strips all cookies and caches everything. The ideal config is somewhere in between.

PLAINTEXT
sub vcl_recv {
    unset req.http.cookie;
}
sub vcl_fetch {
    unset beresp.http.set-cookie;
}

Configure the Daemon

The Varnish daemon file sets ports, hosts, storage method, etc.

Shell
sudo vi /etc/default/varnish 

Tell Varnish to listen on port 80.

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

Configure Nginx Hosting

Varnish sits in front of Nginx on port 80 and talks to Nginx on 8080. Nginx defaults to 80, so make sure every Nginx server config listens on 8080. If not, Nginx will intercept Varnish.

Shell
sudo vi /etc/nginx/sites-available/default
sudo vi /etc/nginx/sites-available/example.com

Add the listen directive to the default Nginx server config.

NGINX
server {
    listen 8080;
    ...
}

Do the same for the example.com virtual host config.

NGINX
index index.php;
server {
    listen 8080;
    server_name www.example.com;
    ...
}
server {
    listen 8080;
    server_name example.com;
    ...
}

Restart Services 

After restart, inspect the page headers for Via:1.1 varnish. The 1.1 refers to HTTP 1.1.

Shell
sudo service nginx restart
sudo service php5-fpm restart
sudo service varnish restart

Troubleshooting

If you see an Nginx error when you visit a page, or if page headers don't mention Varnish, it's likely Nginx is still listening on port 80. Turn off Varnish and use netstat to check ports.

Shell
sudo /etc/init.d/varnish stop
netstat -an | grep LISTEN

If you see anything on port 80, make sure all Nginx virtual hosts are listening on port 8080.

Was this worth your time?