madalinoprea / magneto-varnish

Magento extension that implements full page caching using Varnish
http://moprea.ro/2011/may/6/magento-performance-optimization-varnish-cache-3/
123 stars 36 forks source link

Varnish 3 is out #7

Open rgoytacaz opened 13 years ago

rgoytacaz commented 13 years ago

Hello There,

First of all, awesome work, I would like to know what do you need to get done to use the new features on varnish 3?

I would gladly help you out on this.

stefanskotte commented 13 years ago

Hi, I'm going to port existing configuration to varnish 3.0, I will share it here once I have it working.

bkrembs commented 13 years ago

Any update on this? Because this project looks like exactly what we need.

I think Varnish 3 support isn't just a technical nicety, but the indication of an active open source project that my projects decision-makers need to see before giving it a try.

I'm new to Varnish, but just like rgoytacaz, I'm definitely willing to help out with the migration!

stefanskotte commented 13 years ago

We recently focused more on magento enterprise, which has FPC builtin. However I will probably revisit later, but for now I have no planned updates.

rgoytacaz commented 13 years ago

I've worked on a working VCL file for Varnish 3. I could share it if you want. I would also love you taking a look on the sessions on memcache problem, or giving out pointers so I can take a look at it.

stefanskotte commented 13 years ago

It would be very interesting to see the vcl - does it work reliably when purging urls from the cache ?

Because i've found the 2.x to keep entries regardless, only thing that works is a restart.

gionni2010 commented 13 years ago

hello i don't know how to pull requests on github, but i made modification to the vlc file to make it work on varnish 3.0

My file is here: http://c359372.r72.cf3.rackcdn.com/default.vcl

But i had to comment a variable varnish 3.0 doesn't recognize, you can read the comment on line 137. Someone know if this is a good solution?

Sorry for my english, and thank you!

ghost commented 13 years ago

obj.cacheable became obj.ttl > 0s. it was a piece of cake to convert the original .vcl to a 3.0 compatible one. Here's my version:

# 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 = "127.0.0.1";
     .port = "80"; # READ THIS: You should configure Apache to run on port 81
 }

acl trusted {
    "127.0.0.1";
    "127.0.1.1";
    "192.168.56.1";
    # Add other ips that are allowed to purge cache
}

#
# http://www.varnish-cache.org/docs/2.1/tutorial/vcl.html#vcl-recv
# @param req Request object
sub vcl_recv {
    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") {
    # Allow requests from trusted IPs to purge the cache
    if (!client.ip ~ trusted) {
       error 405 "Not allowed.";
    }
    ban("req.url ~ " + req.url);
    error 200 "Ok"; #We don't go to backend
    #return(lookup); # @see vcl_hit
    }

    if (req.request != "GET" &&
       req.request != "HEAD" &&
       req.request != "PUT" &&
       req.request != "POST" &&
       req.request != "TRACE" &&
       req.request != "OPTIONS" &&
       req.request != "DELETE") {
     /* Non-RFC2616 or CONNECT which is weird. */
     return (pipe);
    }

     # Cache only GET or HEAD requests
     if (req.request != "GET" && req.request != "HEAD") {
     /* We only deal with GET and HEAD by default */
     return (pass);
     }

    # parse accept encoding rulesets to normalize
    if (req.http.Accept-Encoding) {
    if (req.http.Accept-Encoding ~ "gzip") {
        set req.http.Accept-Encoding = "gzip";
    } elsif (req.http.Accept-Encoding ~ "deflate") {
        set req.http.Accept-Encoding = "deflate";
    } else {
        # unkown algorithm
        remove req.http.Accept-Encoding;
    }
    }

     # Rules for static files
     if (req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|gz|rar|txt|bzip|pdf)(\?.*|)$") {
    set req.http.staticmarker = "1";
    unset req.http.Cookie;

    return (lookup);
    }

    # Don't cache pages for Magento Admin
    # FIXME: change this rule if you use custom url in admin
    if (req.url ~ "^/(index.php/)?admin") {
    return(pass);
    }

    # Don't cache checkout/customer pages, product compare
    if (req.url ~ "^/(index.php/)?(checkout|customer|catalog/product_compare|wishlist)") {
    return(pass);
    }

    # Don't cache till session end
    if (req.http.cookie ~ "nocache_stable") {
    return(pass);
    }

    # Unique identifier witch tell Varnish use cache or not
    if (req.http.cookie ~ "nocache") {
    return(pass);
    }

    # Remove cookie
    unset req.http.Cookie;
    set req.http.magicmarker = "1"; #Instruct varnish to remove cache headers received from backend
    return(lookup);
 }

