WordPress.org

Ready to get started?Download WordPress

Forums

W3 Total Cache
Varnish purge is not working (10 posts)

  1. TechHamlet
    Member
    Posted 2 years ago #

    Hi, I have just setup varnish on my server but it looks like W3 Total Cache was unable to clear the varnish cache!

    Here is my varnish default.acl :

    # This is a basic VCL configuration file for varnish.  See the vcl(7)
    # man page for details on VCL syntax and semantics.
    #
    # Default backend definition.  Set this to point to your content
    # server.
    #
    backend default {
        .host = "77.81.240.177";
        .port = "8080";
    }
    
    acl purge {
        "77.81.240.177";
    }
    
    sub vcl_recv {
    
        # Add a unique header containing the client address
        remove req.http.X-Forwarded-For;
        #set    req.http.X-Forwarded-For = client.ip;
        set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;    
    
        # Let's make sure we aren't compressing already compressed formats.
        if (req.http.Accept-Encoding) {
            if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|mp3|mp4|m4v)(\?.*|)$") {
                remove req.http.Accept-Encoding;
            } elsif (req.http.Accept-Encoding ~ "gzip") {
                set req.http.Accept-Encoding = "gzip";
            } elsif (req.http.Accept-Encoding ~ "deflate") {
                set req.http.Accept-Encoding = "deflate";
            } else {
                remove req.http.Accept-Encoding;
            }
        }
    
        if (req.request == "PURGE") {
            if (!client.ip ~ purge) {
                error 405 "Not allowed.";
            }
            return(lookup);
        }
    
        if (req.url ~ "^/$") {
            unset req.http.cookie;
        }
    }
    
    sub vcl_hit {
        if (req.request == "PURGE") {
            set obj.ttl = 0s;
            error 200 "Purged.";
        }
    }
    
    sub vcl_miss {
        if (req.request == "PURGE") {
            error 404 "Not in cache.";
        }
    
        if (!(req.url ~ "wp-(login|admin)")) {
            unset req.http.cookie;
        }
    
        if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
            unset req.http.cookie;
            set req.url = regsub(req.url, "\?.$", "");
        }
    
        if (req.url ~ "^/$") {
            unset req.http.cookie;
        }
    }
    
    sub vcl_fetch {
        if (req.url ~ "^/$") {
            unset beresp.http.set-cookie;
        }
    
        if (!(req.url ~ "wp-(login|admin)")) {
            unset beresp.http.set-cookie;
        }
    
        if (req.url ~ "^/w00tw00t") {
            error 403 "Not permitted";
        }
    }

    I have set the ip 77.81.240.177 in the total cache settings as the varnish server. Please help me to fix this issue!

    http://wordpress.org/extend/plugins/w3-total-cache/

  2. TechHamlet
    Member
    Posted 2 years ago #

    Just updated the default.acl :

    acl purge {
        "localhost";
        "127.0.0.1";
        "77.81.240.177";
    }
    
    sub vcl_recv {
    
        # Add a unique header containing the client address
        remove req.http.X-Forwarded-For;
        #set    req.http.X-Forwarded-For = client.ip;
        set    req.http.X-Forwarded-For = req.http.rlnclientipaddr; 
    
       if (req.request == "PURGE") {
         if (!client.ip ~ purge) {
           error 405 "Not allowed.";
         }
           ban("req.url ~ "+req.url);
           error 200 "Purged";
       }
      if (req.http.Accept-Encoding) {
    #revisit this list
        if (req.url ~ "\.(gif|jpg|jpeg|swf|flv|mp3|mp4|pdf|ico|png|gz|tgz|bz2)(\?.*|)$") {
          remove req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
          set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate") {
          set req.http.Accept-Encoding = "deflate";
        } else {
          remove req.http.Accept-Encoding;
        }
      }
      if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
        unset req.http.cookie;
        set req.url = regsub(req.url, "\?.*$", "");
      }
      #if (req.url ~ "(\?|&)(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=”) {
      #  set req.url = regsub(req.url, “\?.*$”, “”);
      #}
      if (req.http.cookie) {
        if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
          return(pass);
        } else {
          unset req.http.cookie;
        }
      }
    }
    
    sub vcl_fetch {
      if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
        return (hit_for_pass);
      }
      if ( (!(req.url ~ "(wp-(login|admin)|login)")) || (req.request == "GET") ) {
        unset beresp.http.set-cookie;
       set beresp.ttl = 2h;
      }
      if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
        set beresp.ttl = 30d;
      } #else {
       # set beresp.do_esi = true;
      #}
    }
    
    sub vcl_deliver {
    # multi-server webfarm? set a variable here so you can check
    # the headers to see which frontend served the request
    #   set resp.http.X-Server = "server-01";
       if (obj.hits > 0) {
         set resp.http.X-Cache = "HIT";
       } else {
         set resp.http.X-Cache = "MISS";
       }
    }
    sub vcl_hit {
      if (req.request == "PURGE") {
        set obj.ttl = 0s;
        error 200 "OK";
      }
    }
    
    sub vcl_miss {
      if (req.request == "PURGE") {
        error 404 "Not cached";
      }
    }

    Note : Im using the latest varnish 3!

  3. includao
    Member
    Posted 2 years ago #

    And did it work with this new code?

  4. TechHamlet
    Member
    Posted 2 years ago #

    Nope... I hired a freelancer and he made some changes to the default.vcl... I had some problems in the server and he fixed it.

    Also there was a problem with the plugin which won't clear all the cache! The freelancer also fixed that issue... I submitted that code to total cache and they might fix it with the next update :D

  5. cdsalmons
    Member
    Posted 2 years ago #

    TechHamlet,
    First and foremost, thanks for the work you had put into this project. I am trying to figure out the same setup. Just in case W3 does not use it, is there any way I can see the code? I can give you me Skype/Email if you don't want to post public. I have tried 6 VCL/W3 setups and so far they all have issues, so if yours actually works, I would love to see it. Thanks in advance

  6. includao
    Member
    Posted 2 years ago #

    Yes, please, could you share the code and the fix? Thank you for any help you can provide because it seems that W3T Cache is not going to update anytime soon.

  7. TechHamlet
    Member
    Posted 2 years ago #

    Sorry for the late reply guys! :) I have just written a complete guide on what we did in here : http://techhamlet.com/forum/viewtopic.php?id=10

    First change the plugin files and see if it works. Some times you might have already set up your default.vcl correctly. But if it didn't work, adjust the VCL according to the sample I have posted. If it also didn't work, please tell me! Hope it fix your problems :D

  8. leandrorg
    Member
    Posted 1 year ago #

    TechHamlet, could you post here? Because your link isn't working anymore.

  9. Markus Stefanko
    Member
    Posted 1 year ago #

    ditto, would be great!

  10. josephseeley
    Member
    Posted 1 year ago #

    I was having this problem too. W3TC and Better WP Varnish were both not able to clear the cache. All I needed to do was add/edit (in default.vcl):

    if (req.request == "PURGE") {
    	if (!client.ip ~ purge) {
    		error 405 "Not allowed.";
    	}
    	ban("req.url ~ "+req.url);
    	error 200 "Purged";
    }

    Just before:

    if (req.request == "PURGE") {
    	if (!client.ip ~ purge) {
    		error 405 "Not allowed.";
    	}
    	return(lookup);
    }

    Restarted varnish and nginx and now it looks like it's working fine! (I'm a noob, so if I've done something horribly wrong here please let me know.)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic