{"id":17487,"date":"2024-04-04T07:45:39","date_gmt":"2024-04-04T07:45:39","guid":{"rendered":"https:\/\/cloudkul.com\/blog\/?p=17487"},"modified":"2024-10-22T12:23:03","modified_gmt":"2024-10-22T12:23:03","slug":"impact-of-varnish-on-wordpress","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/","title":{"rendered":"Impact of Varnish on WordPress"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Varnish?<\/strong><\/h2>\n\n\n\n<p>Varnish is a software that helps websites load faster by storing a copy of the website&#8217;s content in a special place called a cache. <\/p>\n\n\n\n<p><a href=\"https:\/\/cloudkul.com\/blog\/purge-varnish-cache-remotely-using-cloudflare\/\">Varnish Cache <\/a>is a web application accelerator also known as a caching HTTP reverse proxy. However, You can install it in front of your web server. <\/p>\n\n\n\n<p>When someone visits the website, instead of fetching all the information from scratch, varnish quickly retrieves the stored copy, making the website load much faster.<\/p>\n\n\n\n<p>In this blog, we will learn how to implement Varnish and Apache.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Benefits of using varnish<\/strong><\/h2>\n\n\n\n<p><strong>Speed<\/strong>: Websites load faster because varnish serves up cached copies of web pages instead of generating them from scratch every time someone visits.<\/p>\n\n\n\n<p><strong>Reduced Server Load:<\/strong> Since varnish reduces the number of requests to the main server, it helps to lower the burden on the server, which can handle more visitors without slowing down.<\/p>\n\n\n\n<p><strong>Better User Experience:<\/strong> Faster loading times mean visitors spend less time waiting for pages to load, leading to a better overall experience and higher satisfaction.<\/p>\n\n\n\n<p><strong>Scalability:<\/strong> Varnish can handle a large number of simultaneous requests, making it suitable for high-traffic websites.<\/p>\n\n\n\n<p><strong>Cost Savings:<\/strong> By reducing server load and increasing efficiency, varnish can help save on hosting costs.<\/p>\n\n\n\n<p>In simple terms, varnish is like a turbocharger for websites, making them faster and more efficient, which benefits both website owners and visitors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to set up\/install Varnish?<\/strong><\/h2>\n\n\n\n<p>It completely depends on the OS of the server. Based on the server OS you will run the command to install.<\/p>\n\n\n\n<p><strong>Note &#8211; <\/strong>Here we are using the <a href=\"https:\/\/cloudkul.com\/blog\/how-to-install-turn-stun-server-on-aws-ubuntu-20-04\/\">Ubuntu 20.04 server<\/a>.<\/p>\n\n\n\n<p>Please run the below commands:<\/p>\n\n\n\n<p>This command updates the package list to get information on the latest available packages.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt-get update<\/pre>\n\n\n\n<p>This command will install the further dependencies to configure the package repository.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt-get install debian-archive-keyring curl gnupg apt-transport-https<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Here&#8217;s a breakdown of what each of these packages does and why they might be required:<\/strong><\/h3>\n\n\n\n<p><strong>&gt; Debian-archive-keyring: <\/strong>This package contains the GnuPG archive keys of the Debian archive. These keys are used to verify the authenticity of Debian packages during installation. <\/p>\n\n\n\n<p>Installing this package ensures that your system can authenticate packages from the Debian repository.<\/p>\n\n\n\n<p><strong>&gt; Curl: <\/strong>Curl is a command-line tool and library for transferring data with URLs. However, it supports various protocols, including HTTP, HTTPS, FTP, and more.<\/p>\n\n\n\n<p>It&#8217;s commonly used for downloading files or interacting with web services from the command line.<\/p>\n\n\n\n<p><strong>&gt; gnupg:<\/strong> GnuPG (GNU Privacy Guard) is a complete and free implementation of the OpenPGP standard. It&#8217;s used for encryption and signing of data and communication. <\/p>\n\n\n\n<p>It&#8217;s likely included here because it&#8217;s a dependency for other packages or because it&#8217;s commonly used for secure communication or package verification.<\/p>\n\n\n\n<p><strong>&gt; apt-transport-https:<\/strong> This package allows apt-get to retrieve and download packages over HTTPS. <\/p>\n\n\n\n<p>It&#8217;s essential for fetching packages from repositories that use HTTPS, which is increasingly becoming the standard for package repositories to ensure data integrity and security during transmission.<\/p>\n\n\n\n<p>Further, the next command will import the GPG key into the package manager configuration:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -s -L https:\/\/packagecloud.io\/varnishcache\/varnish60lts\/gpgkey | sudo apt-key add -<\/pre>\n\n\n\n<p>This command retrieves a GNU Privacy Guard key from the specified URL (<a href=\"https:\/\/packagecloud.io\/varnishcache\/varnish60lts\/gpgkey\">https:\/\/packagecloud.io\/varnishcache\/varnish60lts\/gpgkey<\/a>).<\/p>\n\n\n\n<p>And then adds it to the list of trusted keys used by the APT package manager on a Debian-based Linux system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Here&#8217;s a breakdown of the components:<\/strong><\/h3>\n\n\n\n<p><strong>&gt; curl: <\/strong>This is the command-line tool used for transferring data with URLs. In this command, it&#8217;s fetching the GPG key from the specified URL.<\/p>\n\n\n\n<p><strong>&gt; -s:<\/strong> This flag makes the curl operate in silent mode, meaning it doesn&#8217;t show progress or error messages.<\/p>\n\n\n\n<p><strong>&gt; -L: <\/strong>This flag tells the curl to follow any HTTP redirects, so it will automatically go to the final destination URL if there are any redirections.<\/p>\n\n\n\n<p>You can retrieve the GPG key from the URL &#8211; <a href=\"https:\/\/packagecloud.io\/varnishcache\/varnish60lts\/gpgkey\">https:\/\/packagecloud.io\/varnishcache\/varnish60lts\/gpgkey<\/a><\/p>\n\n\n\n<p><strong>&gt;<\/strong> |: This is the pipe operator, which redirects the output of the curl command (the GPG key) to the next command (sudo apt-key add -).<\/p>\n\n\n\n<p><strong>&gt;<\/strong> sudo apt-key add -: This command adds the GPG key received from curl to the list of trusted keys used by APT. sudo is used to run the command with administrative privileges.<\/p>\n\n\n\n<p>And finally, we have to update the package list once again. This will ensure the Package-cloud repository is included:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt-get update<\/pre>\n\n\n\n<p>Now that the repositories are added and the right repository preferences are configured, you can install Varnish by running the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt-get install varnish<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to configure Varnish?<\/strong><\/h2>\n\n\n\n<p>Varnish usually listens for requests on port 6081, but we&#8217;re configuring Varnish to listen on port 80, We are also adjusting how much Varnish can store in memory by making changes to its unit file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">vim \/lib\/systemd\/system\/varnish.service<\/pre>\n\n\n\n<p>Default block:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ExecStart=\/usr\/sbin\/varnishd \\\n\t  -a :6081 \\\n\t  -a localhost:6082,PROXY \\\n\t  -p feature=+http2 \\\n\t  -f \/etc\/varnish\/default.vcl \\\n\t  -s malloc,256m<\/pre>\n\n\n\n<p>After doing the required changes our block will look like this:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ExecStart=\/usr\/sbin\/varnishd \\\n          -j unix,user=vcache \\\n          -F \\\n          -a :80 \\\n          -T localhost:6082 \\\n          -p feature=+http2 \\\n          -f \/etc\/varnish\/default.vcl \\\n          -S \/etc\/varnish\/secret \\\n          -s malloc,2g<\/pre>\n\n\n\n<p>This block is a configuration for starting the Varnish Cache daemon (varnishd). <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Let&#8217;s break down each option:<\/h3>\n\n\n\n<p><strong>&gt; ExecStart: <\/strong>This is a directive typically found in a systemd service unit file (*.service). It specifies the command to execute when starting the service.<\/p>\n\n\n\n<p><strong>&gt; \/usr\/sbin\/varnishd:<\/strong> This is the path to the Varnish Cache daemon executable.<\/p>\n\n\n\n<p><strong>&gt; -j unix,user=vcache: <\/strong>This option tells Varnish to create a shared memory file (VSM) using Unix domain sockets with permissions granted to the vcache user. <\/p>\n\n\n\n<p>We are using this for inter-process communication.<\/p>\n\n\n\n<p><strong>&gt; -F: <\/strong>This option tells Varnish to run in the foreground, meaning it won&#8217;t daemonize and detach from the terminal.<\/p>\n\n\n\n<p><strong>&gt; -a :80:<\/strong> This option specifies the address and port on which Varnish should listen for incoming client requests. <\/p>\n\n\n\n<p>In this case, it&#8217;s listening on port 80 of all available network interfaces (: represents all interfaces).<\/p>\n\n\n\n<p><strong>&gt; -T localhost:6082:<\/strong> This option specifies the address and port where the Varnish administration interface (Varnish Management Interface, VSM) is available. <\/p>\n\n\n\n<p>It&#8217;s listening on localhost (127.0.0.1) on port 8443. <\/p>\n\n\n\n<p>This interface is typically used for administration tasks such as cache inspection, statistics gathering, and dynamic configuration updates.<\/p>\n\n\n\n<p><strong>&gt; -p feature=<\/strong>+http2: This option enables HTTP\/2 support in Varnish. <\/p>\n\n\n\n<p>HTTP\/2 is the latest version of the HTTP protocol and offers improvements over HTTP\/1.1, including multiplexing as well as header compression.<\/p>\n\n\n\n<p><strong>&gt; -f \/etc\/varnish\/default.vcl: <\/strong>This option specifies the location of the Varnish Configuration Language (VCL) file that Varnish uses for configuring its behavior. <\/p>\n\n\n\n<p>In this case, it&#8217;s using \/etc\/varnish\/default.vcl.<\/p>\n\n\n\n<p><strong>&gt; -S \/etc\/varnish\/secret:<\/strong> This option specifies the location of the secret file used for securing communication between Varnish and varnishadm (the management CLI for Varnish). <\/p>\n\n\n\n<p>Hence, this file typically contains a shared secret key.<\/p>\n\n\n\n<p><strong>&gt; -s malloc,2g<\/strong>: This option specifies the storage method and size for the Varnish cache. In this case, it&#8217;s using the malloc storage backend, which stores cache objects in memory. <\/p>\n\n\n\n<p>The 2g indicates that Varnish should allocate 2 gigabytes of memory for caching.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">We&#8217;ve finished configuring Varnish. Now, we&#8217;re setting up the web server &#8211; Apache.<\/h2>\n\n\n\n<p>Now we&#8217;ll <a href=\"https:\/\/cloudkul.com\/blog\/apache-virtual-hosting-with-different-users\/\">install the Apache<\/a> on the Ubuntu server. For Apache, we&#8217;ll have it running on port 8080.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install apache2<\/pre>\n\n\n\n<p>In Apache, we&#8217;ll tweak settings in both the virtual hosts (vhosts) files and the port.conf file.<\/p>\n\n\n\n<p>First, we will change the port in the ports.conf which is located at \/etc\/apache2\/ports.conf. Here you will change the port from 80 to 8080.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/03\/port-80-8080-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"282\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/03\/port-80-8080-2.png\" alt=\"apache\" class=\"wp-image-17560\" style=\"width:680px;height:auto\"\/><\/a><\/figure><\/div>\n\n\n<p>Now, we will make the changes in this file 000-default.conf, it is located at \/etc\/apache2\/sites-enabled\/000-default.conf.<\/p>\n\n\n\n<p>Here also you will change Apache port from 80 to 8080.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/03\/port-80-8080-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"576\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/03\/port-80-8080-3.png\" alt=\"port\" class=\"wp-image-17561\" style=\"width:766px;height:auto\"\/><\/a><\/figure><\/div>\n\n\n<p><br>Varnish gives us a WordPress VCL (Varnish Configuration Language) file, and we&#8217;ll use it for our setup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Here&#8217;s the full VCL file:<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">vcl 4.1;\n\nimport std;\n\nbackend default {\n    .host = \"127.0.0.1\";\n    .port = \"8080\";\n}\n\n# Add hostnames, IP addresses and subnets that are allowed to purge content\nacl purge {\n    \"localhost\";\n    \"127.0.0.1\";\n    \"::1\";\n}\n\nsub vcl_recv {   \n    # Remove empty query string parameters\n    # e.g.: www.example.com\/index.html?\n    if (req.url ~ \"\\?$\") {\n        set req.url = regsub(req.url, \"\\?$\", \"\");\n    }\n\n    # Remove port number from host header\n    set req.http.Host = regsub(req.http.Host, \":[0-9]+\", \"\");\n\n    # Sorts query string parameters alphabetically for cache normalization purposes\n    set req.url = std.querysort(req.url);\n\n    # Remove the proxy header to mitigate the httpoxy vulnerability\n    # See https:\/\/httpoxy.org\/\n    unset req.http.proxy;\n    \n    # Add X-Forwarded-Proto header when using https\n    if (!req.http.X-Forwarded-Proto) {\n        if(std.port(server.ip) == 443 || std.port(server.ip) == 8443) {\n            set req.http.X-Forwarded-Proto = \"https\";\n        } else {\n            set req.http.X-Forwarded-Proto = \"http\";\n        }\n    }\n\n    # Purge logic to remove objects from the cache. \n    # Tailored to the Proxy Cache Purge WordPress plugin\n    # See https:\/\/wordpress.org\/plugins\/varnish-http-purge\/\n    if(req.method == \"PURGE\") {\n        if(!client.ip ~ purge) {\n            return(synth(405,\"PURGE not allowed for this IP address\"));\n        }\n        if (req.http.X-Purge-Method == \"regex\") {\n            ban(\"obj.http.x-url ~ \" + req.url + \" &amp;&amp; obj.http.x-host == \" + req.http.host);\n            return(synth(200, \"Purged\"));\n        }\n        ban(\"obj.http.x-url == \" + req.url + \" &amp;&amp; obj.http.x-host == \" + req.http.host);\n        return(synth(200, \"Purged\"));\n    }\n\n    # Only handle relevant HTTP request methods\n    if (\n        req.method != \"GET\" &amp;&amp;\n        req.method != \"HEAD\" &amp;&amp;\n        req.method != \"PUT\" &amp;&amp;\n        req.method != \"POST\" &amp;&amp;\n        req.method != \"PATCH\" &amp;&amp;\n        req.method != \"TRACE\" &amp;&amp;\n        req.method != \"OPTIONS\" &amp;&amp;\n        req.method != \"DELETE\"\n    ) {\n        return (pipe);\n    }\n\n    # Remove tracking query string parameters used by analytics tools\n    if (req.url ~ \"(\\?|&amp;)(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=\") {\n        set req.url = regsuball(req.url, \"&amp;(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\\-\\.%25]+)\", \"\");\n        set req.url = regsuball(req.url, \"\\?(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\\-\\.%25]+)\", \"?\");\n        set req.url = regsub(req.url, \"\\?&amp;\", \"?\");\n        set req.url = regsub(req.url, \"\\?$\", \"\");\n    }\n\n    # Only cache GET and HEAD requests\n    if (req.method != \"GET\" &amp;&amp; req.method != \"HEAD\") {\n        set req.http.X-Cacheable = \"NO:REQUEST-METHOD\";\n        return(pass);\n    }\n\n    # Mark static files with the X-Static-File header, and remove any cookies\n    # X-Static-File is also used in vcl_backend_response to identify static files\n    if (req.url ~ \"^[^?]*\\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|ogg|ogm|opus|otf|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)(\\?.*)?$\") {\n        set req.http.X-Static-File = \"true\";\n        unset req.http.Cookie;\n        return(hash);\n    }\n\n    # No caching of special URLs, logged in users and some plugins\n    if (\n        req.http.Cookie ~ \"wordpress_(?!test_)[a-zA-Z0-9_]+|wp-postpass|comment_author_[a-zA-Z0-9_]+|woocommerce_cart_hash|woocommerce_items_in_cart|wp_woocommerce_session_[a-zA-Z0-9]+|wordpress_logged_in_|comment_author|PHPSESSID\" ||\n        req.http.Authorization ||\n        req.url ~ \"add_to_cart\" ||\n        req.url ~ \"edd_action\" ||\n        req.url ~ \"nocache\" ||\n        req.url ~ \"^\/addons\" ||\n        req.url ~ \"^\/bb-admin\" ||\n        req.url ~ \"^\/bb-login.php\" ||\n        req.url ~ \"^\/bb-reset-password.php\" ||\n        req.url ~ \"^\/cart\" ||\n        req.url ~ \"^\/checkout\" ||\n        req.url ~ \"^\/control.php\" ||\n        req.url ~ \"^\/login\" ||\n        req.url ~ \"^\/logout\" ||\n        req.url ~ \"^\/lost-password\" ||\n        req.url ~ \"^\/my-account\" ||\n        req.url ~ \"^\/product\" ||\n        req.url ~ \"^\/register\" ||\n        req.url ~ \"^\/register.php\" ||\n        req.url ~ \"^\/server-status\" ||\n        req.url ~ \"^\/signin\" ||\n        req.url ~ \"^\/signup\" ||\n        req.url ~ \"^\/stats\" ||\n        req.url ~ \"^\/wc-api\" ||\n        req.url ~ \"^\/wp-admin\" ||\n        req.url ~ \"^\/wp-comments-post.php\" ||\n        req.url ~ \"^\/wp-cron.php\" ||\n        req.url ~ \"^\/wp-login.php\" ||\n        req.url ~ \"^\/wp-activate.php\" ||\n        req.url ~ \"^\/wp-mail.php\" ||\n        req.url ~ \"^\/wp-login.php\" ||\n        req.url ~ \"^\\?add-to-cart=\" ||\n        req.url ~ \"^\\?wc-api=\" ||\n        req.url ~ \"^\/preview=\" ||\n        req.url ~ \"^\/\\.well-known\/acme-challenge\/\"\n    ) {\n\t     set req.http.X-Cacheable = \"NO:Logged in\/Got Sessions\";\n\t     if(req.http.X-Requested-With == \"XMLHttpRequest\") {\n\t\t     set req.http.X-Cacheable = \"NO:Ajax\";\n\t     }\n        return(pass);\n    }\n\n    # Remove any cookies left\n    unset req.http.Cookie;\n    return(hash);\n}\n\nsub vcl_hash {\n    if(req.http.X-Forwarded-Proto) {\n        # Create cache variations depending on the request protocol       \n        hash_data(req.http.X-Forwarded-Proto);\n    }\n}\n\nsub vcl_backend_response {\n    # Inject URL &amp; Host header into the object for asynchronous banning purposes\n    set beresp.http.x-url = bereq.url;\n    set beresp.http.x-host = bereq.http.host;\n\n    # If we dont get a Cache-Control header from the backend\n    # we default to 1h cache for all objects\n    if (!beresp.http.Cache-Control) {\n        set beresp.ttl = 1h;\n        set beresp.http.X-Cacheable = \"YES:Forced\";\n    }\n\n    # If the file is marked as static we cache it for 1 day\n    if (bereq.http.X-Static-File == \"true\") {\n        unset beresp.http.Set-Cookie;\n        set beresp.http.X-Cacheable = \"YES:Forced\";\n        set beresp.ttl = 1d;\n    }\n\n\t# Remove the Set-Cookie header when a specific Wordfence cookie is set\n    if (beresp.http.Set-Cookie ~ \"wfvt_|wordfence_verifiedHuman\") {\n\t    unset beresp.http.Set-Cookie;\n\t }\n\t\n    if (beresp.http.Set-Cookie) {\n        set beresp.http.X-Cacheable = \"NO:Got Cookies\";\n    } elseif(beresp.http.Cache-Control ~ \"private\") {\n        set beresp.http.X-Cacheable = \"NO:Cache-Control=private\";\n    }\t\n}\n\nsub vcl_deliver {\n    # Debug header\n    if(req.http.X-Cacheable) {\n        set resp.http.X-Cacheable = req.http.X-Cacheable;    \n    } elseif(obj.uncacheable) {\n        if(!resp.http.X-Cacheable) {\n            set resp.http.X-Cacheable = \"NO:UNCACHEABLE\";        \n        }\n    } elseif(!resp.http.X-Cacheable) {\n        set resp.http.X-Cacheable = \"YES\";\n    }\n    \n    # Cleanup of headers\n    unset resp.http.x-url;\n    unset resp.http.x-host;    \n}<\/pre>\n\n\n\n<p>This is a Varnish Configuration Language (VCL) script used for configuring Varnish Cache, an HTTP accelerator. Let&#8217;s break down the script section by section:<\/p>\n\n\n\n<p><strong>Backend Configuration:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">backend default {\n    .host = \"127.0.0.1\";\n    .port = \"8080\";\n}<\/pre>\n\n\n\n<p>This section defines the default backend server to which Varnish will forward requests.<\/p>\n\n\n\n<p><strong>Purge ACL:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">acl purge {\n    \"localhost\";\n    \"127.0.0.1\";\n    \"::1\";\n}<\/pre>\n\n\n\n<p>Defines an ACL (Access Control List) named &#8220;purge&#8221; containing IP addresses and hostnames allowed to perform cache purges.<\/p>\n\n\n\n<p><strong>vcl_recv (Receive)<\/strong><br>This subroutine handles incoming client requests and performs various actions:<\/p>\n\n\n\n<ul>\n<li>Query String Handling: Removes empty query string parameters and sorts query parameters alphabetically for cache normalization.<\/li>\n\n\n\n<li>Proxy Header Removal: Removes the proxy header to mitigate the httproxy vulnerability.<\/li>\n\n\n\n<li>X-Forwarded-Proto Header Addition: Adds the X-Forwarded-Proto header to indicate the protocol used (http or https).<\/li>\n\n\n\n<li>Purge Logic: Implements logic for cache purging based on the PURGE method.<\/li>\n\n\n\n<li>HTTP Method Handling: Allows only specific HTTP request methods and passes others through.<\/li>\n\n\n\n<li>Query String Parameter Removal: Removes tracking query string parameters used by analytics tools.<\/li>\n\n\n\n<li>Cache Control: Specifies which requests should not be cached.<\/li>\n<\/ul>\n\n\n\n<p><strong>vcl_hash<\/strong><br>This subroutine is responsible for generating a hash key used to look up objects in the cache. As a result, it creates cache variations based on the request protocol.<\/p>\n\n\n\n<p><strong>vcl_backend_response<\/strong><br>Handles responses from the backend server:<\/p>\n\n\n\n<ul>\n<li>Header Injection: Injects URL and Host headers into the object for asynchronous banning purposes.<\/li>\n\n\n\n<li>Cache-Control Header Handling: Sets a default TTL if no Cache-Control header is present.<\/li>\n\n\n\n<li>Static File Caching: Sets a specific TTL for static files.<\/li>\n\n\n\n<li>Cookie Handling: Removes Set-Cookie headers under certain conditions.<\/li>\n<\/ul>\n\n\n\n<p><strong>vcl_deliver<\/strong><br>Performs actions before delivering the response to the client:<\/p>\n\n\n\n<p><strong>&gt;<\/strong> Debug Header: Sets a debug header indicating cacheability.<br><strong>&gt;<\/strong> Header Cleanup: Removes injected headers.<\/p>\n\n\n\n<p><br>Finally, we will reload and restart the services.<\/p>\n\n\n\n<p>To reload the systemd daemon we will use the below command: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl daemon-reload<\/pre>\n\n\n\n<p>Restart the Apache web server, we will run the below command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl restart apache2<\/pre>\n\n\n\n<p>To restart the Varnish, we will run the below command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl restart varnish<\/pre>\n\n\n\n<p>This restart will also make sure that the correct settings are in place for Nginx, Apache, and Varnish to properly store and serve cached versions of your WordPress website, improving its performance.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/02\/varnish_1-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"511\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/02\/varnish_1-1-1024x511.png\" alt=\"ngimx\" class=\"wp-image-17526\"\/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/02\/varnish_2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"516\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/02\/varnish_2-1024x516.png\" alt=\"nginx\" class=\"wp-image-17527\"\/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to delete the cache?<\/strong><\/h3>\n\n\n\n<p>You can restart or reload Varnish if you want to clear the entire cache. This will effectively clear all cached content.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl restart varnish\nor\nsudo service varnish reload<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to disable varnish completely?<\/strong><\/h3>\n\n\n\n<p>To completely disable the Varnish service and switch Apache back to port 80 from port 8080, follow these steps:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Stop the Varnish service<\/strong>:<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl stop varnish<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Edit Apache configuration:<\/strong><\/h3>\n\n\n\n<p>Open the Apache configuration file, typically located at \/etc\/apache2\/sites-enabled\/*.conf.<\/p>\n\n\n\n<p>Find the line that specifies the port number. It might look like Listen 8080 or Listen.<br>Further, change the port number to 80.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Edit ports.conf<\/strong><\/h3>\n\n\n\n<p>Open the ports.conf file, typically located in the \/etc\/apache2\/ directory.<br>Now, change any references from 8080 to 80.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Restart Apache:<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl restart apache2<\/pre>\n\n\n\n<p>Open a web browser and navigate to your server&#8217;s domain or IP address. As a result, it should now be accessible without specifying the port number, as Apache is now listening on port 80.<\/p>\n\n\n\n<p>By following these steps, you&#8217;ve effectively disabled Varnish and restored Apache to listen on port 80, ensuring your web server serves content directly without Varnish caching in between.<\/p>\n\n\n\n<p>The above-mentioned part will only work for port 80 because the varnish is an HTTP accelerator and it can cache the HTTP level but what if we want to serve the website with SSL? <\/p>\n\n\n\n<p>So let&#8217;s configure the varnish with Apache and Nginx to work with SSL.<\/p>\n\n\n\n<p>The above Varnish configuration will be the same as it is mentioned earlier. Just we will make the changes for the Ngixn conf.<\/p>\n\n\n\n<p>You can find the nginx default file with the name default in the path. <strong>Path<\/strong> &#8211; \/etc\/nginx\/sites-enabled\/default<\/p>\n\n\n\n<p>Please go through the below-given conf and make the changes in your file accordingly.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server {\n        listen 80;\n        server_name yourdomain.com www.yourdomain.com;\n        return 301 https:\/\/$server_name$request_uri;\n}\n\nserver {\n        listen 443;\n        server_name yourdomain.com;\n        ssl on;\n        ssl_certificate \/path\/to\/your\/certificate_file;\n        ssl_certificate_key \/path\/to\/your\/certificate_key;\n\n\n        ssl_session_timeout 5m;\n        ssl_protocols TLSv1.1 TLSv1.2;\n        ssl_ciphers \"HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES\";\n        ssl_prefer_server_ciphers on;\n\n\n        location \/ {\n             proxy_pass http:\/\/127.0.0.1:6081;\n             include \/etc\/nginx\/proxy_params;\n        }\n\n}<\/pre>\n\n\n\n<p>Now restart all the services i.e. Nginx, Varnish and Apache. Open your browser and hit the domain. As a result, you will able to see the varnish hit in the browser console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"need-help\">NEED HELP?<\/h2>\n\n\n\n<p>Was this guide helpful? Please share your feedback in the comments below.<\/p>\n\n\n\n<p>In case you have any issues\/queries regarding the module please raise a ticket at<br><a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\"><strong>https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/<\/strong><\/a><\/p>\n\n\n\n<p>For any further information or query contact us at&nbsp;<strong>support@webkul.com<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Thanks for reading!!<\/strong><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>What is Varnish? Varnish is a software that helps websites load faster by storing a <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":42,"featured_media":0,"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,3,780,68,875],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Impact of Varnish on WordPress - Cloudkul<\/title>\n<meta name=\"description\" content=\"Varnish is a software that helps websites load faster by storing a copy of the website&#039;s content in a special place called a cache.\" \/>\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\/impact-of-varnish-on-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Impact of Varnish on WordPress - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Varnish is a software that helps websites load faster by storing a copy of the website&#039;s content in a special place called a cache.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-04T07:45:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-22T12:23:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/03\/port-80-8080-2.png\" \/>\n<meta name=\"author\" content=\"kshitiz pathak\" \/>\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\/impact-of-varnish-on-wordpress\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/\",\"name\":\"Impact of Varnish on WordPress - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2024-04-04T07:45:39+00:00\",\"dateModified\":\"2024-10-22T12:23:03+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/b89f1d815be925560497bfa6856c023f\"},\"description\":\"Varnish is a software that helps websites load faster by storing a copy of the website's content in a special place called a cache.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Impact of Varnish on WordPress\"}]},{\"@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\/b89f1d815be925560497bfa6856c023f\",\"name\":\"kshitiz pathak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/25b42f20ad828edde55592fd63ff894d?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/25b42f20ad828edde55592fd63ff894d?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"kshitiz pathak\"},\"url\":\"https:\/\/cloudkul.com\/blog\/author\/kshitizpathak-cloud\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Impact of Varnish on WordPress - Cloudkul","description":"Varnish is a software that helps websites load faster by storing a copy of the website's content in a special place called a cache.","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\/impact-of-varnish-on-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"Impact of Varnish on WordPress - Cloudkul","og_description":"Varnish is a software that helps websites load faster by storing a copy of the website's content in a special place called a cache.","og_url":"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/","og_site_name":"Cloudkul","article_published_time":"2024-04-04T07:45:39+00:00","article_modified_time":"2024-10-22T12:23:03+00:00","og_image":[{"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2024\/03\/port-80-8080-2.png"}],"author":"kshitiz pathak","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/","url":"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/","name":"Impact of Varnish on WordPress - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2024-04-04T07:45:39+00:00","dateModified":"2024-10-22T12:23:03+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/b89f1d815be925560497bfa6856c023f"},"description":"Varnish is a software that helps websites load faster by storing a copy of the website's content in a special place called a cache.","breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/impact-of-varnish-on-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Impact of Varnish on WordPress"}]},{"@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\/b89f1d815be925560497bfa6856c023f","name":"kshitiz pathak","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/25b42f20ad828edde55592fd63ff894d?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/25b42f20ad828edde55592fd63ff894d?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"kshitiz pathak"},"url":"https:\/\/cloudkul.com\/blog\/author\/kshitizpathak-cloud\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/17487"}],"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\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=17487"}],"version-history":[{"count":78,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/17487\/revisions"}],"predecessor-version":[{"id":18388,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/17487\/revisions\/18388"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=17487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=17487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=17487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}