sub vcl_pipe {
# # Note that only the first request to the backend will have
# # X-Forwarded-For set. If you use X-Forwarded-For and want to
# # have it set for all requests, make sure to have:
# # set req.http.connection = "close";
# # here. It is not set by default as it might break some broken web
# # applications, like IIS with NTLM authentication.
     return (pipe);
}

#sub vcl_pass {
# return (pass);
#}

#sub vcl_hash {
# set req.hash += req.url;
# if (req.http.host) {
# set req.hash += req.http.host;
# } else {
# set req.hash += server.ip;
# }
# return (hash);
# }

# Called after a cache lookup if the req. document was found in the cache.
sub vcl_hit {
    if (req.request == "PURGE") {
    ban_url(req.url);
    error 200 "Purged";
    }

    if (!obj.ttl > 0s) {
    return (pass);
    }
    return (deliver);
}

# Called after a cache lookup and odc was not found in cache.
sub vcl_miss {
    if (req.request == "PURGE"){
    error 200 "Not in cache";
    }
    return (fetch);
}

# Called after document was retreived from backend
# @var req Request object.
# @var beresp Backend response (contains HTTP headers from backend)
sub vcl_fetch {
    set req.grace = 30s;

    # Current response should not be cached
    if(beresp.http.Set-Cookie ~ "nocache=1") {
    return (deliver);
    }

    # Flag set when we want to delete cache headers received from backend
    if (req.http.magicmarker){
    unset beresp.http.magicmarker;
    unset beresp.http.Cache-Control;
    unset beresp.http.Expires;
    unset beresp.http.Pragma;
    unset beresp.http.Cache;
    unset beresp.http.Server;
    unset beresp.http.Set-Cookie;
    unset beresp.http.Age;

    # default ttl for pages
    set beresp.ttl = 1d;
    }
    if (req.http.staticmarker) {
    set beresp.ttl = 30d; # static file cache expires in 30 days
    unset beresp.http.staticmarker;
    unset beresp.http.ETag; # Removes Etag in case we have multiple frontends
    }

    return (deliver);
}

# Called after a cached document is delivered to the client.
sub vcl_deliver {
    if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT (" + obj.hits + ")";
    } else {
    set resp.http.X-Cache = "MISS";
    # set resp.http.X-Cache-Hash = obj.http.hash;
    }
    return (deliver);
}
#
# sub vcl_error {
# set obj.http.Content-Type = "text/html; charset=utf-8";
# synthetic {"
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
# <html>
# <head>
# <title>"} obj.status " " obj.response {"</title>
# </head>
# <body>
# <h1>Error "} obj.status " " obj.response {"</h1>
# <p>"} obj.response {"</p>
# <h3>Guru Meditation:</h3>
# <p>XID: "} req.xid {"</p>
# <hr>
# <address>
# <a href="http://www.varnish-cache.org/">Varnish cache server</a>
# </address>
# </body>
# </html>
# "};
# return (deliver);
# }
stefanskotte commented 13 years ago

Sounds interesting - will do some testing with that configuration :)

Did anyone manage to solve the nocache=1 not being honored in IE8 ? I have problems when you put products in the cart, and it insists on caching pages you visited previously.

Use case:

1) put product in cart 2) press back in the IE8 browser 3) page will be fetched from cache, and thus the cart contents-block is not updated in the top right. 4) if you refresh the page it works.

Any ideas ?

0d1 commented 11 years ago

Same as kevin25.

I don't know if you're still working on it but, I have a magento 1.7.0.2 with varnish 3.0.3 and facing an issue while modifying/changing a product. In fact, I cannot modify a product by having Full page cache in Varnish activated I got this error :

PHP Fatal error: Call to a member function isActive() on a non-object in /path/to/app/code/community/Magneto/Varnish/Helper/Data.php on line 113, referer: https://www.xxx.com/index.php/mysecretadminpage/cache/index/key/2f08f73fabe13959a24acda3999143ef/ PHP Fatal error: Class 'Mage' not found in /path/to/app/code/core/Mage/Core/functions.php on line 244, referer: https://www.xxx.com/index.php/mysecretadminpage/cache/index/key/2f08f73fabe13959a24acda3999143ef/

the portion of code line 113 is :

    protected function logAdminAction($success=true, $generalInfo=null, $additionalInfo=null, $errors=array()) {
            $eventCode = 'varnish_purge'; // this needs to match the code in logging.xml

            if (!Mage::getSingleton('enterprise_logging/config')->isActive($eventCode, true)) {
                    return;
            }

            $username = null;
            $userId   = null;
            if (Mage::getSingleton('admin/session')->isLoggedIn()) {
                    $userId = Mage::getSingleton('admin/session')->getUser()->getId();
                    $username = Mage::getSingleton('admin/session')->getUser()->getUsername();
            }

Could you please help me to fix it please. to workaround for the moment is to manually purge by cli en varnish ( really annoying )