{"id":341,"date":"2013-08-15T23:37:15","date_gmt":"2013-08-15T23:37:15","guid":{"rendered":"http:\/\/3dd48ab31d016ffcbf3314df2b3cb9ce"},"modified":"2018-07-01T10:08:34","modified_gmt":"2018-07-01T02:08:34","slug":"varnish-vcl","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/341","title":{"rendered":"varnish vcl"},"content":{"rendered":"<pre class=\"brush:perl; toolbar: true; auto-links: true;\"># This is a basic VCL configuration file for varnish.  See the vcl(7)\r\n# man page for details on VCL syntax and semantics.\r\n# \r\n# Default backend definition.  Set this to point to your content\r\n# server.\r\n# \r\n\r\nprobe healthcheck {\r\n   .url = \"\/\";\r\n   .interval = 60s;\r\n   .timeout = 0.3s;\r\n   .window = 8;\r\n   .threshold = 3;\r\n   .initial = 3;\r\n}\r\n\r\n#backend local {\r\n#\t.host = \"127.0.0.1\";\r\n#\t.port = \"80\";\r\n#}\r\n\r\nbackend web244 {\r\n\t.host = \"192.168.2.244\";\r\n\t.port = \"80\";\r\n\t.probe = healthcheck;\r\n}\r\nbackend web243 {\r\n\t.host = \"192.168.2.243\";\r\n\t.port = \"80\";\r\n\t.probe = healthcheck;\r\n}\r\nbackend web232 {\r\n\t.host = \"192.168.2.232\";\r\n\t.port = \"80\";\r\n\t.probe = healthcheck;\r\n}\r\n\r\ndirector default round-robin {\r\n\t{ .backend = web244; }\r\n\t{ .backend = web243; }\r\n}\r\n\r\ndirector image fallback{\r\n\t{ .backend = web232; }\r\n\t{ .backend = web243; }\r\n}\r\n\r\n# \r\n# Below is a commented-out copy of the default VCL logic.  If you\r\n# redefine any of these subroutines, the built-in logic will be\r\n# appended to your code.\r\nsub vcl_recv {\r\n#     if (req.restarts == 0) {}\r\n \r\n\tif (req.http.x-forwarded-for) {\r\n \t    set req.http.X-Forwarded-For =\r\n \t\treq.http.X-Forwarded-For + \", \" + client.ip;\r\n \t} else {\r\n \t    set req.http.X-Forwarded-For = client.ip;\r\n \t}\r\n\r\n\tif (req.http.host == \"static.aooshi.org\"\r\n\t|| req.http.host == \"image.aooshi.org\"\r\n\t) {\r\n\t\tset req.backend = image;\r\n\t}\r\n     \r\n#     if (req.request != \"GET\" &amp;&amp;\r\n#       req.request != \"HEAD\" &amp;&amp;\r\n#       req.request != \"PUT\" &amp;&amp;\r\n#       req.request != \"POST\" &amp;&amp;\r\n#       req.request != \"TRACE\" &amp;&amp;\r\n#       req.request != \"OPTIONS\" &amp;&amp;\r\n#       req.request != \"DELETE\") {\r\n#         \/* Non-RFC2616 or CONNECT which is weird. *\/\r\n#         return (pipe);\r\n#     }\r\n\r\n     if (req.request != \"GET\" &amp;&amp; req.request != \"HEAD\") {\r\n         \/* We only deal with GET and HEAD by default *\/\r\n         return (pass);\r\n     }\r\n\r\n#     if (req.http.Authorization || req.http.Cookie) {\r\n     if (req.http.Authorization) {\r\n#         \/* Not cacheable by default *\/\r\n         return (pass);\r\n     }\r\n     return (lookup);\r\n}\r\n# \r\n# sub vcl_pipe {\r\n#     # Note that only the first request to the backend will have\r\n#     # X-Forwarded-For set.  If you use X-Forwarded-For and want to\r\n#     # have it set for all requests, make sure to have:\r\n#     # set bereq.http.connection = \"close\";\r\n#     # here.  It is not set by default as it might break some broken web\r\n#     # applications, like IIS with NTLM authentication.\r\n#     return (pipe);\r\n# }\r\n# \r\n# sub vcl_pass {\r\n#     return (pass);\r\n# }\r\n# \r\n# sub vcl_hash {\r\n#     hash_data(req.url);\r\n#     if (req.http.host) {\r\n#         hash_data(req.http.host);\r\n#     } else {\r\n#         hash_data(server.ip);\r\n#     }\r\n#     return (hash);\r\n# }\r\n# \r\nsub vcl_hit {\r\n     return (deliver);\r\n}\r\n# \r\n# sub vcl_miss {\r\n#     return (fetch);\r\n# }\r\n# \r\nsub vcl_fetch {\r\n     if (beresp.ttl &lt;= 0s ||\r\n         beresp.http.Set-Cookie ||\r\n         beresp.http.Vary == \"*\" ||\r\n         beresp.http.Pragma == \"no-cache\" ||\r\n         beresp.http.Cache-Control == \"no-cache\" ||\r\n         beresp.http.Cache-Control == \"private\" ||\r\n         beresp.status != 200\r\n        ) {\r\n#               \/*\r\n#                * Mark as \"Hit-For-Pass\" for the next 2 minutes\r\n#                *\/\r\n#               set beresp.ttl = 120 s;\r\n                return (hit_for_pass);\r\n     }\r\n     return (deliver);\r\n}\r\n# \r\nsub vcl_deliver {\r\n\tremove resp.http.X-Varnish; \r\n#\tremove resp.http.Via; \r\n     if (obj.hits &gt; 0) {\r\n        set resp.http.X-Cache = \"HIT from \" + server.hostname;\r\n     }\r\n\treturn (deliver);\r\n}\r\n# \r\n# sub vcl_error {\r\n#     set obj.http.Content-Type = \"text\/html; charset=utf-8\";\r\n#     set obj.http.Retry-After = \"5\";\r\n#     synthetic {\"\r\n# &lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n# &lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN\"\r\n#  \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd\"&gt;\r\n# &lt;html&gt;\r\n#   &lt;head&gt;\r\n#     &lt;title&gt;\"} + obj.status + \" \" + obj.response + {\"&lt;\/title&gt;\r\n#   &lt;\/head&gt;\r\n#   &lt;body&gt;\r\n#     &lt;h1&gt;Error \"} + obj.status + \" \" + obj.response + {\"&lt;\/h1&gt;\r\n#     &lt;p&gt;\"} + obj.response + {\"&lt;\/p&gt;\r\n#     &lt;h3&gt;Guru Meditation:&lt;\/h3&gt;\r\n#     &lt;p&gt;XID: \"} + req.xid + {\"&lt;\/p&gt;\r\n#     &lt;hr&gt;\r\n#     &lt;p&gt;Varnish cache server&lt;\/p&gt;\r\n#   &lt;\/body&gt;\r\n# &lt;\/html&gt;\r\n# \"};\r\n#     return (deliver);\r\n# }\r\n# \r\n# sub vcl_init {\r\n# \treturn (ok);\r\n# }\r\n# \r\n# sub vcl_fini {\r\n# \treturn (ok);\r\n# }<\/pre>\n<p><strong>\u5176\u5b83\uff1a<\/strong><\/p>\n<p>\uff081\uff09\u00a0Receive\u72b6\u6001\uff0c\u4e5f\u5c31\u662f\u8bf7\u6c42\u5904\u7406\u7684\u5165\u53e3\u72b6\u6001\uff0c\u6839\u636eVCL\u89c4\u5219\u5224\u65ad\u8be5\u8bf7\u6c42\u5e94\u8be5\u662fPass\u6216Pipe\uff0c\u6216\u8005\u8fdb\u5165Lookup\uff08\u672c\u5730\u67e5\u8be2\uff09\u3002<br \/>\n\uff082\uff09\u00a0Lookup\u72b6\u6001\uff0c\u8fdb\u5165\u6b64\u72b6\u6001\u540e\uff0c\u4f1a\u5728hash\u8868\u4e2d\u67e5\u627e\u6570\u636e\uff0c\u82e5\u627e\u5230\uff0c\u5219\u8fdb\u5165Hit\u72b6\u6001\uff0c\u5426\u5219\u8fdb\u5165miss\u72b6\u6001\u3002<br \/>\n\uff083\uff09\u00a0Pass\u72b6\u6001\uff0c\u5728\u6b64\u72b6\u6001\u4e0b\uff0c\u4f1a\u8fdb\u5165\u540e\u7aef\u8bf7\u6c42\uff0c\u5373\u8fdb\u5165fetch\u72b6\u6001\u3002<br \/>\n\uff084\uff09\u00a0Fetch\u72b6\u6001\uff0c\u5728Fetch\u72b6\u6001\u4e0b\uff0c\u5bf9\u8bf7\u6c42\u8fdb\u884c\u540e\u7aef\u7684\u83b7\u53d6\uff0c\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u5f97\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u672c\u5730\u7684\u5b58\u50a8\u3002<br \/>\n\uff085\uff09\u00a0Deliver\u72b6\u6001\uff0c \u5c06\u83b7\u53d6\u5230\u7684\u6570\u636e\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u7136\u540e\u5b8c\u6210\u672c\u6b21\u8bf7\u6c42\u3002<\/p>\n<p><a id=\"ematt:215\" href=\"\/notes\/content\/uploadfile\/201308\/72ba1376620265.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"\/notes\/content\/uploadfile\/201308\/72ba1376620265.png\" alt=\"\u70b9\u51fb\u67e5\u770b\u539f\u56fe\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5b9e\u4f8b\u89e3\u6790<\/strong>:<\/p>\n<p>#<br \/>\n# This is an example VCL file for Varnish.<br \/>\n#<br \/>\n# It does not do anything by default, delegating control to the<br \/>\n# builtin VCL. The builtin VCL is called when there is no explicit<br \/>\n# return statement.<br \/>\n#<br \/>\n# See the VCL chapters in the Users Guide at https:\/\/www.varnish-cache.org\/docs\/<br \/>\n# and http:\/\/varnish-cache.org\/trac\/wiki\/VCLExamples for more examples.<br \/>\n# Marker to tell the VCL compiler that this VCL has been adapted to the<br \/>\n# new 4.0 format.<br \/>\nvcl 4.0;<br \/>\nimport directors;<br \/>\nprobe backend_healthcheck { # \u521b\u5efa\u5065\u5eb7\u76d1\u6d4b<br \/>\n.url = \/health.html;<br \/>\n.window = 5;<br \/>\n.threshold = 2;<br \/>\n.interval = 3s;<br \/>\n}<br \/>\nbackend web1 { # \u521b\u5efa\u540e\u7aef\u4e3b\u673a<br \/>\n.host = \"static1.lnmmp.com\";<br \/>\n.port = \"80\";<br \/>\n.probe = backend_healthcheck;<br \/>\n}<br \/>\nbackend web2 {<br \/>\n.host = \"static2.lnmmp.com\";<br \/>\n.port = \"80\";<br \/>\n.probe = backend_healthcheck;<br \/>\n}<br \/>\nbackend img1 {<br \/>\n.host = \"img1.lnmmp.com\";<br \/>\n.port = \"80\";<br \/>\n.probe = backend_healthcheck;<br \/>\n}<br \/>\nbackend img2 {<br \/>\n.host = \"img2.lnmmp.com\";<br \/>\n.port = \"80\";<br \/>\n.probe = backend_healthcheck;<br \/>\n}<br \/>\nvcl_init { # \u521b\u5efa\u540e\u7aef\u4e3b\u673a\u7ec4\uff0c\u5373directors<br \/>\nnew web_cluster = directors.random();<br \/>\nweb_cluster.add_backend(web1);<br \/>\nweb_cluster.add_backend(web2);<br \/>\nnew img_cluster = directors.random();<br \/>\nimg_cluster.add_backend(img1);<br \/>\nimg_cluster.add_backend(img2);<br \/>\n}<br \/>\nacl purgers { # \u5b9a\u4e49\u53ef\u8bbf\u95ee\u6765\u6e90IP<br \/>\n\"127.0.0.1\";<br \/>\n\"192.168.0.0\"\/24;<br \/>\n}<br \/>\nsub vcl_recv {<br \/>\nif (req.request == \"GET\" &amp;&amp; req.http.cookie) { # \u5e26cookie\u9996\u90e8\u7684GET\u8bf7\u6c42\u4e5f\u7f13\u5b58<br \/>\nreturn(hash);<br \/>\n}<br \/>\nif (req.url ~ \"test.html\") { # test.html\u6587\u4ef6\u7981\u6b62\u7f13\u5b58<br \/>\nreturn(pass);<br \/>\n}<br \/>\nif (req.request == \"PURGE\") { # PURGE\u8bf7\u6c42\u7684\u5904\u7406<br \/>\nif (!client.ip ~ purgers) {<br \/>\nreturn(synth(405,\"Method not allowed\"));<br \/>\n}<br \/>\nreturn(hash);<br \/>\n}<br \/>\nif (req.http.X-Forward-For) { # \u4e3a\u53d1\u5f80\u540e\u7aef\u4e3b\u673a\u7684\u8bf7\u6c42\u6dfb\u52a0X-Forward-For\u9996\u90e8<br \/>\nset req.http.X-Forward-For = req.http.X-Forward-For + \",\" + client.ip;<br \/>\n} else {<br \/>\nset req.http.X-Forward-For = client.ip;<br \/>\n}<br \/>\nif (req.http.host ~ \"(?i)^(www.)?lnmmp.com$\") { # \u6839\u636e\u4e0d\u540c\u7684\u8bbf\u95ee\u57df\u540d\uff0c\u5206\u53d1\u81f3\u4e0d\u540c\u7684\u540e\u7aef\u4e3b\u673a\u7ec4<br \/>\nset req.http.host = \"www.lnmmp.com\";<br \/>\nset req.backend_hint = web_cluster.backend();<br \/>\n} elsif (req.http.host ~ \"(?i)^images.lnmmp.com$\") {<br \/>\nset req.backend_hint = img_cluster.backend();<br \/>\n}<br \/>\nreturn(hash);<br \/>\n}<br \/>\nsub vcl_hit { # PURGE\u8bf7\u6c42\u7684\u5904\u7406<br \/>\nif (req.request == \"PURGE\") {<br \/>\npurge;<br \/>\nreturn(synth(200,\"Purged\"));<br \/>\n}<br \/>\n}<br \/>\nsub vcl_miss { # PURGE\u8bf7\u6c42\u7684\u5904\u7406<br \/>\nif (req.request == \"PURGE\") {<br \/>\npurge;<br \/>\nreturn(synth(404,\"Not in cache\"));<br \/>\n}<br \/>\n}<br \/>\nsub vcl_pass { # PURGE\u8bf7\u6c42\u7684\u5904\u7406<br \/>\nif (req.request == \"PURGE\") {<br \/>\nreturn(synth(502,\"PURGE on a passed object\"));<br \/>\n}<br \/>\n}<br \/>\nsub vcl_backend_response { # \u81ea\u5b9a\u4e49\u7f13\u5b58\u6587\u4ef6\u7684\u7f13\u5b58\u65f6\u957f\uff0c\u5373TTL\u503c<br \/>\nif (req.url ~ \"\\.(jpg|jpeg|gif|png)$\") {<br \/>\nset beresp.ttl = 7200s;<br \/>\n}<br \/>\nif (req.url ~ \"\\.(html|css|js)$\") {<br \/>\nset beresp.ttl = 1200s;<br \/>\n}<br \/>\nif (beresp.http.Set-Cookie) { # \u5b9a\u4e49\u5e26Set-Cookie\u9996\u90e8\u7684\u540e\u7aef\u54cd\u5e94\u4e0d\u7f13\u5b58\uff0c\u76f4\u63a5\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef<br \/>\nreturn(deliver);<br \/>\n}<br \/>\n}<br \/>\nsub vcl_deliver {<br \/>\nif (obj.hits &gt; 0) { # \u4e3a\u54cd\u5e94\u6dfb\u52a0X-Cache\u9996\u90e8\uff0c\u663e\u793a\u7f13\u5b58\u662f\u5426\u547d\u4e2d<br \/>\nset resp.http.X-Cache = \"HIT from \" + server.ip;<br \/>\n} else {<br \/>\nset resp.http.X-Cache = \"MISS\";<br \/>\n}<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<pre class=\"brush:perl; toolbar: true; auto-links: true;\"># This is a basic VCL configuration file for varnish.  See the vcl(7)\r\n# man page for details on VCL syntax and semantics.\r\n# \r\n# Default backend definition.  Set this to point to your content\r\n# server.\r\n# \r\n\r\nprobe healthcheck {\r\n   .url = \"\/\";\r\n   .interval = 60s;\r\n   .timeout = 0.3s;\r\n   .wind...<\/pre>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/341\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[284],"tags":[38],"class_list":["post-341","post","type-post","status-publish","format-standard","hentry","category-webserver","tag-varnish"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}