Atlantic.Net Blog

Install and Configure Varnish Cache with Nginx Web Server on Ubuntu 18.04

Hitesh Jethva
by Atlantic.Net (52posts) under VPS Hosting
0 Comments

Varnish Cache is a free, open-source, and powerful web application accelerator designed for high-load dynamic websites. The Varnish Cache sits behind the web server and can speed up your website by a factor between about 300 and 1000 times. It can also be used as a load balancer if you are running multiple servers. Varnish Cache works by caching requested web pages in memory and serving a requested page without the delay of building content from scratch when the same information is asked for several times.

In this tutorial, we will show you how to set up Varnish Cache as a proxy server for Nginx on Ubuntu 18.04 VPS.

Prerequisites

  • A fresh Ubuntu 18.04 VPS on the Atlantic.Net Cloud Platform.
  • A root password set up on your server.

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 18.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Install Varnish Cache

By default, the Varnish package is available in the Ubuntu 18.04 default repository. You can install it by running the following command:

apt-get install varnish -y

After successful installation, the Varnish cache service has been started automatically. You can check the status of Varnish service with the following command:

systemctl status varnish

You should get the following output:

You can also verify the installed version of Varnish with the following command:

varnishd -V

You should see the following output:

By default, Varnish cache is listening on port 6081 and 6082. You can verify it with the following command:

netstat -ant

You should see the following output:

Step 3 – Install and Configure Nginx

In this section, we will install and configure Nginx to sit behind the Varnish cache server. By default, Nginx runs on port 80, so you will need to configure the Nginx to listen on port 8088.

First, install the Nginx web server with the following command:

apt-get install nginx -y

After installing the Nginx web server, edit the Nginx default virtual host configuration file:

nano /etc/nginx/sites-available/default

Change the default port from 80 to 8088, as shown below:

server {
        listen 8088 default_server;
        listen [::]:8088 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }

}

Note : Ensure you have deleted or commented out all of the default entries in the files, make sure only the above content is saved. Failure to do this will result in the nginx service refusing to start

Save and close the file when you are finished. Then, restart the Nginx service to apply the configuration:

systemctl restart nginx

Step 4 – Configure Varnish Cache

Next, you will need to configure Varnish to use port 80 so it can route traffic to the Nginx web server via the Varnish cache server.

You can do it by editing the file /lib/systemd/system/varnish.service:

nano /lib/systemd/system/varnish.service

Change the Varnish default port from 6081 to 80 as shown below:

[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd

[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
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
ExecReload=/usr/share/varnish/varnishreload
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true

[Install]
WantedBy=multi-user.target

Save and close the file when you are finished. Then, reload systemd daemon with the following command:

systemctl daemon-reload

Next, you will need to configure Nginx as a backend server for Varnish. You can do it by editing the file /etc/varnish/default.vcl:

nano /etc/varnish/default.vcl

Change the port from 8080 to 8088, as shown below:

backend default {
    .host = "127.0.0.1";
    .port = "8088";
}

Save and close the file when you are finished.

Note: Replace 127.0.0.1 with your Nginx server IP address if your Nginx web server is installed on the other host.

Finally, restart the Varnish cache server with the following command:

systemctl restart varnish

Step 5 – Verify Varnish Cache Server

At this point, the Varnish cache is configured to work with the Nginx web server. It’s time to test it.

You can check the Varnish cache with the curl command as shown below:

curl -I your-server-ip

You should get the following output:

HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 01 Dec 2019 06:46:18 GMT
Content-Type: text/html
Last-Modified: Sun, 01 Dec 2019 06:41:28 GMT
ETag: W/"5de36098-264"
Vary: Accept-Encoding
X-Varnish: 32770 3
Age: 17
Via: 1.1 varnish (Varnish/5.2)
Accept-Ranges: bytes
Connection: keep-alive

The above output clearly indicates that you are using Nginx server with Varnish Cache.

You can also verify Varnish caching statistics with the following command:

varnishstat

You should see the following screen:

You can also see Varnish log entry ranking with the following command:

varnishtop

Output:

If you have any issues with the Varnish cache, you can check the Varnish log with the following commands:

tail -f varnishncsa.log

Conclusion

Congratulations! You have successfully installed and configured the Varnish Cache server with Nginx on Ubuntu 18.04 VPS. You can refer to the Varnish documentation to optimize your configuration per your needs.

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Sales Today! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Case Studies

White Papers

Resources


HIPAA Partners


Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources