Nginx + Varnish

Install Varnish & Configure to work with NGINX

Configure the VCL

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

sudo vi /etc/varnish/default.vcl [adsenseyu2]

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

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.

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.

sudo vi /etc/default/varnish [adsenseyu2]

Tell Varnish to listen on port 80.

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.

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.

server {
    listen 8080;
    ...
}

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

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

Restart Services [adsenseyu2]

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

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *