DEV Community

Ashokkumar
Ashokkumar

Posted on

Reverse proxy using Varnish

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.

image

Some of the Reverse proxy

  • Nginx
  • HAProxy
  • Varnish-Cache
  • Lighttpd
  • Repose

Reverse proxies are typically implemented to help increase Security, Performance, and Reliability. Most of us are already familiar with Nginx, so will try Varnish Cache in this article.

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.

Alt Text

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.

Alt Text

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,

brew info varnish

Alt Text

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.

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

Setting up Varnish with NodeJS
where we had already installed Varnish, Now setting up a NodeJS app.

Alt Text

Save file as server.js

  • Open terminal and start your NodeJS server,

node server.js

Goto browser and open https://localhost:8080.

Configure Varnish

  • Open your default.vcl file.
  • Setup your server configuration.

Alt Text

  • 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.

Alt Text

  • 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.

Alt Text

  • Start your varnish server.
  • Goto your browser, then we can see the feedback in our response headers as HIT or MISS.

Alt Text

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.

In built VCL makes life easy. Based on our needs we can stick with Nginx or Varnish to boost our site performance.

Please find full version of default.vcl file Github

Video link youtube

Top comments (0)