{"id":211,"date":"2010-06-20T02:24:28","date_gmt":"2010-06-19T17:24:28","guid":{"rendered":"http:\/\/blog.xcir.net\/?p=211"},"modified":"2010-06-21T00:57:23","modified_gmt":"2010-06-20T15:57:23","slug":"%e3%83%9e%e3%83%ab%e3%83%81%e3%83%89%e3%83%a1%e3%82%a4%e3%83%b3%e7%92%b0%e5%a2%83%e3%81%a7http%e7%b5%8c%e7%94%b1%e3%81%a7purge%e3%82%92%e8%a1%8c%e3%81%86vcl%e8%a8%ad%e5%ae%9a%e3%81%a8%e3%82%b9","status":"publish","type":"post","link":"https:\/\/blog.xcir.net\/?p=211","title":{"rendered":"\u30de\u30eb\u30c1\u30c9\u30e1\u30a4\u30f3\u74b0\u5883\u3067HTTP\u7d4c\u7531\u3067Purge\u3092\u884c\u3046VCL\u8a2d\u5b9a\u3068\u30b9\u30af\u30ea\u30d7\u30c8"},"content":{"rendered":"<p>\u50d5\u306e\u30b5\u30a4\u30c8\u307f\u305f\u3044\u306b\u8907\u6570\u306e\u30c9\u30e1\u30a4\u30f3\uff08blog.xcir.net\/xcir.net\/wiki.xcir.net)\u7ba1\u7406\u3057\u3066\u3044\u308b\u3051\u3069Varnish\u30b5\u30fc\u30d0\u306f\u4e00\u3064\u306e\u5834\u5408<br \/>\n<a target='_blank' href=\"http:\/\/varnish-cache.org\/wiki\/VCLExamplePurging\">\u516c\u5f0f\u30b5\u30a4\u30c8\u306e\u8a2d\u5b9a\u4f8b<\/a><\/p>\n<pre class=\"brush: plain; \">\n\r\nacl purge {\r\n        &quot;localhost&quot;;\r\n        &quot;192.0.2.14&quot;;\r\n}\r\n\r\nsub vcl_recv {\r\n        if (req.request == &quot;PURGE&quot;) {\r\n                if (!client.ip ~ purge) {\r\n                        error 405 &quot;Not allowed.&quot;;\r\n                }\r\n                purge(&quot;req.url ~ &quot; req.url);\r\n        }\r\n}\r\n\n<\/pre>\n<p>\u3060\u3068\u9055\u3046\u30c9\u30e1\u30a4\u30f3\u3067\u3082\u540c\u3058URL\u3067\u3042\u308c\u3070\u6d88\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n\u30c9\u30e1\u30a4\u30f3\u3092\u6307\u5b9a\u3057\u3066\u5b89\u5168\u306b\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u30d1\u30fc\u30b8\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<br \/>\n<!--more--><\/p>\n<h4>VCL<\/h4>\n<pre class=\"brush: plain; \">\n\r\nacl purge {\r\n\t\t\/\/\u30d1\u30fc\u30b8\u3092\u8a31\u53ef\u3059\u308b\u30db\u30b9\u30c8\u4e00\u89a7\r\n\t\t&quot;localhost&quot;;\r\n\t\t&quot;192.168.1.0&quot;\/24;\r\n}\r\nsub vcl_recv {\r\n\t\/\/PURGE\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u5834\u5408PURGE\u51e6\u7406\u3092\u884c\u3046\r\n\tif (req.request == &quot;PURGE&quot;) {\r\n\t\t\/\/\u30a2\u30af\u30bb\u30b9\u3057\u3066\u304d\u305fIP\u30a2\u30c9\u30ec\u30b9\u304cpurge\u306eACL\u3068\u4e00\u81f4\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306f\u62d2\u5426\u3057\u3066405 Not allowed.\u3092\u8fd4\u5374\r\n\t\tif (!client.ip ~ purge) {\r\n\t\t\terror 405 &quot;Not allowed.&quot;;\r\n\t\t}\r\n\t\t\/\/HOST\u304c\u5b8c\u5168\u4e00\u81f4\u304b\u3064URL\u304c\u4e00\u81f4\u3059\u308b\uff08\u6b63\u898f\u8868\u73fe\u3067\u8a55\u4fa1\uff09URL\u306b\u3064\u3044\u3066\u30d1\u30fc\u30b8\u3059\u308b\r\n\t\tpurge(&quot;req.http.host == &quot; req.http.host &quot; &amp;&amp; req.url ~ &quot;req.url );\r\n\t\t\/\/200\u3092 Purged.\u3092\u8fd4\u5374\r\n\t\terror 200 &quot;Purged.&quot;;\r\n\t}\r\n}\r\n\n<\/pre>\n<h4>\u30ea\u30af\u30a8\u30b9\u30c8\u65b9\u6cd5<\/h4>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30ea\u30af\u30a8\u30b9\u30c8\u3092Varnish\u30b5\u30fc\u30d0\u306b\u6295\u3052\u307e\u3059<\/p>\n<pre class=\"brush: plain; \">\n\r\nPURGE \/img.jpg HTTP\/1.0\r\nHost: blog.xcir.net\r\n\n<\/pre>\n<p>\u6b63\u898f\u8868\u73fe\u3082\u4f7f\u3048\u307e\u3059\u306e\u3067<\/p>\n<pre class=\"brush: plain; \">\n\r\nPURGE .* HTTP\/1.0\r\nHost: blog.xcir.net\r\n\n<\/pre>\n<p>\u3082OK\u3067\u3059\u3002\u3059\u3079\u3066\u6d88\u3048\u307e\u3059\u304c<\/p>\n<p>\u7279\u306b\u91cd\u8981\u306a\u306e\u304cACL\u306e\u8a2d\u5b9a\u3067\u3059\u3002<br \/>\n\u304d\u3061\u3093\u3068\u8a31\u53efIP\u3092\u8a2d\u5b9a\u3057\u306a\u3044\u3068\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u304b\u3089\u81ea\u7531\u306b\u30d1\u30fc\u30b8\u3055\u308c\u3061\u3083\u3044\u307e\u3059\u3002<\/p>\n<p>\u8907\u6570\u306e\u30b5\u30fc\u30d0\u3092\u7ba1\u7406\u3057\u3066\u3044\u308b\u3068\u30b9\u30af\u30ea\u30d7\u30c8\u304c\u6b32\u3057\u304f\u306a\u308b\u306e\u3067PHP\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3082\u4f5c\u308a\u307e\u3057\u305f\u3002<\/p>\n<h4>\u30b9\u30af\u30ea\u30d7\u30c8<\/h4>\n<pre class=\"brush: php; \">\n\r\nclass Varnish_HTTP_Purge{\r\n\tstatic function purge($varnish,$url,$port=80){\r\n\t\t$all=true;\r\n\t\tif(!is_array($varnish) &amp;&amp; strlen($varnish)&gt;0){\r\n\t\t\t$varnish=array($varnish);\r\n\t\t}\r\n\t\tif(!is_array($url) &amp;&amp; strlen($url)&gt;0){\r\n\t\t\t$url=array($url);\r\n\t\t}\r\n\t\t$ret=array();\r\n\t\tforeach($varnish as $v){\r\n\t\t\tforeach($url as $u){\r\n\t\t\t\t$ret[&quot;$v-&gt;$u&quot;]=self::_purge($v,$u,$port);\r\n\t\t\t\t$all &amp;=$ret[&quot;$v-&gt;$u&quot;];\r\n\t\t\t}\r\n\t\t}\r\n\t\t$ret[&#039;all&#039;]=$all;\r\n\t\treturn $ret;\r\n\t}\r\n\r\n\tprivate static function _purge($varnish,$url,$port=80){\r\n\t\t$url=preg_replace(&#039;@http:\/\/@&#039;,,$url);\r\n\t\t$p=explode(&#039;\/&#039;,$url,2);\r\n\t\tif(count($p)==1){\r\n\t\t\t$p[]=;\r\n\t\t}\r\n\t\t$p[1]=&#039;\/&#039;.$p[1];\r\n\t\t$socket = fsockopen($varnish, $port, $errno, $errstr);\r\n\t\tif (!$socket) {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\t$reqheader=&quot;PURGE {$p[1]} HTTP\/1.0\\r\\n&quot;.\r\n\t\t\t&quot;Host:{$p[0]}\\r\\n&quot;.\r\n\t\t\t&quot;\\r\\n&quot;;\r\n\t\tfputs($socket, $reqheader);\r\n\t\t$body=&quot;&quot;;\r\n\t\twhile (!feof($socket)) {\r\n\t\t\t$body .= fgets($socket, 4096);\r\n\t\t}\r\n\t\tfclose($socket);\r\n\t\tif(stristr($body,&#039;HTTP\/1.1 200 Purged.&#039;)){return true;}\r\n\t\treturn false;\r\n\t}\r\n}\r\n\n<\/pre>\n<p>\u4f7f\u3044\u65b9<br \/>\nVarnish_HTTP_Purge::purge(<strong>\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6295\u3052\u305f\u3044Varnish\u30b5\u30fc\u30d0<\/strong>,<strong>\u30d1\u30fc\u30b8\u3057\u305f\u3044URL<\/strong>);<\/p>\n<pre class=\"brush: php; \">\n\r\nVarnish_HTTP_Purge::purge(&quot;192.168.1.1&quot;,&quot;http:\/\/blog.xcir.net\/test.jpg&quot;);\r\n\n<\/pre>\n<p>\u30b5\u30fc\u30d0\u3068URL\u306f\u914d\u5217\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\u3067<\/p>\n<pre class=\"brush: php; \">\n\r\nVarnish_HTTP_Purge::purge(array(&quot;192.168.1.1&quot;,&quot;192.168.1.2&quot;),array(&quot;http:\/\/blog.xcir.net\/test.jpg&quot;,&quot;http:\/\/blog.xcir.net\/test.jpg2&quot;));\r\n\n<\/pre>\n<p>\u3068\u3059\u308c\u3070OK\u3067\u3059<\/p>\n<p>\u7d50\u679c\u306f\u914d\u5217\u3067\u8fd4\u5374\u3057\u307e\u3059\u3002<br \/>\n\u8981\u6c42\u3057\u305f\u30db\u30b9\u30c8->URL\u3067\u30ad\u30fc\u3092\u4f5c\u308a\u305d\u308c\u305e\u308c\u3067bool\u3067\u8fd4\u5374\u3057\u307e\u3059\u3002<br \/>\n\u307e\u305f\u5168\u4f53\u3067\u6210\u529f\u3057\u305f\u304b\u3069\u3046\u304b\u306fall\u30ad\u30fc\u306b\u683c\u7d0d\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u8fd4\u5374\u5024\u3092var_dump\u3059\u308b\u3068\u3053\u3093\u306a\u611f\u3058\u3067\u3059<\/p>\n<pre class=\"brush: plain; \">\n\r\n\r\narray(5) {\r\n  [&quot;localhost-&gt;http:\/\/blog.xcir.net\/test.jpg&quot;]=&gt;\r\n  bool(false)\r\n  [&quot;localhost-&gt;http:\/\/blog.xcir.net\/test.jpg2&quot;]=&gt;\r\n  bool(false)\r\n  [&quot;192.168.1.123-&gt;http:\/\/blog.xcir.net\/test.jpg&quot;]=&gt;\r\n  bool(true)\r\n  [&quot;192.168.1.123-&gt;http:\/\/blog.xcir.net\/test.jpg2&quot;]=&gt;\r\n  bool(true)\r\n  [&quot;all&quot;]=&gt;\r\n  int(0)\r\n}\r\n\n<\/pre>\n<p>wiki\u306b\u3082\u307e\u3068\u3081\u307e\u3057\u305f\u3053\u3063\u3061\u306b\u306f\u66f8\u3044\u3066\u306a\u3044Perl\u7248\u306f\u4ee5\u4e0b\u3092\u3069\u3046\u305e<br \/>\n<a target='_blank' href=\"http:\/\/wiki.xcir.net\/index.php?title=HTTP%E7%B5%8C%E7%94%B1%E3%81%A7%E3%81%AE%E3%83%91%E3%83%BC%E3%82%B8(%E3%83%9E%E3%83%AB%E3%83%81%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E5%AF%BE%E5%BF%9C)\">HTTP\u7d4c\u7531\u3067\u306e\u30d1\u30fc\u30b8(\u30de\u30eb\u30c1\u30c9\u30e1\u30a4\u30f3\u5bfe\u5fdc)<\/a><br \/>\n<a target='_blank' href=\"http:\/\/wiki.xcir.net\/index.php?title=Varnish%E3%81%AE%E3%83%91%E3%83%BC%E3%82%B8%E3%82%92%E8%A1%8C%E3%81%86%E3%82%AF%E3%83%A9%E3%82%B9(PHP)\">Varnish\u306e\u30d1\u30fc\u30b8\u3092\u884c\u3046\u30af\u30e9\u30b9(PHP)<\/a><br \/>\n<a target='_blank' href=\"http:\/\/wiki.xcir.net\/index.php?title=Varnish%E3%81%AE%E3%83%91%E3%83%BC%E3%82%B8%E3%82%92%E8%A1%8C%E3%81%86%E3%83%91%E3%83%83%E3%82%B1%E3%83%BC%E3%82%B8(Perl)\">Varnish\u306e\u30d1\u30fc\u30b8\u3092\u884c\u3046\u30d1\u30c3\u30b1\u30fc\u30b8(Perl)<\/a><\/p>\n<div class='wp_social_bookmarking_light'>        <div class=\"wsbl_hatena\"><a href='\/\/b.hatena.ne.jp\/add?mode=confirm&url=https%3A%2F%2Fblog.xcir.net%2F%3Fp%3D211&title=%E3%83%9E%E3%83%AB%E3%83%81%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E7%92%B0%E5%A2%83%E3%81%A7HTTP%E7%B5%8C%E7%94%B1%E3%81%A7Purge%E3%82%92%E8%A1%8C%E3%81%86VCL%E8%A8%AD%E5%AE%9A%E3%81%A8%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88' title='\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/blog.xcir.net\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/hatena.gif' alt='\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0' title='\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0' width='16' height='12' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_hatena_users\"><a href='\/\/b.hatena.ne.jp\/entry\/https:\/\/blog.xcir.net\/?p=211' title='\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af - \u30de\u30eb\u30c1\u30c9\u30e1\u30a4\u30f3\u74b0\u5883\u3067HTTP\u7d4c\u7531\u3067Purge\u3092\u884c\u3046VCL\u8a2d\u5b9a\u3068\u30b9\u30af\u30ea\u30d7\u30c8' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='\/\/b.hatena.ne.jp\/entry\/image\/https:\/\/blog.xcir.net\/?p=211' alt='\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af - \u30de\u30eb\u30c1\u30c9\u30e1\u30a4\u30f3\u74b0\u5883\u3067HTTP\u7d4c\u7531\u3067Purge\u3092\u884c\u3046VCL\u8a2d\u5b9a\u3068\u30b9\u30af\u30ea\u30d7\u30c8' title='\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af - \u30de\u30eb\u30c1\u30c9\u30e1\u30a4\u30f3\u74b0\u5883\u3067HTTP\u7d4c\u7531\u3067Purge\u3092\u884c\u3046VCL\u8a2d\u5b9a\u3068\u30b9\u30af\u30ea\u30d7\u30c8'   class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_facebook\"><a href='http:\/\/www.facebook.com\/share.php?u=https%3A%2F%2Fblog.xcir.net%2F%3Fp%3D211&t=%E3%83%9E%E3%83%AB%E3%83%81%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E7%92%B0%E5%A2%83%E3%81%A7HTTP%E7%B5%8C%E7%94%B1%E3%81%A7Purge%E3%82%92%E8%A1%8C%E3%81%86VCL%E8%A8%AD%E5%AE%9A%E3%81%A8%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88' title='Facebook \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/blog.xcir.net\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/facebook.png' alt='Facebook \u306b\u30b7\u30a7\u30a2' title='Facebook \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_linkedin\"><a href='\/\/www.linkedin.com\/shareArticle?mini=true&url=https%3A%2F%2Fblog.xcir.net%2F%3Fp%3D211&title=%E3%83%9E%E3%83%AB%E3%83%81%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E7%92%B0%E5%A2%83%E3%81%A7HTTP%E7%B5%8C%E7%94%B1%E3%81%A7Purge%E3%82%92%E8%A1%8C%E3%81%86VCL%E8%A8%AD%E5%AE%9A%E3%81%A8%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88' title='LinkedIn \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/blog.xcir.net\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/linkedin.png' alt='LinkedIn \u306b\u30b7\u30a7\u30a2' title='LinkedIn \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/blog.xcir.net\/?p=211\" data-text=\"\u30de\u30eb\u30c1\u30c9\u30e1\u30a4\u30f3\u74b0\u5883\u3067HTTP\u7d4c\u7531\u3067Purge\u3092\u884c\u3046VCL\u8a2d\u5b9a\u3068\u30b9\u30af\u30ea\u30d7\u30c8\" data-lang=\"ja\">Tweet<\/a><\/div><\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>\u50d5\u306e\u30b5\u30a4\u30c8\u307f\u305f\u3044\u306b\u8907\u6570\u306e\u30c9\u30e1\u30a4\u30f3\uff08blog.xcir.net\/xcir.net\/wiki.xcir.net)\u7ba1\u7406\u3057\u3066\u3044\u308b\u3051\u3069Varnish\u30b5\u30fc\u30d0\u306f\u4e00\u3064\u306e\u5834\u5408 \u516c\u5f0f\u30b5\u30a4\u30c8\u306e\u8a2d\u5b9a\u4f8b acl purge { &quot;loc <a href='https:\/\/blog.xcir.net\/?p=211' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5,14],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-3","tag-varnish","tag-varnish2-1-2","category-3-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=211"}],"version-history":[{"count":17,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}