Skip to content

rovolution/varnish-examples

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Varnish Examples

A collection of varnish examples

Table of Contents

Prerequisites

conf contains the examples for Varnish 4.0; conf3 contains the samples for Varnish 3.0

Examples

Alive Page

Check if varnish is up and running (e.g. if using with monit)

Source: alive.vcl

# start the varnish webcache locally
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/alive.vcl

curl -v http://localhost:8000/alive

Switch to a different backend per URL

Use varnish to change to a different backend per URL.

Source: other.vcl

# start a http backend server
node src/backend.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/other.vcl

curl -v http://localhost:8000/
curl -v http://localhost:8000/other/

# Wait some seconds - Age shall be greater than "0"
curl -v http://localhost:8000/
< Age: 5
3000:/ GET

curl -v http://localhost:8000/other/
< Age: 6
4000:/other/ GET

# POST requests shall be passed - Age shall always be "0"
curl -v http://localhost:8000/ -X POST
< Age: 0
3000:/ POST

curl -v http://localhost:8000/other/ -X POST
< Age: 0
4000:/other/ POST

Listen to another port and redirect the request to the same server

Source: secondport.vcl

node src/backend.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -a 127.0.0.1:8001 -f conf/secondport.vcl

# repeat the requests after some seconds to see that page gets cached
curl -v http://localhost:8000/
< Age: 5
3000:/ GET

curl -v http://localhost:8001/
< Age: 5
3000:/ GET

# do not cache POST requests
curl -v http://localhost:8001/ -X POST
< Age: 0
3000:/ POST

Remove Response Headers

For security reasons hide the servers default response headers

Source: respheaders.vcl

node src/backend.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/respheaders.vcl

curl -v http://localhost:8000/

Whitelist cookies

Remove unwanted cookies from a request to improve caching

Source: cookiewhitelist.vcl

See also here.

node src/backend.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/cookiewhitelist.vcl

curl -v -b "pass=1; delete=1" http://localhost:8000
< Age: 0
cookie: pass=1
3000:/ GET

Do not cache 30x Redirects

Source: nocache30x.vcl

node src/backend.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/nocache30x.vcl

curl -v http://localhost:8000/301
< Location: /
< Age: 0

curl -v http://localhost:8000/302
< Location: /
< Age: 0

Using restart if content not found at backend

This recipe is useful to normalize URLs if content is distributed over various backends (e.g. SEO) or to serve a fallback content on 404 responses.

See VCLExampleRestarts

Flow

Source: restart404.vcl

node src/backend.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/restart404.vcl

curl -v http://localhost:8000/404/
<
4000:/other GET undefined

Device detection with restart

This example demonstrates how to use a device-detection service with varnish. It uses the restart mechanism to both cache the device detection response as well as the device specific page.

The device-detection service here responds with a "x-ua-device" HTTP-Header which contains "mobile", "tablet" or "other" as device type. This response than gets cached and restart issues the original request to the backend containing the "x-ua-device" HTTP-Header. With this a device specific page is being delivered by the "backend" and cached as well.

Flow

Source: devicedetect.vcl

# start the sample device-detection service (this starts the backend as well)
node src/devicedetect.js &
varnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/devicedetect.vcl

curl -v "http://localhost:8000" -A iphone
< x-ua-device: mobile
3000:/ GET mobile

curl -v "http://localhost:8000" -A android
< x-ua-device: tablet
3000:/ GET tablet

curl -v "http://localhost:8000"
< x-ua-device: other
3000:/ GET other

References

About

A collection of varnish examples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • VCL 78.4%
  • JavaScript 20.8%
  • Makefile 0.8%