Giter Club home page Giter Club logo

varnish-examples's Introduction

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

varnish-examples's People

Contributors

commenthol avatar

Stargazers

olutola olajide michael avatar Rohit Kalkur avatar yayoc avatar Ewout van Mansom avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

rovolution

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.