Reverse proxy using Varnish

Reverse proxy using Varnish

  • Save

Reverse proxy

A Reverse proxy is a server that sits in front of web servers and forwards client (e.g. web browser) requests to those web servers. 

  • Save

Reverse proxy software for HTTP

  • Nginx
  • HAProxy
  • Varnish Cache
  • Lighttpd
  • Repose

Reverse proxies are typically implemented to help increase Security, Performance, and Reliability. We are already most familiar with Nginx, so will try Varnish Cache as a reverse proxy.

Varnish Cache

Varnish is a program that can increase the speed of a Web site while simultaneously reducing the load on the Web server. “Varnish is a Web application accelerator also known as a caching HTTP reverse proxy”.

It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture.

How varnish works?

The first time a certain URL and path are requested, Varnish has to request it from the origin server in order to serve it to the visitor. This is called a CACHE MISS, which can be read in HTTP response headers, depending on the Varnish setup.

  • Save

When a particular URL or a resource is cached by Varnish and stored in memory, it can be served directly from server RAM; it doesn’t need to be computed every time. Varnish will start delivering a CACHE HIT in a matter of microseconds.

  • Save

Varnish vs Ngnix

If you are using Nginx and Varnish only as reverse proxy, it’s fair to compare both.

  • Both Nginx and Varnish can be used as a reverse proxy cache, also for load balancing between two or more Apache servers that will deliver the dynamic content.
  • Varnish Cache has a lot of flexibility, allowing developers to create a more complex caching structure than Nginx.
  • Varnish Cache Configuration Language (VCL). VCL allows developers to specify request handling rules and set specific caching policies giving them a lot of control over what and how they cache.
  • Varnish Cache supports ESI while Nginx doesn’t; Nginx supports SSL where Varnish Cache doesn’t.
  • Varnish by default supports PURGE.

How to install Varnish in MacOS

With the help of brew we can install Varnish cache.

  • Open your terminal then run,
    • > brew install varnish.
  • Check varnish is installed your machine by running this command,
    • > brew info varnish.
  • Save

Varnish has a great configuration system. Most other systems use configuration directives, where you basically turn on and off lots of switches. We have instead chosen to use a domain specific language called VCL for this.

Varnish is configured via Varnish Configuration Language (VCL). Once the configuration file is loaded by the system, Varnish translates and compiles.

when you install varnish, default configuration file will be available called default.vcl file.

In the above image you can able to locate default.vcl file. 

Run the following command to view,

> cat /usr/local/etc/varnish/default.vcl

  • Save

Setting up Varnish with NodeJS

where we had already installed Varnish, setting up a hello-world Node app. 

  • Save
  • Save file as server.js
  • Open terminal and start your NodeJS server,

> node server.js

  • Goto browser and open localhost:8080.
  • Check your server is running properly.

Configure Varnish

  • Open your default.vcl file.
  • Setup your backend configuration.
  • Save
  • Handle request methods, by default varnish supports GET and HEAD method.
  • Handle backend response, once varnish fetch content from backend we can set ttl(time to live) and other configurations like handling response code.
  • Save
  •        We can control whether or not our request is being cached in our browser inspector, we ought to add the following snippet to our Varnish config file, into the sub vcl_deliver.
  • Save
  • Start your varnish server.
  • Goto your browser, then we can see the feedback in our response headers as HIT or MISS.
  • Save

This was just a short tutorial on speeding up your web service using Varnish.You can use Varnish with any backend server like Python, PHP, NodeJS.. Etc. In built VCL makes life easy. Based on our needs we can stick with Nginx or Varnish to boost our site performance.


Ashokkumar Palanisamy
  • Save

Choose to be Optimistic


Leave a Reply

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

Copy link
Powered by Social Snap