Varnish

Learn Varnish on Arch Linux, Nginx and Django

What is it?

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents.

https://www.varnish-cache.org/

Setup

Install

Generally speaking, you should restart nginx then varnish in this order when making changes.

Set port to 8080 in nginx config

Set the backend port to 8080 in VCL config. It’s 8080 by default

Architecture

All incoming requests will hit Varnish first. If it is unable to serve up a cached version, it will use the backend specified (you can define multiple backends). By default, we use nginx so nginx gets hit which in turn calls uwsgi.

Varnish Gotcha

  1. By default (and design) Varnish does not cache content with cookies in it. So you have to exclude/remove cookies in the VCL config file to bypass this.
  2. Varnish also does not deal with HTTPS, because encryption makes it impossible for Varnish to distinguish what the objects are that it is passing back and forth from the user to the backend. There are workarounds (like installing Pound) in front of Varnish and terminating SSL/TLS there.

https://www.varnish-cache.org/trac/wiki/VCLExampleCacheCookies

Django Gotcha

http://stackoverflow.com/questions/12034242/is-varnish-compatible-with-django-csrf-protection

You also have to remove the sssionid and csrftoken set in the cookies. The VCL file below shows you how

VCL file

The VCL file is the place where you control the rules.

Some basic settings

The service file in /etc/systemd/system/varnish.service be defaults listens on port 80. To change the allocated ram, change 64M to whatever you wish.

Commands

To check config file. If everything is ok, there will be a printout

To ban a particular page (clear the cache for the page)

Is Varnish Working?

or simply

http://www.isvarnishworking.com/

Benchmarks (Exciting!)

What you don’t see here is the CPU utilization. Nginx uses a little bit more but not significantly.

Project Versions

Table Of Contents

Previous topic

Welcome to Learn Varnish’s documentation!

This Page