{"id":1389,"date":"2016-09-29T09:50:52","date_gmt":"2016-09-29T09:50:52","guid":{"rendered":"http:\/\/cloudkul.com\/blog\/?p=1389"},"modified":"2023-10-20T12:41:51","modified_gmt":"2023-10-20T12:41:51","slug":"purge-varnish-cache-remotely-using-cloudflare","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/","title":{"rendered":"How to purge varnish cache remotely  when using cloudflare"},"content":{"rendered":"<p style=\"text-align: justify;\">Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.<\/p>\n<p style=\"text-align: justify;\">Whenever someone requests something from a website, varnish fetches the available contents from its cache to provide a faster response to the request.<\/p>\n<p style=\"text-align: justify;\">Whenever a developer makes any changes to the website, it becomes mandatory to clear the cache to reflect the desired changes. In order to clear the cache, we need to restart the\u00a0service.<\/p>\n<p style=\"text-align: justify;\">However, this is not a good practice to log in to the server and execute the command every time.<\/p>\n<p style=\"text-align: justify;\">So, to ease the process,\u00a0 varnish provides a few methods to purge the cache. We can send a &#8220;BAN&#8221; or &#8220;PURGE&#8221; header or request a particular URL for purging the cache.<\/p>\n<p style=\"text-align: justify;\">In this tutorial, we will show you how to allow purging from a particular IP.<\/p>\n<p><strong>Varnish Configurations<\/strong><\/p>\n<p style=\"text-align: justify;\">In the default varnish configuration, we define IP addresses inside the acl. If the sender&#8217;s IP address matches with the IP address mentioned inside the acl, then they can purge the cache.<\/p>\n<p style=\"text-align: justify;\">But the problem is when you are using Cloudflare, varnish does not get the original IP of the sender. Instead, it gets the Cloudflare&#8217;s IP address using which the purging can not be done.<\/p>\n<p style=\"text-align: justify;\">So we need to define the original IP of the sender in the varnish.<\/p>\n<p style=\"text-align: justify;\"><strong>First, you need to comment or delete the default purging configuration lines inside vcl_recv, <span style=\"color: initial;\">which is located in\/etc\/varnish\/default.vcl<\/span><\/strong><\/p>\n<div class=\"page\" data-page-number=\"1\" data-loaded=\"true\">\n<div class=\"textLayer\">\n<div class=\"endOfContent\"><\/div>\n<\/div>\n<\/div>\n<div class=\"page\" data-page-number=\"2\" data-loaded=\"true\">\n<div class=\"canvasWrapper\"><\/div>\n<div class=\"textLayer\"><\/div>\n<\/div>\n<pre class=\"lang:default decode:true\">#acl purge {\n#    \"localhost\";\n#    \"127.0.0.1\";\n#}\n\n # Allow purging from ACL\n  #if (req.method == \"PURGE\") {\n    # If not allowed then a error 405 is returned\n#    if (!client.ip ~ purge) {\n#      return(synth(405, \"This IP is not allowed to send PURGE requests.\"));\n#    }\n    # If allowed, do a cache_lookup -&gt; vlc_hit() or vlc_miss()\n#    return (purge);\n#  } # These lines are different for varnish 3 and varnish 4\n<\/pre>\n<p style=\"text-align: justify;\"><strong>For varnish 6, add the following lines inside vcl_recv<\/strong><\/p>\n<pre class=\"lang:default decode:true\"> if (req.restarts == 0) {\n                if (req.http.X-Forwarded-For) {\n        set req.http.X-Forwarded-For = req.http.X-Forwarded-For + \", \" + client.ip;\n                } else {\n                        set req.http.X-Forwarded-For = client.ip;\n                }\n        }\nif (req.request == \"PURGE\" || req.url == \"\/purge\") {\n      # Replace these IP with your IP\n      if (req.http.X-Forwarded-For !~ \"(209.152.41.21|105.45.120.37)\") {\n         return(synth(405, \"This IP is not allowed to send PURGE requests.\"));\n      }\n     ban(\"req.url ~ \/\");\n     return (purge);\n}<\/pre>\n<pre class=\"lang:default decode:true \"><\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>Suppose your site is <strong>varnish.example.com<\/strong>. To clear the cache, you have to hit the URL <strong>varnish.example.com\/purge<\/strong>.<\/p>\n<p>If you wish to use any specific term other than &#8216;<strong>purge&#8217; <\/strong>to clear the\u00a0cache, then you can do so. You just need to update that word in the varnish configuration file.<\/p>\n<p>For example, if you wish to use the &#8216;Clear&#8217; word to purge the cache, then you need to update the code like this &#8211;<\/p>\n<p>if (req.request == &#8220;BAN&#8221; || req.url == &#8220;\/clear&#8221;) {<\/p>\n<p><strong>To purge the cache from the command line you need to type the following command<\/strong><\/p>\n<pre class=\"lang:default decode:true \">curl -X PURGE varnish.example.com # For varnish 6<\/pre>\n<p style=\"text-align: justify;\">Here in the blog, I am allowing two IP addresses to allow purging.<\/p>\n<p style=\"text-align: justify;\">If you wish to add more addresses, then you can add the IP address in the format &#8220;<strong>(your IP | your IP | your IP)<\/strong>&#8220;.<\/p>\n<p>if (req.http.X-Forwarded-For !~ &#8220;(IP-address|IP-address|IP-address)&#8221;) {<\/p>\n<p style=\"text-align: justify;\">Now you can purge the cache remotely and allow only particular IP addresses for purging.<\/p>\n<h2>Need Support?<\/h2>\n<p>Thank You for reading this Blog!<\/p>\n<p>For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at\u00a0<a href=\"https:\/\/webkul.uvdesk.com\/en\/\">https:\/\/webkul.uvdesk.com\/en\/<\/a>.<\/p>\n<p>You may also visit our Odoo development services and quality\u00a0<a href=\"https:\/\/store.webkul.com\/index.php\/Odoo.html\">Odoo Extensions<\/a>.<\/p>\n<p><strong>For further help or queries, please\u00a0<a href=\"https:\/\/cloudkul.com\/contact\/\">contact<\/a>\u00a0us or raise a\u00a0<a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\">ticket<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":8,"featured_media":1402,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,87,341,340],"tags":[159,150,849,868,869,68],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to purge varnish cache remotely when using cloudflare - Cloudkul<\/title>\n<meta name=\"description\" content=\"Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to purge varnish cache remotely when using cloudflare - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-29T09:50:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-20T12:41:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2016\/09\/Allow-Varnish-Cache-Purging-Remotely-When-Using-Cloudflare.png\" \/>\n\t<meta property=\"og:image:width\" content=\"848\" \/>\n\t<meta property=\"og:image:height\" content=\"422\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"suranjan horrow\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\",\"name\":\"How to purge varnish cache remotely when using cloudflare - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2016-09-29T09:50:52+00:00\",\"dateModified\":\"2023-10-20T12:41:51+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/a653191c790a89b07d4b4aaefc3e2809\"},\"description\":\"Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to purge varnish cache remotely when using cloudflare\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\",\"url\":\"https:\/\/cloudkul.com\/blog\/\",\"name\":\"Cloudkul\",\"description\":\"Host your eCommerce Store on AWS with Optimized Performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudkul.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/a653191c790a89b07d4b4aaefc3e2809\",\"name\":\"suranjan horrow\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dfd7f87bc7d62c1426e1f4c07653ff00?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dfd7f87bc7d62c1426e1f4c07653ff00?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"suranjan horrow\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/cloudkul.com\/blog\/author\/suranjan-horrow869\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to purge varnish cache remotely when using cloudflare - Cloudkul","description":"Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/","og_locale":"en_US","og_type":"article","og_title":"How to purge varnish cache remotely when using cloudflare - Cloudkul","og_description":"Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.","og_url":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/","og_site_name":"Cloudkul","article_published_time":"2016-09-29T09:50:52+00:00","article_modified_time":"2023-10-20T12:41:51+00:00","og_image":[{"width":848,"height":422,"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2016\/09\/Allow-Varnish-Cache-Purging-Remotely-When-Using-Cloudflare.png","type":"image\/png"}],"author":"suranjan horrow","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/","url":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/","name":"How to purge varnish cache remotely when using cloudflare - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2016-09-29T09:50:52+00:00","dateModified":"2023-10-20T12:41:51+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/a653191c790a89b07d4b4aaefc3e2809"},"description":"Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.","breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to purge varnish cache remotely when using cloudflare"}]},{"@type":"WebSite","@id":"https:\/\/cloudkul.com\/blog\/#website","url":"https:\/\/cloudkul.com\/blog\/","name":"Cloudkul","description":"Host your eCommerce Store on AWS with Optimized Performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudkul.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/a653191c790a89b07d4b4aaefc3e2809","name":"suranjan horrow","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/dfd7f87bc7d62c1426e1f4c07653ff00?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dfd7f87bc7d62c1426e1f4c07653ff00?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"suranjan horrow"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/cloudkul.com\/blog\/author\/suranjan-horrow869\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/1389"}],"collection":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=1389"}],"version-history":[{"count":38,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/1389\/revisions"}],"predecessor-version":[{"id":17120,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/1389\/revisions\/17120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media\/1402"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=1389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=1389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}