Guest User

Varnish Default.vcl

a guest
Mar 14th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.99 KB | None | 0 0
  1. # Original source: https://github.com/NITEMAN/varnish-bites/varnish4/drupal-base.vcl
  2. # Copyright (c) 2015 Pedro González Serrano and individual contributors.
  3. # MIT License
  4.  
  5. # Intended to be used both in simple production environments and with
  6. # learning/teaching purposes.
  7. # Some references may not be yet updated to VCL 4.0, we use SeeV3 then.
  8.  
  9. # WARNING:
  10. # Note that the built-in logic will be appended to our code if no return is
  11. # performed before.
  12. # Built-in logic is included commented out right after our's for reference
  13. # purposes in that cases.
  14. # See https://www.varnish-cache.org/trac/wiki/VCLExampleDefault
  15.  
  16. #######################################################################
  17. # Initialization: Version & imports;
  18.  
  19. # Since Varnish 4.0 it's mandatory to declare VCL version on first line.
  20. vcl 4.0;
  21.  
  22. # Standard module
  23. # See https://www.varnish-cache.org/docs/4.0/reference/vmod_std.generated.html
  24. import std;
  25.  
  26. # Directors module
  27. # See https://www.varnish-cache.org/docs/4.0/reference/vmod_directors.generated.html
  28. # Unused in simple configs.
  29. #import directors;
  30.  
  31.  
  32. #######################################################################
  33. # Probe, backend, ACL and subroutine definitions
  34.  
  35. # See https://www.varnish-cache.org/docs/4.0/reference/vcl.html#probes
  36. #probe basic {
  37. # This might be a too heavy probe
  38. # .url = "/";
  39.  
  40. # Nginx would fail this probe with a default config
  41. #.request =
  42. # "OPTIONS * HTTP/1.1"
  43. # "Host: *"
  44. # "Connection: close";
  45.  
  46. #.interval = 10s;
  47. #.timeout = 2s;
  48. #.window = 8;
  49. #.threshold = 6;
  50. #}
  51.  
  52. # See https://www.varnish-cache.org/docs/4.0/reference/vcl.html#backend-definition
  53. backend default {
  54. # WARNING: timeouts could be not big enought for certain POST requests.
  55. .host = "127.0.0.1";
  56. .port = "8080";
  57. .max_connections = 100;
  58. .connect_timeout = 60s;
  59. .first_byte_timeout = 60s;
  60. .between_bytes_timeout = 60s;
  61. #.probe = basic;
  62. }
  63.  
  64. # See https://www.varnish-cache.org/docs/4.0/reference/vcl.html#access-control-list-acl
  65. acl purge_ban {
  66. "localhost";
  67. "127.0.0.1";
  68. }
  69. acl allowed_monitors {
  70. "localhost";
  71. "127.0.0.1";
  72. }
  73. # acl own_proxys {
  74. # "127.0.0.1"/32; // We can use'"localhost";' instead
  75. #}
  76.  
  77. # See https://www.varnish-cache.org/docs/4.0/reference/vcl.html#subroutines
  78. #TODO# Test in Varnihs 4
  79. # Empty in simple configs.
  80. # The only restriction naming subs is that the 'vlc_' prefix is reserverd for
  81. # Varnish use. As a task can need several chunks of code in diferent states,
  82. # it's a good idea to identify what main sub will call each with a suffix.
  83. # /* Example 301 client redirection removing "www" prefix from request */
  84. # sub perm_redirections_recv {
  85. # if ( req.http.host ~ "^www.*$" ) {
  86. # return (
  87. # synth(751, "http://" + regsub(req.http.host, "^www\.", "") + req.url)
  88. # );
  89. # }
  90. # }
  91. # sub perm_redirections_synth {
  92. # if ( resp.status == 751 ) {
  93. # /* Get new URL from the response */
  94. # set resp.http.Location = resp.reason;
  95. # /* Set HTTP 301 for permanent redirect */
  96. # set resp.status = 301;
  97. # set resp.reason = "Moved Permanently";
  98. # return (deliver);
  99. # }
  100. # }
  101.  
  102.  
  103. #######################################################################
  104. # Client side
  105.  
  106. # vcl_recv: Called at the beginning of a request, after the complete request
  107. # has been received and parsed. Its purpose is to decide whether or not to
  108. # serve the request, how to do it, and, if applicable, which backend to use.
  109. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-recv
  110. sub vcl_recv {
  111. # Empty in simple configs.
  112. # Useful for debugging we can pipe or pass the request to default backend
  113. # here to bypass completely Varnish.
  114. # return (pipe);
  115. # return (pass);
  116. # We can also return here a 200 Ok for network performance benchmarking.
  117. # return (synth(200, "Ok"));
  118. # Finally we can perform basic HTTP authentification here, by example.
  119. # SeeV3 http://blog.tenya.me/blog/2011/12/14/varnish-http-authentication/
  120.  
  121. # Custom response implementation example in order to check that Varnish is
  122. # working properly.
  123. # This is usefull for automatic monitoring with monit or when Varnish is
  124. if ( ( req.http.host == "monitor.server.health"
  125. || req.http.host == "health.varnish" )
  126. && client.ip ~ allowed_monitors
  127. && ( req.method == "OPTIONS" || req.method == "GET" )
  128. ) {
  129. return (synth(200, "OK"));
  130. }
  131. # Purge logic
  132. # See https://www.varnish-cache.org/docs/4.0/users-guide/purging.html#http-purging
  133. # SeeV3 https://www.varnish-software.com/static/book/Cache_invalidation.html#removing-a-single-object
  134. if ( req.method == "PURGE" ) {
  135. if ( client.ip !~ purge_ban ) {
  136. return (synth(405, "Not allowed."));
  137. }
  138. return (hash);
  139. }
  140. # Ban logic
  141. # See https://www.varnish-cache.org/docs/4.0/users-guide/purging.html#bans
  142. if ( req.method == "BAN" ) {
  143. if ( client.ip !~ purge_ban ) {
  144. return (synth(405, "Not allowed."));
  145. }
  146. ban( "req.http.host == " + req.http.host +
  147. "&& req.url == " + req.url);
  148. return (synth(200, "Ban added"));
  149. }
  150.  
  151. # Avoid direct calls to .php files
  152. if (client.ip !~ allowed_monitors && req.url ~ "\.php" && req.url !~ "index\.php") {
  153. return (synth(403, "Not Allowed"));
  154. }
  155.  
  156. # Empty in simple configs.
  157. # call perm_redirections_recv;
  158. # Here we can also enforce SSL when Varnish run behind some SSL termination
  159. # point.
  160.  
  161. # Empty in simple configs.
  162.  
  163. # Empty in simple configs.
  164. # Example remove own_proxys from X-Forwarded-For
  165. # See https://www.varnish-cache.org/docs/4.0/whats-new/upgrading.html#x-forwarded-for-is-now-set-before-vcl-recv
  166. # Varnish 4 regsub doesn't accept anything but plain regexp, so we can't use
  167. # client.ip to exclude the proxy ips from the request:
  168. # set req.http.X-Forwarded-For
  169. # = regsub(req.http.X-Forwarded-For, ",( )?" + client.ip, "");
  170. # Instead, we need to add the proxy ips manually in the exclude list:
  171. #if ( req.restarts == 0
  172. # && client.ip ~ own_proxys
  173. # && req.http.x-forwarded-for
  174. #) {
  175. # set req.http.X-Forwarded-For
  176. # = regsub(req.http.X-Forwarded-For,
  177. # "(, )?(10\.10\.10\.10|10\.11\.11\.11)", "");
  178. #}
  179. # An alternative could be to skip all this and try to modify the header
  180. # manually so Varnish doesn't touch it.
  181. # set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "";
  182. #
  183. # Example normalize the host header, remove the port (in case you're testing
  184. # this on various TCP ports)
  185. # set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
  186. if (req.http.x-forwarded-for) {
  187. set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
  188. } else {
  189. set req.http.X-Forwarded-For = client.ip;
  190. }
  191.  
  192. # Useful for debugging we can now pipe or pass the request to backend with
  193. # headers setted.
  194. # return (pipe);
  195. # return (pass);
  196.  
  197. if ( req.method == "PRI" ) {
  198. return (synth(405));
  199. }
  200. if ( req.method != "GET"
  201. && req.method != "HEAD"
  202. && req.method != "PUT"
  203. && req.method != "POST"
  204. && req.method != "TRACE"
  205. && req.method != "OPTIONS"
  206. && req.method != "DELETE"
  207. ) {
  208. return (pipe);
  209. }
  210. if ( req.method != "GET"
  211. && req.method != "HEAD"
  212. ) {
  213. return (pass);
  214. }
  215. if ( req.http.Authorization ) {
  216. return (pass);
  217. }
  218. # Websocket support
  219. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-example-websockets.html
  220. if ( req.http.Upgrade ~ "(?i)websocket" ) {
  221. return (pipe);
  222. }
  223.  
  224. # Empty in simple configs.
  225. # By example denial some URLs depending on client-ip, we'll need to define
  226. # corresponding ACL 'internal'.
  227. # if ( req.url ~ "^/(cron|install)\.php"
  228. # && client.ip !~ internal
  229. # ) {
  230. # # Have Varnish throw the error directly.
  231. # return (synth(403, "Forbidden."));
  232. # # Use a custom error page that you've defined in Drupal at the path "404".
  233. # # set req.url = "/403";
  234. # }
  235.  
  236. # Host exception example:
  237. # if ( req.http.host == "ejemplo.exception.com" ) {
  238. # return (pass);
  239. # }
  240. # Drupal exceptions, edit if we want to cache some AJAX/AHAH request.
  241. # Add here filters for never cache URLs such as Payment Gateway's callbacks.
  242. if ( req.url ~ "^/status\.php$"
  243. || req.url ~ "^/update\.php$"
  244. || req.url ~ "^/ooyala/ping$"
  245. || req.url ~ "^/info/.*$"
  246. || req.url ~ "^/flag/.*$"
  247. || req.url ~ "^.*/ajax/.*$"
  248. || req.url ~ "^.*/ahah/.*$"
  249. || req.url ~ "^/opcache-gui/.*$"
  250. || req.url ~ "^/ejectorseat/check"
  251. || req.url ~ "^/user"
  252. || req.url ~ "^/user/.*$"
  253. || req.url ~ "^/overlay-ajax/*"
  254. || req.url ~ "^/overlay/dismiss-message"
  255. || req.url ~ "^/dynamic-select/ajax/*"
  256. || req.url ~ "^/webform/ajax/options/*"
  257. || req.url ~ "^/taxonomy/autocomplete/.*$"
  258. ) {
  259. return (pass);
  260. }
  261. # Pipe these paths directly to backend for streaming.
  262. if ( req.url ~ "^/admin/content/backup_migrate/export"
  263. || req.url ~ "^/admin/config/system/backup_migrate"
  264. ) {
  265. return (pipe);
  266. }
  267. if ( req.url ~ "^/system/files" ) {
  268. return (pipe);
  269. }
  270.  
  271. # See https://www.varnish-software.com/blog/grace-varnish-4-stale-while-revalidate-semantics-varnish
  272. # set req.http.grace = "none";
  273. if ( ! std.healthy(req.backend_hint) ) {
  274. # We must do this here since cookie hashing
  275. unset req.http.Cookie;
  276. #TODO# Add sick marker
  277. }
  278.  
  279. # Althought Varnish 3 handles gziped content itself by default, just to be
  280. # sure we want to remove Accept-Encoding for some compressed formats.
  281. # See https://www.varnish-cache.org/docs/4.0/phk/gzip.html#what-does-http-gzip-support-do
  282. # See https://www.varnish-cache.org/docs/4.0/users-guide/compression.html
  283. # See https://www.varnish-cache.org/docs/4.0/reference/varnishd.html?highlight=http_gzip_support
  284. # See (for older configs) https://www.varnish-cache.org/trac/wiki/VCLExampleNormalizeAcceptEncoding
  285. if ( req.http.Accept-Encoding ) {
  286. if ( req.url ~ "(?i)\.(7z|avi|bz2|flv|gif|gz|jpe?g|mpe?g|mk[av]|mov|mp[34]|og[gm]|pdf|png|rar|swf|tar|tbz|tgz|woff2?|zip|xz)(\?.*)?$"
  287. ) {
  288. unset req.http.Accept-Encoding;
  289. }
  290. }
  291.  
  292. # Empty in simple configs.
  293. # We could add here a custom header grouping User-agent families.
  294. # Generic URL manipulation.
  295. # Remove Google Analytics added parameters, useless for our backends.
  296. if ( req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=" ) {
  297. set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
  298. set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
  299. set req.url = regsub(req.url, "\?&", "?");
  300. set req.url = regsub(req.url, "\?$", "");
  301. }
  302. # Strip anchors, server doesn't need it.
  303. if ( req.url ~ "\#" ) {
  304. set req.url = regsub(req.url, "\#.*$", "");
  305. }
  306. # Strip a trailing ? if it exists
  307. if ( req.url ~ "\?$" ) {
  308. set req.url = regsub(req.url, "\?$", "");
  309. }
  310. # Normalize the querystring arguments
  311. set req.url = std.querysort(req.url);
  312.  
  313. # Always cache the following static file types for all users.
  314. # Use with care if we control certain downloads depending on cookies.
  315. # Be carefull also if appending .htm[l] via Drupal's clean URLs.
  316. if ( req.url ~ "(?i)\.(bz2|css|eot|gif|gz|html?|ico|jpe?g|js|mp3|ogg|otf|pdf|png|rar|svg|swf|tbz|tgz|ttf|woff2?|zip)(\?(itok=)?[a-z0-9_=\.\-]+)?$"
  317. && req.url !~ "/system/storage/serve"
  318. ) {
  319. unset req.http.Cookie;
  320. }
  321. # Remove all cookies that backend doesn't need to know about.
  322. # See https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies
  323. if ( req.http.Cookie ) {
  324. # Prefix header containing cookies with ';'
  325. set req.http.Cookie = ";" + req.http.Cookie;
  326. # Remove any spaces after ';' in header containing cookies
  327. set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
  328. # Prefix cookies we want to preserve with one space:
  329. # 'S{1,2}ESS[a-z0-9]+' is the regular expression matching a Drupal session
  330. # cookie ({1,2} added for HTTPS support).
  331. # 'NO_CACHE' is usually set after a POST request to make sure issuing user
  332. # see the results of his post.
  333. # 'OATMEAL' & 'CHOCOLATECHIP' are special cookies used by Drupal's Bakery
  334. # module to provide Single Sign On.
  335. # Keep in mind we should add here any cookie that should reach the backend
  336. # such as splash avoiding cookies.
  337. set req.http.Cookie
  338. = regsuball(
  339. req.http.Cookie,
  340. ";(S{1,2}ESS[a-z0-9]+|NO_CACHE|OATMEAL|CHOCOLATECHIP)=",
  341. "; \1="
  342. );
  343. # Remove from the header any single Cookie not prefixed with a space until
  344. # next ';' separator.
  345. set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
  346. # Remove any '; ' at the start or the end of the header.
  347. set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
  348. #If there are no remaining cookies, remove the cookie header.
  349. if ( req.http.Cookie == "" ) {
  350. unset req.http.Cookie;
  351. }
  352. }
  353.  
  354. # As we might want to cache some requests, hashed with its cookies, we don't
  355. # simply pass when some cookies remain present at this point.
  356. # Instead we look for request that must be passed due to the cookie header.
  357. if ( req.http.Cookie ~ "SESS"
  358. || req.http.Cookie ~ "SSESS"
  359. || req.http.Cookie ~ "NO_CACHE"
  360. || req.http.Cookie ~ "OATMEAL"
  361. || req.http.Cookie ~ "CHOCOLATECHIP"
  362. ) {
  363. return (pass);
  364. }
  365.  
  366. # Empty in simple configs.
  367. # See https://www.varnish-cache.org/docs/4.0/users-guide/esi.html
  368. # Note that ESI included requests inherits its parent's modified request, so
  369. # depending on the case you will end playing with req.esi_level to know
  370. # current depth.
  371. # Send Surrogate-Capability headers
  372. # See http://www.w3.org/TR/edge-arch
  373. # Note that myproxyname is an identifier that should avoid collitions
  374. # set req.http.Surrogate-Capability = "myproxyname=ESI/1.0";
  375.  
  376. # Useful for debugging we can now pipe or pass the request to backend to
  377. # bypass cache.
  378. # return (pipe);
  379. # return (pass);
  380.  
  381. # We make sure no built-in logic is processed after ours returning
  382. # inconditionally.
  383. return (hash);
  384. }
  385.  
  386. # vcl_pipe: Called upon entering pipe mode.
  387. # In this mode, the request is passed on to the backend, and any further data
  388. # from either client or backend is passed on unaltered until either end closes
  389. # the connection.
  390. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-pipe
  391. sub vcl_pipe {
  392. # Websocket support
  393. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-example-websockets.html
  394. if ( req.http.upgrade ) {
  395. set bereq.http.upgrade = req.http.upgrade;
  396. }
  397. }
  398. # sub vcl_pipe {
  399. # # By default Connection: close is set on all piped requests, to stop
  400. # # connection reuse from sending future requests directly to the
  401. # # (potentially) wrong backend. If you do want this to happen, you can undo
  402. # # it here.
  403. # # unset bereq.http.connection;
  404. # return (pipe);
  405. # }
  406.  
  407. # vcl_pass: Called upon entering pass mode.
  408. # In this mode, the request is passed on to the backend, and the backend's
  409. # response is passed on to the client, but is not entered into the cache.
  410. # Subsequent requests submitted over the same client connection are handled normally.
  411. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-pass
  412. # sub vcl_pass {
  413. # return (fetch);
  414. # }
  415.  
  416. # vcl_hash: You may call hash_data() on the data you would like to add to the
  417. # hash.
  418. # Hash is used by Varnish to uniquely identify objects.
  419. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-hash
  420. sub vcl_hash {
  421. # As requests with same URL and host can produce diferent results when issued
  422. # with different cookies, we need to store items hashed with the associated
  423. # cookies. Note that cookies are already sanitized when we reach this point.
  424. if ( req.http.Cookie ) {
  425. hash_data(req.http.Cookie);
  426. }
  427.  
  428. if (req.http.Accept-Language) {
  429. hash_data(req.http.Accept-Language);
  430. }
  431.  
  432. # Empty in simple configs.
  433. # Example for caching differents object versions by device previously
  434. # detected (when static content could also vary):
  435. # if ( req.http.X-UA-Device ) {
  436. # hash_data(req.http.X-UA-Device);
  437. # }
  438. # Example for caching diferent object versions by X-Forwarded-Proto, trying
  439. # to be smart about what kind of request could generate diffetent responses.
  440. if ( req.http.X-Forwarded-Proto
  441. && req.url !~ "(?i)\.(bz2|css|eot|gif|gz|html?|ico|jpe?g|js|mp3|ogg|otf|pdf|png|rar|svg|swf|tbz|tgz|ttf|woff2?|zip)(\?(itok=)?[a-z0-9_=\.\-]+)?$"
  442. ) {
  443. hash_data(req.http.X-Forwarded-Proto);
  444. }
  445.  
  446. # We want built-in logic to be processed after ours so we don't call return.
  447. }
  448. # sub vcl_hash {
  449. # hash_data(req.url);
  450. # if (req.http.host) {
  451. # hash_data(req.http.host);
  452. # } else {
  453. # hash_data(server.ip);
  454. # }
  455. # return (lookup);
  456. # }
  457.  
  458. # vcl_purge: Called after the purge has been executed and all its variants have
  459. # been evited.
  460. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-purge
  461. # sub vcl_purge {
  462. # return (synth(200, "Purged"));
  463. # }
  464.  
  465. # vcl_hit: Called after a cache lookup if the requested document was found in
  466. # the cache.
  467. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-hit
  468. sub vcl_hit {
  469. if ( obj.ttl >= 0s ) {
  470. return (deliver);
  471. }
  472. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-grace.html
  473. # See https://www.varnish-software.com/blog/grace-varnish-4-stale-while-revalidate-semantics-varnish
  474. if ( obj.ttl + 60s > 0s ) {
  475. set req.http.grace = "normal";
  476. return (deliver);
  477. }
  478. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-grace.html
  479. # See https://www.varnish-software.com/blog/grace-varnish-4-stale-while-revalidate-semantics-varnish
  480. if ( ! std.healthy(req.backend_hint)
  481. && obj.ttl + obj.grace > 0s
  482. ) {
  483. set req.http.grace = "extended";
  484. return (deliver);
  485. }
  486. # We make sure no built-in logic is processed after ours returning
  487. # inconditionally.
  488. return (fetch);
  489. }
  490.  
  491. # vcl_miss: Called after a cache lookup if the requested document was not found
  492. # in the cache.
  493. # Its purpose is to decide whether or not to attempt to retrieve the document
  494. # from the backend, and which backend to use.
  495. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-miss
  496. # sub vcl_miss {
  497. # return (fetch);
  498. # }
  499.  
  500. # vcl_deliver: Called before an object is delivered to the client
  501. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-deliver
  502. sub vcl_deliver {
  503. # Please consider the risks of showing publicly this information, we can wrap
  504. # this with an ACL.
  505. # Add whether the object is a cache hit or miss and the number of hits for
  506. # the object.
  507. # SeeV3 https://www.varnish-cache.org/trac/wiki/VCLExampleHitMissHeader#Addingaheaderindicatinghitmiss
  508. # In Varnish 4 the obj.hits counter behaviour has changed (see bug 1492), so
  509. # we use a different method: if X-Varnish contains only 1 id, we have a miss,
  510. # if it contains more (and therefore a space), we have a hit.
  511. if ( resp.http.x-varnish ~ " " ) {
  512. set resp.http.X-Cache = "HIT";
  513. # Since in Varnish 4 the behaviour of obj.hits changed, this might not be
  514. # accurate.
  515. # See https://www.varnish-cache.org/trac/ticket/1492
  516. set resp.http.X-Cache-Hits = obj.hits;
  517. } else {
  518. set resp.http.X-Cache = "MISS";
  519. set resp.http.X-Cookie = req.http.Cookie;
  520. }
  521. # See https://www.varnish-software.com/blog/grace-varnish-4-stale-while-revalidate-semantics-varnish
  522. set resp.http.grace = req.http.grace;
  523.  
  524. #TODO# Add sick marker
  525.  
  526. # Restart count
  527. if ( req.restarts > 0 ) {
  528. set resp.http.X-Restarts = req.restarts;
  529. }
  530.  
  531. # Add the Varnish server hostname
  532. set resp.http.X-Varnish-Server = req.http.host;
  533. # If we have setted a custom header with device's family detected we can show
  534. # it:
  535. # if ( req.http.X-UA-Device ) {
  536. # set resp.http.X-UA-Device = req.http.X-UA-Device;
  537. # }
  538. # If we have recived a custom header indicating the protocol in the request we
  539. # can show it:
  540. # if ( req.http.X-Forwarded-Proto ) {
  541. # set resp.http.X-Forwarded-Proto = req.http.X-Forwarded-Proto;
  542. # }
  543.  
  544. # Empty in simple configs.
  545. # By example, if we are storing & serving diferent objects depending on
  546. # User-Agent header we must set the correct Vary header:
  547. # if ( resp.http.Vary ) {
  548. # set resp.http.Vary = resp.http.Vary + ",User-Agent";
  549. # } else {
  550. # set resp.http.Vary = "User-Agent";
  551. # }
  552.  
  553. # Empty in simple configs
  554. # We can fake server headers here, by example:
  555. # set resp.http.Server = "Deep thought";
  556. # set resp.http.X-Powered-By = "BOFH";
  557. # Or have some fun with headers:
  558. # See http://www.nextthing.org/archives/2005/08/07/fun-with-http-headers
  559. # See http://royal.pingdom.com/2012/08/15/fun-and-unusual-http-response-headers/
  560. # set resp.http.X-Thank-You = "for bothering to look at my HTTP headers";
  561. # set resp.http.X-Answer = "42";
  562.  
  563. # We want built-in logic to be processed after ours so we don't call return.
  564. }
  565. # sub vcl_deliver {
  566. # return (deliver);
  567. # }
  568.  
  569. # vcl_synth: Called to deliver a synthetic object. A synthetic object is
  570. # generated in VCL, not fetched from the backend. It is typically contructed
  571. # using the synthetic() function.
  572. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-synth
  573. sub vcl_synth {
  574. # Empty in simple configs.
  575. # call perm_redirections_synth;
  576.  
  577. # Note that max_restarts defaults to 4
  578. # SeeV3 https://www.varnish-cache.org/trac/wiki/VCLExampleRestarts
  579. if (resp.status == 850) {
  580. set resp.http.Location = req.http.x-redir;
  581. set resp.status = 302;
  582. return (deliver);
  583. }
  584.  
  585. if ( resp.status == 503
  586. && req.restarts < 4
  587. ) {
  588. return (restart);
  589. }
  590.  
  591. set resp.http.Content-Type = "text/html; charset=utf-8";
  592.  
  593. # Empty in simple configs.
  594. # SeeV3 http://blog.tenya.me/blog/2011/12/14/varnish-http-authentication/
  595.  
  596. # Note that files loaded this way are never re-readed (even after a reload).
  597. # You should consider PROS/CONS of doing an include instead.
  598. # See https://www.varnish-cache.org/docs/4.0/reference/vmod_std.generated.html#func-fileread
  599. # Example custom 403 error page.
  600. # if ( resp.status == 403 ) {
  601. # synthetic(std.fileread("/403.html"));
  602. # return (deliver);
  603. # }
  604.  
  605. # We have plenty of choices when we have to serve an error to the client,
  606. # from the default error page to javascript black magic or plain redirections.
  607. # Adding some external statistic javascript to track failures served to
  608. # clients is strongly suggested.
  609. # We can't use external resources on synthetic content, everything must be
  610. # inlined.
  611. # If we need to include images we can embed them in base64 encoding.
  612. # We're using error 200 for monitoring puposes which should not be retried
  613. # client side.
  614. if ( resp.status != 200 ) {
  615. set resp.http.Retry-After = "5";
  616. }
  617.  
  618. # Here is the default error page for Varnish 4 (not so pretty)
  619. synthetic( {"<!DOCTYPE html>
  620. <html>
  621. <head>
  622. <title>"} + resp.status + " " + resp.reason + {"</title>
  623. </head>
  624. <body>
  625. <h1>Error "} + resp.status + " " + resp.reason + {"</h1>
  626. <p>"} + resp.reason + {"</p>
  627. <h3>Guru Meditation:</h3>
  628. <p>XID: "} + req.xid + {"</p>
  629. <hr>
  630. <p>Varnish cache server</p>
  631. </body>
  632. </html>
  633. "} );
  634.  
  635. # We make sure no built-in logic is processed after ours returning
  636. # inconditionally.
  637. return (deliver);
  638. }
  639.  
  640. #######################################################################
  641. # Backend Fetch
  642.  
  643. # vcl_backend_fetch: Called before sending the backend request. In this
  644. # subroutine you typically alter the request before it gets to the backend.
  645. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-backend-fetch
  646. # sub vcl_backend_fetch {
  647. # return (fetch);
  648. # }
  649.  
  650. # vcl_backend_response: Called after the response headers has been successfully
  651. # retrieved from the backend.
  652. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-backend-response
  653. sub vcl_backend_response {
  654. # Varnish will cache objects with response codes:
  655. # 200, 203, 300, 301, 302, 307, 404 & 410.
  656. # SeeV3 https://www.varnish-software.com/static/book/VCL_Basics.html#the-initial-value-of-beresp-ttl
  657. # Drupal's Imagecache module can return a 307 redirection to the requested
  658. # url itself and, depending on Drupal's cache settings, this could lead to a
  659. # redirection loop being cached for a long time but also we want Varnish to
  660. # shield a little the backend.
  661. # See http://drupal.org/node/1248010
  662. # See http://drupal.org/node/310656
  663. if ( beresp.status == 307
  664. #TODO# verify that this work better than 'bereq.url ~ "imagecache"'
  665. && beresp.http.Location == bereq.url
  666. && beresp.ttl > 5s
  667. ) {
  668. set beresp.ttl = 5s;
  669. set beresp.http.cache-control = "max-age=5";
  670. }
  671.  
  672. if ( beresp.status == 500
  673. || beresp.status == 503
  674. ) {
  675. #TODO# consider not restarting POST requests as seenV3 on https://www.varnish-cache.org/trac/wiki/VCLExampleSaintMode
  676. return (retry);
  677. }
  678.  
  679. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-grace.html
  680. # See https://www.varnish-software.com/blog/grace-varnish-4-stale-while-revalidate-semantics-varnish
  681. set beresp.grace = 1h;
  682.  
  683. # Related with our 12th stage on vcl_recv
  684. if ( bereq.url ~ "(?i)\.(bz2|css|eot|gif|gz|html?|ico|jpe?g|js|mp3|ogg|otf|pdf|png|rar|svg|swf|tbz|tgz|ttf|woff2?|zip)(\?(itok=)?[a-z0-9_=\.\-]+)?$"
  685. ) {
  686. unset beresp.http.set-cookie;
  687. }
  688.  
  689. # Empty in simple configs.
  690. # See https://www.varnish-cache.org/docs/4.0/users-guide/esi.html
  691. # Send Surrogate-Capability headers
  692. # See http://www.w3.org/TR/edge-arch
  693. # Note that myproxyname is an identifier that should avoid collitions
  694. # Check for ESI acknowledgement and remove Surrogate-Control header
  695. #TODO# Add support for Surrogate-Control Targetting
  696. # if ( beresp.http.Surrogate-Control ~ "ESI/1.0" ) {
  697. # unset beresp.http.Surrogate-Control;
  698. # set beresp.do_esi = true;
  699. # }
  700.  
  701. # Empty in simple configs.
  702. # Use Varnish to Gzip respone, if suitable, before storing it on cache.
  703. # See https://www.varnish-cache.org/docs/4.0/users-guide/compression.html
  704. # See https://www.varnish-cache.org/docs/4.0/phk/gzip.html
  705. if ( ! beresp.http.Content-Encoding
  706. && ( beresp.http.content-type ~ "text"
  707. || beresp.http.content-type ~ "application/x-javascript"
  708. || beresp.http.content-type ~ "application/javascript"
  709. || beresp.http.content-type ~ "application/rss+xml"
  710. || beresp.http.content-type ~ "application/xml"
  711. || beresp.http.content-type ~ "Application/JSON")
  712. ) {
  713. set beresp.do_gzip = true;
  714. }
  715.  
  716. # Please consider the risks of showing publicly this information, we can wrap
  717. # this with an ACL.
  718. # We can add the name of the backend that has processed the request:
  719. # set beresp.http.X-Backend = beresp.backend.name;
  720. # We can use a header to tell if the object was gziped by Varnish:
  721. # if ( beresp.do_gzip ) {
  722. # set beresp.http.X-Varnish-Gzipped = "yes";
  723. # } else {
  724. # set beresp.http.X-Varnish-Gizipped = "no";
  725. # }
  726. # We can do the same to tell if Varnish is streaming it:
  727. # if ( beresp.do_stream ) {
  728. # set beresp.http.X-Varnish-Streaming = "yes";
  729. # } else {
  730. # set beresp.http.X-Varnish-Streaming = "no";
  731. # }
  732. # We can also add headers informing whether the object is cacheable or not and why:
  733. # SeeV3 https://www.varnish-cache.org/trac/wiki/VCLExampleHitMissHeader#Varnish3.0
  734. if ( beresp.ttl <= 0s ) {
  735. set beresp.http.X-Cacheable = "NO:Not Cacheable";
  736. } elsif ( bereq.http.Cookie ~ "(SESS|SSESS|NO_CACHE|OATMEAL|CHOCOLATECHIP)" ) {
  737. # Related with our 9th stage on vcl_recv
  738. set beresp.http.X-Cacheable = "NO:Cookies";
  739. # set beresp.uncacheable = true;
  740. } elsif ( beresp.http.Cache-Control ~ "private" ) {
  741. set beresp.http.X-Cacheable = "NO:Cache-Control=private";
  742. # set beresp.uncacheable = true;
  743. } else {
  744. set beresp.http.X-Cacheable = "YES";
  745. }
  746.  
  747. # Empty in simple configs.
  748. # We can also unset some headers to prevent information disclosure and save
  749. # some cache space.
  750. # unset beresp.http.Server;
  751. # unset beresp.http.X-Powered-By;
  752. # Retry count.
  753. if ( bereq.retries > 0 ) {
  754. set beresp.http.X-Retries = bereq.retries;
  755. }
  756.  
  757. # We want built-in logic to be processed after ours so we don't call return.
  758. }
  759. # sub vcl_backend_response {
  760. # if (beresp.ttl <= 0s ||
  761. # beresp.http.Set-Cookie ||
  762. # beresp.http.Surrogate-control ~ "no-store" ||
  763. # (!beresp.http.Surrogate-Control &&
  764. # beresp.http.Cache-Control ~ "no-cache|no-store|private") ||
  765. # beresp.http.Vary == "*") {
  766. # /*
  767. # * Mark as "Hit-For-Pass" for the next 2 minutes
  768. # */
  769. # set beresp.ttl = 120s;
  770. # set beresp.uncacheable = true;
  771. # }
  772. # return (deliver);
  773. # }
  774.  
  775. # vcl_backend_error: This subroutine is called if we fail the backend fetch.
  776. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-backend-error
  777. sub vcl_backend_error {
  778.  
  779. #TODO# Confirm max_retries default value
  780. # SeeV3 https://www.varnish-cache.org/trac/wiki/VCLExampleRestarts
  781. if ( bereq.retries < 4 ) {
  782. return (retry);
  783. }
  784.  
  785. # Please consider the risks of showing publicly this information, we can wrap
  786. # this with an ACL.
  787. # Retry count
  788. if ( bereq.retries > 0 ) {
  789. set beresp.http.X-Retries = bereq.retries;
  790. }
  791.  
  792. set beresp.http.Content-Type = "text/html; charset=utf-8";
  793. set beresp.http.Retry-After = "5";
  794. synthetic( {"<!DOCTYPE html>
  795. <html>
  796. <head>
  797. <title>"} + beresp.status + " " + beresp.reason + {"</title>
  798. </head>
  799. <body>
  800. <h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
  801. <p>"} + beresp.reason + {"</p>
  802. <h3>Guru Meditation:</h3>
  803. <p>XID: "} + bereq.xid + {"</p>
  804. <hr>
  805. <p>Varnish cache server</p>
  806. </body>
  807. </html>
  808. "} );
  809.  
  810. # We make sure no built-in logic is processed after ours returning at this
  811. # point.
  812. return (deliver);
  813. }
  814.  
  815. #######################################################################
  816. # Housekeeping
  817.  
  818. # vcl_init: Called when VCL is loaded, before any requests pass through it.
  819. # Typically used to initialize VMODs.
  820. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-init
  821. # Here is where you should declare your directors now.
  822. # See https://www.varnish-cache.org/docs/4.0/reference/vmod_directors.generated.html
  823. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-backends.html#directors
  824. # Empty in simple configs
  825. # sub vcl_init {
  826. # return (ok);
  827. # }
  828.  
  829. # vcl_fini: Called when VCL is discarded only after all requests have exited
  830. # the VCL. Typically used to clean up VMODs.
  831. # See https://www.varnish-cache.org/docs/4.0/users-guide/vcl-built-in-subs.html#vcl-fini
  832. # sub vcl_fini {
  833. # return (ok);
  834. # }
Add Comment
Please, Sign In to add comment