Install Varnish on Ubuntu 16
Varnish is a web application accelerator that can be used as a caching layer in front of a web server. In this setup, Varnish listens on port 80 and forwards requests to the backend web server. This guide explains how to configure Varnish with Nginx as the backend web server on Ubuntu 16.
Introduction
Varnish can be configured to listen on the default HTTP port 80 while the backend web server listens on another port. In this example, Varnish listens on port 80 and Nginx listens on port 8080.
The backend web server can be either Apache or Nginx. This guide uses Nginx as the backend web server.
Prerequisites
- Ubuntu 16 server
- Root access to the server
- Nginx or Apache web server installed
- Basic knowledge of Linux commands
- Sufficient RAM if additional memory is allocated to Varnish
Implementation
Step 1
Log in to the server as the root user.
Step 2
Add the Varnish repository key.
curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
Step 3
Add the Varnish repository to the server.
echo "deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0" >> /etc/apt/sources.list
Step 4
Update the package repository information.
apt-get update
Step 5
Install Varnish.
apt-get install varnish
Step 6
Open the Varnish configuration file.
nano /etc/default/varnish
Change the following configuration from:
DAEMON_OPTS="-a :6081 \
to:
DAEMON_OPTS="-a :80 \
This configures Varnish to listen on port 80.
Step 7
Edit the Varnish systemd service configuration.
nano /etc/systemd/system/multi-user.target.wants/varnish.service
Change the ExecStart configuration from:
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
to:
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Step 8
Varnish memory allocation can optionally be adjusted based on the available server RAM.
Open the Varnish configuration file:
nano /etc/varnish/default.vcl
Configure the backend as follows:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Step 9
Configure Nginx to listen on port 8080 instead of port 80.
Open the Nginx default configuration:
nano /etc/nginx/sites-available/default
Change:
listen 80 default_server; listen [::]:80 default_server;
to:
listen 127.0.0.1:8080 default_server; listen [::]:8080 default_server;
If multiple domains are configured, update the corresponding virtual host configurations.
If Apache is being used as the backend web server instead, configure Apache to listen on port 8080.
Step 10
Restart Nginx.
systemctl restart nginx
Step 11
Restart Varnish.
systemctl restart varnish
Verification
Step 1
Verify that Varnish is listening on port 80.
netstat -plan | grep -w 80 | grep varnish
Step 2
Verify that Nginx is listening on port 8080.
netstat -plan | grep -w 8080 | grep nginx
Step 3
To verify the configuration externally, use the following command:
curl -I IPaddress
A response containing X-Varnish and Via: 1.1 varnish-v4 confirms that the request is being handled through Varnish.
Example:
HTTP/1.1 301 Moved Permanently Server: nginx/1.10.0 (Ubuntu) Date: Sun, 12 Feb 2017 07:17:57 GMT Content-Type: text/html Content-Length: 194 Location: https://IPaddress/ X-Varnish: 529814 Age: 0 Via: 1.1 varnish-v4 Connection: keep-alive
Conclusion
Varnish is now configured to listen on port 80 and forward requests to the Nginx backend running on port 8080. The configuration can be verified by checking the listening ports and using the curl command to confirm that requests are passing through Varnish.
