Blog

Installation and Configuring of Varnish Reverse Proxy with Nginx

Tags: Nginx

Published on: September 2, 2019 by Sheldon Moor

Installation and Configuring of Varnish Reverse Proxy with Nginx

Scenario:

Varnish is a proxy server which can be used for HTTP caching. It can act as a reverse proxy for nginx. Here we are going to look how we can configure varnish as a reverse proxy for nginx web server.

Prerequisites

Ubuntu 16.04

Root Privileges

Step 1: Installing nginx

We can install Nginx from the Ubuntu repository using the apt command

root@server:~# apt install nginx -y

After the installation is complete, start Nginx and enable it to launch every time at system boot using the systemctl commands below.

root@server:~#systemctl start nginx
root@server:~#systemctl enable nginx

Step 2: Configuring Nginx

We will configure nginx to run under non-standard HTTP port 8080. For this purpose, we need to edit virtual host files under ‘sites-available’ directory and change the ‘listen’ line value to 8080.

root@server:~#vi /etc/nginx/sites-available/default

------------------------------

listen 8080 default_server;

listen [::]:8080 default_server;

-------------------------------

Now test the nginx server using the command “nginx -t” and restart the service.

root@server:~# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

root@server:~#systemctl restart nginx

Step 3: Installing Varnish

Installing varnish from the Ubuntu repository using apt command. After installing start and enable it to launch at system boot.

apt install varnish -y

root@server:~#systemctl start varnish

root@server:~#systemctl enable varnish

Step 4 – Configuring Varnish as a Reverse Proxy for Nginx

Here we are using Varnish as a reverse proxy for the Nginx web server. So Varnish will be running on the HTTP port 80, and the Nginx web server on HTTP port 8080.

Go to the varnish configuration directory and edit the ‘default.vcl’ file.

root@server:~#vi /etc/varnish/default.vcl

On the backend line, define the configuration as below.

backend default {

.host = "127.0.0.1";

.port = "8080";

}

After that edit the Varnish configuration file and on the ‘DAEMON_OPTS’ line, change the default port 6081 to HTTP port 80.

root@server:~#vi /etc/default/varnish

-----------------------------------------

DAEMON_OPTS="-a :80 \

-T localhost:6082 \

-f /etc/varnish/default.vcl \

-S /etc/varnish/secret \

-s malloc,256m"

-------------------------------------------

Now go to the systemd system directory and edit the varnish.service file.

vi /lib/systemd/system/varnish.service

Step 5: Reload the systemd configuration and restart varnish.

root@server:~#systemctl daemon-reload

root@server:~#systemctl restart varnish

Step 6 : Configuring UFW Firewall

In this step, we will activate the firewall and open new ports for SSH, HTTP, and HTTPS.

root@server:# ufw allow ssh

root@server:# ufw allow http

root@server:# ufw allow https

Step 7: Testing

Test varnish using the curl command, so we can see HTTP headers from the server.

root@server:~# curl -I http://136.243.201.230
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Thu, 21 Mar 2019 09:58:33 GMT
Content-Type: text/html
Last-Modified: Mon, 04 Mar 2019 10:36:49 GMT
ETag: W/"5c7cffc1-2a"
Vary: Accept-Encoding
X-Varnish: 5 3
Age: 7
Via: 1.1 varnish-v4
Accept-Ranges: bytes
Connection: keep-alive

Thank You for reading!!

Server Management

Category : Sever management

Sheldon Moor

Sheldon Moor

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Get new updates

Categories

$0.000 items