{"id":19621379,"url":"https://github.com/commenthol/varnish-examples","last_synced_at":"2025-07-30T23:13:59.134Z","repository":{"id":66111699,"uuid":"44119573","full_name":"commenthol/varnish-examples","owner":"commenthol","description":"A collection of varnish examples","archived":false,"fork":false,"pushed_at":"2016-11-26T13:14:28.000Z","size":32,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-28T03:34:05.807Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/commenthol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-12T16:24:19.000Z","updated_at":"2022-11-19T14:29:48.000Z","dependencies_parsed_at":"2023-03-10T02:16:42.334Z","dependency_job_id":null,"html_url":"https://github.com/commenthol/varnish-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/commenthol/varnish-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fvarnish-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fvarnish-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fvarnish-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fvarnish-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/commenthol","download_url":"https://codeload.github.com/commenthol/varnish-examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fvarnish-examples/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267960176,"owners_count":24172490,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T11:22:43.459Z","updated_at":"2025-07-30T23:13:59.110Z","avatar_url":"https://github.com/commenthol.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Varnish Examples\n\n\u003e A collection of varnish examples\n\n## Table of Contents\n\n\u003c!-- !toc (minlevel=2 omit=\"Table of Contents\") --\u003e\n\n* [Prerequisites](#prerequisites)\n* [Examples](#examples)\n  * [Alive Page](#alive-page)\n  * [Switch to a different backend per URL](#switch-to-a-different-backend-per-url)\n  * [Listen to another port and redirect the request to the same server](#listen-to-another-port-and-redirect-the-request-to-the-same-server)\n  * [Whitelist cookies](#whitelist-cookies)\n  * [Do not cache 30x Redirects](#do-not-cache-30x-redirects)\n  * [Using restart if content not found at backend](#using-restart-if-content-not-found-at-backend)\n  * [Device detection with restart](#device-detection-with-restart)\n* [References](#references)\n\n\u003c!-- toc! --\u003e\n\n## Prerequisites\n\n- Varnish 4.0 [conf](conf/) / 3.0 [conf3](conf3/)\n- node.js\n- curl\n\n[`conf`](conf/) contains the examples for Varnish 4.0; [`conf3`](conf3/) contains the samples for Varnish 3.0\n\n## Examples\n\n### Alive Page\n\nCheck if varnish is up and running (e.g. if using with [monit][])\n\nSource: [alive.vcl](conf/alive.vcl)\n\n````sh\n# start the varnish webcache locally\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/alive.vcl\n\ncurl -v http://localhost:8000/alive\n````\n\n### Switch to a different backend per URL\n\nUse varnish to change to a different backend per URL.\n\nSource: [other.vcl](conf/other.vcl)\n\n````sh\n# start a http backend server\nnode src/backend.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/other.vcl\n\ncurl -v http://localhost:8000/\ncurl -v http://localhost:8000/other/\n\n# Wait some seconds - Age shall be greater than \"0\"\ncurl -v http://localhost:8000/\n\u003c Age: 5\n3000:/ GET\n\ncurl -v http://localhost:8000/other/\n\u003c Age: 6\n4000:/other/ GET\n\n# POST requests shall be passed - Age shall always be \"0\"\ncurl -v http://localhost:8000/ -X POST\n\u003c Age: 0\n3000:/ POST\n\ncurl -v http://localhost:8000/other/ -X POST\n\u003c Age: 0\n4000:/other/ POST\n````\n\n### Listen to another port and redirect the request to the same server\n\nSource: [secondport.vcl](conf/secondport.vcl)\n\n````sh\nnode src/backend.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -a 127.0.0.1:8001 -f conf/secondport.vcl\n\n# repeat the requests after some seconds to see that page gets cached\ncurl -v http://localhost:8000/\n\u003c Age: 5\n3000:/ GET\n\ncurl -v http://localhost:8001/\n\u003c Age: 5\n3000:/ GET\n\n# do not cache POST requests\ncurl -v http://localhost:8001/ -X POST\n\u003c Age: 0\n3000:/ POST\n````\n\n### Remove Response Headers\n\nFor security reasons hide the servers default response headers\n\nSource: [respheaders.vcl](conf/respheaders.vcl)\n\n```sh\nnode src/backend.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/respheaders.vcl\n\ncurl -v http://localhost:8000/\n\n```\n\n### Whitelist cookies\n\nRemove unwanted cookies from a request to improve caching\n\nSource: [cookiewhitelist.vcl](conf/cookiewhitelist.vcl)\n\nSee also [here][VCLExampleRemovingSomeCookies].\n\n````sh\nnode src/backend.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/cookiewhitelist.vcl\n\ncurl -v -b \"pass=1; delete=1\" http://localhost:8000\n\u003c Age: 0\ncookie: pass=1\n3000:/ GET\n````\n\n### Do not cache 30x Redirects\n\nSource: [nocache30x.vcl](conf/nocache30x.vcl)\n\n````sh\nnode src/backend.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/nocache30x.vcl\n\ncurl -v http://localhost:8000/301\n\u003c Location: /\n\u003c Age: 0\n\ncurl -v http://localhost:8000/302\n\u003c Location: /\n\u003c Age: 0\n````\n\n### Using restart if content not found at backend\n\nThis 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.\n\nSee [VCLExampleRestarts][]\n\n![Flow](assets/restart404.png)\n\nSource: [restart404.vcl](conf/restart404.vcl)\n\n````sh\nnode src/backend.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/restart404.vcl\n\ncurl -v http://localhost:8000/404/\n\u003c\n4000:/other GET undefined\n````\n\n### Device detection with restart\n\nThis 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.\n\nThe device-detection service here responds with a \"x-ua-device\" HTTP-Header which contains \"mobile\", \"tablet\" or \"other\" as device type.\nThis response than gets cached and restart issues the original request to the backend containing the \"x-ua-device\" HTTP-Header.\nWith this a device specific page is being delivered by the \"backend\" and cached as well.\n\n![Flow](assets/devicedetect.png)\n\nSource: [devicedetect.vcl](conf/devicedetect.vcl)\n\n````sh\n# start the sample device-detection service (this starts the backend as well)\nnode src/devicedetect.js \u0026\nvarnishd -F -n $(pwd) -a 127.0.0.1:8000 -f conf/devicedetect.vcl\n\ncurl -v \"http://localhost:8000\" -A iphone\n\u003c x-ua-device: mobile\n3000:/ GET mobile\n\ncurl -v \"http://localhost:8000\" -A android\n\u003c x-ua-device: tablet\n3000:/ GET tablet\n\ncurl -v \"http://localhost:8000\"\n\u003c x-ua-device: other\n3000:/ GET other\n````\n\n## References\n\n\u003c!-- !ref --\u003e\n\n* [monit][monit]\n* [Upgrading to Varnish 4.0][Upgrading to Varnish 4.0]\n* [varnish-examples][varnish-examples]\n* [VCLExampleRemovingSomeCookies][VCLExampleRemovingSomeCookies]\n* [VCLExampleRestarts][VCLExampleRestarts]\n\n\u003c!-- ref! --\u003e\n\n[varnish-examples]: https://www.varnish-cache.org/trac/wiki/VCLExamples\n[VCLExampleRemovingSomeCookies]: https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies\n[VCLExampleRestarts]: https://www.varnish-cache.org/trac/wiki/VCLExampleRestarts\n[monit]: https://mmonit.com/monit/\n[Upgrading to Varnish 4.0]: https://www.varnish-cache.org/docs/trunk/whats-new/upgrade-4.0.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fvarnish-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommenthol%2Fvarnish-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fvarnish-examples/lists"}