Date Posted:12-02-2017

In this article, we are gonna explain on how to setup varnish to listen on port 80(http) and forward request to backend webserver(our example, we deals with nginx as backend server). The backend server can be anything, either Apache or Nginx webserver.

Assumption:
  1. Apache or nginx webserver. This post also deals with how to change the webserver port 80 on webserver.
  2. Ubuntu 16

Incase, if there is no webserver installed, refer the following post on installing webserver.

Install apache, php5.6 ubuntu

Install Nginx, Php, MariaDB on Ubuntu 16.04.1

Implementation:

Login to the server as root. As updated earlier, this post assumes that nginx is installed on the webserver.

Add the key to the server.

curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

Add the varnish repo to the server.

echo "deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0" >>/etc/apt/sources.list

Run the update

apt-get update

Install varnish on the server.

apt-get install varnish

Open the file /etc/default/varnish  and edit the following files.

nano /etc/default/varnish

DAEMON_OPTS="-a :6081 \

to

DAEMON_OPTS=”-a :80 \

Edit the file /etc/systemd/system/multi-user.target.wants/varnish.service  to use port from 6081 to 80.

nano /etc/systemd/system/multi-user.target.wants/varnish.service

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

We can optionally edit the memory allocated to varnish. Incase of dedicated server, increase the memory depends on the RAM available.

Open the file /etc/varnish/default.vcl  and add/edit/modify the file.

nano /etc/varnish/default.vcl

backend default {
.host = “127.0.0.1”;
.port = “8080”;
}

Edit the port on the nginx default configuration. Incase, if there are multiple domains then we may need to edit all the domain virtualhost. If the webserver is apache then edit the Listen section to use 8080 port(default installation location is /etc/apache2/apache2.conf). In our example, we are using nginx and default configuration is /etc/nginx/sites-available/default.

nano /etc/nginx/sites-available/default

From

listen 80 default_server;
listen [::]:80 default_server;

To,

listen 127.0.0.1:8080 default_server;
listen [::]:8080 default_server;

Restart the services to make the changes into effect.

systemctl restart nginx

sytemctl restart varnish

Verification:
To verify whether varnish is working on port 80 and nginx on port 8080 use the following commands,

netstat -plan |grep -w 80|grep varnish

netstat -plan |grep -w 8080|grep nginx

TO verify from outside of the server, use curl command to get the result.

 curl -I IPaddress

Output of the command is mentioned which confirms that varnish implemented over nginx.

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

 

Leave a Reply