• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

How to setup Varnish to do not cache particular url specific domain

October 3, 2013 by Sharad Chhetri Leave a Comment

This is again with my post I come back to share how I setup a Varnish in sharadchhetri.com network which is hosted in DigitalOcean but this time scenario is different, we do not want a particular URL or domain name should be cached by Varnish.
In this real scenario I have taken our own URL called myip.sharadchhetri.com (with this link you can get information about what is your ip address and your GeoMap location of IP address[ISP point]).

Paste the given below lines in sub vcl_recv section and be careful also it must be pasted inside return(lookup) of sub vcl_recv. Just below given section ,I have shared my default.vcl file.Check from line no. 43 to 46.
Note: replace myip.sharadchhetri.com to your URL.

Edit /etc/varnish/defualt.vcl file and paste the below given line

  # dont cache myip.sharadchhetri.com
   if (req.http.host ~ "(myip.sharadchhetri.com)") {
     return(pass);
   }

After pasting the code in default.vcl,restart the varnish

/etc/init.d/varnish restart

How to check,is configuration working or not:

Use the below command from your system(curl must be installed)

curl -I http://your-domain-name.com

The below given is reference from my server

[root@myserver ~]# cat /etc/varnish/default.vcl
backend default {
  .host = "127.0.0.1";
  .port = "8880";
}
##################################################
sub vcl_recv {
    if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
            set req.http.X-Forwarded-For =
            req.http.X-Forwarded-For + ", " + client.ip;
        } else {
            set req.http.X-Forwarded-For = client.ip;
        }
    }
    if (req.request == "PURGE") {
        if ( client.ip != "192.241.190.251") {
            error 405 "Not allowed.";
        }
        return (lookup);
    }
    if (req.request != "GET" &&
        req.request != "HEAD" &&
        req.request != "PUT" && 
        req.request != "POST" &&
        req.request != "TRACE" &&
        req.request != "OPTIONS" &&
        req.request != "DELETE") {
            return (pipe);
    }
    if (req.request != "GET" && req.request != "HEAD") {
        return (pass);
    }
    if (!(req.url ~ "wp-(login|admin)") &&
        !(req.url ~ "&preview=true" ) ) {
        unset req.http.cookie;
    }
 
    if (req.http.Authorization || req.http.Cookie) {
        return (pass);
    }

    # dont cache myip.sharadchhetri.com
   if (req.http.host ~ "(myip.sharadchhetri.com)") {
     return(pass);
   }

    return (lookup);
}

sub vcl_fetch {
    if (!(req.url ~ "wp-(login|admin)")) {
        unset beresp.http.set-cookie;
        set beresp.ttl = 96h;
    }
 
    if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
            set beresp.ttl = 120 s;
            return (hit_for_pass);
    }
    return (deliver);
}
 
sub vcl_hit {
    if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
    }
    return (deliver);
}
 
sub vcl_miss {
    if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
    }
    return (fetch);
}
 
[root@myserver ~]#

Below is reference from my system(See Age:0)

linux@tuxworld:~/Desktop$ curl -I myip.sharadchhetri.com
HTTP/1.1 200 OK
Server: Apache
Content-Type: text/html; charset=UTF-8
Accept-Ranges: bytes
Date: Thu, 03 Oct 2013 16:53:31 GMT
X-Varnish: 2015266685
Age: 0
Via: 1.1 varnish
Connection: keep-alive

linux@tuxworld:~/Desktop$

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. How to setup varnish to do not cache particular url
  2. Setup varnish cache server for wordpress site
  3. Install and configure Varnish Cache server on CentOS/RHEL 6.x
  4. Attach DKIM signature to domain name like sendgrid
  5. owncloud error : You are accessing the server from an untrusted domain
  6. How to clear apc cache by using php script
  7. How to set DHCP offered ip address to particular ethernet in freebsd
  8. Do not show line haveing particular keyword by grep command
  9. How to list users above or below particular user id
  10. How to print particular line number by using sed command

Filed Under: web accelerator

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

convert space into new line using sed command

How to see system load average in terminal with graphical representation

How to increase Password Expire date without resetting the password

Virtual Machine inaccessible status on VirtualBox 4.3

How to find when Operating system was installed in linux CentOS and Red Hat

Read the file inside compressed .gz files without extract

New XAMPP security concept

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy