<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div>Hello.</div><div><br></div><div>I
 recently added Varnish to my MediaWiki wiki and was amazed by Varnish's
 amazing performance. Many thanks to everyone who helps make this 
incredible software available to the community.</div><div><br></div><div>Mediawiki,
 the software that powers Wikipedia, has a manual page about using 
MediaWiki with Varnish, but it needs to be updated to reflect both the 
changes to VCL in version 3 and some updates to Mediawiki itself. I'm 
planning on writing this update and am requesting a review of the 
example VCL code I'll be including in the MediaWiki manual. <br></div><div><br></div><div>I
 am in no way a Varnish or HTTP expert (in fact, I'm just a graduate 
student in economics who is passionate about supporting his wiki and 
free culture in general), so I would be very grateful for any feedback 
on the example code. My hope is to spur adoption of Varnish within the 
Mediawiki community. Since many Mediawiki installations are done 
by people with little or no experience with HTTP, so I think that 
adoption requires a good manual page. <br>

</div><div><br>
</div>
<div>Some notes:</div><div>* When Mediawiki's built in support for http 
accelerators is enabled, if a
 page or image is changed on the wiki, MediaWiki will send 
notification to every registered Varnish or Squid server, telling it to 
discard the outdated stored page. The specific notification 
protocol was designed for Squid and involves HTTP requests with a PURGE 
method. Also, with support enabled, changes made to the wiki by 
anonymous users will be attributed to their IP address, found in 
'x-forwarded-for,' rather than to the requesting cache's IP address.</div><div>*
 The current manual page, designed for Varnish 2.x is 
<a target="_blank" href="http://www.mediawiki.org/wiki/Manual:Varnish_caching"><span class="yshortcuts" id="lw_1325532480_0">http://www.mediawiki.org/wiki/Manual:Varnish_caching</span></a>  . I've brought the
 issue with the Vector skin up on the Mediawiki-L list, and <a rel="nofollow" target="_blank" href="http://lists.wikimedia.org/pipermail/mediawiki-l/2011-December/038523.html">Mediawiki's Erik Moeller pointed me</a> to <a rel="nofollow" target="_blank" href="https://svn.wikia-code.com/utils/varnishhtcpd/mediawiki.vcl">Wikia's VCL script</a>, from which I adapted the vcl_recv code for dealing with cookies.</div>
* Finally, the example code is still a bit rough and I plan on cleaning 
it up a bit when writing the documentation. Knowing whether I'm on the 
right track would be very helpful, though, as my own wiki will be going 
live with the code in less than a week. :)<br><div><br></div><div>Thank you.<br></div><br>Forest<br><br><br># set default backend if no server cluster specified<br>backend default {<br>    .host = "localhost";<span class="yiv356626434tab"></span><br>    .port = "8080";<br><span class="yiv356626434tab">    # .port = "80"; also works well, but using 8080 allows direct access to Apache for debugging purposes.</span><br>}<br><br># access control list for "purge": open to only localhost and other local nodes<br>acl purge {<br>    "localhost";<br>}<br><br>#
 The default code for vcl_recv is incorporated into the following 
subroutine to make it easier to specify the proper order of execution.<br>sub vcl_recv
 {<br>    set req.backend = default;<br>    # Serve objects up to 2 minutes past their expiry if the backend is slow to respond.<br>    # Not relevant to low traffic wikis.<br>    set req.grace = 120s;<br><br>    if (req.restarts == 0) {<br>        if (req.http.x-forwarded-for) {<br>            set req.http.X-Forwarded-For =<br>            req.http.X-Forwarded-For + ", " + client.ip;<br>        } else {<br>            set req.http.X-Forwarded-For = client.ip;<br>        }<br>    }<br><br>    # This uses the ACL action called "purge". Basically if a request to<br>    # PURGE the cache comes from anywhere other than
 localhost, ignore it.<br>    if (req.request == "PURGE") {<br>        if (!client.ip ~ purge) {<br>            error 405 "Not allowed.";<br>        }<br>        return(lookup);<br>    }<br><br>    if (req.request != "GET" &&<br>        req.request != "HEAD" &&<br>        req.request != "PUT" &&<br>        req.request != "POST" &&<br>        req.request != "TRACE" &&<br>        req.request != "OPTIONS" &&<br>        req.request != "DELETE") {<br>        # Non-RFC2616 or CONNECT which is weird.<br>   
         return (pipe);<br>    }<br><br>    if (req.request != "GET" && req.request != "HEAD") {<br>        # We only deal with GET and HEAD by default<br>        return (pass);<br>    }<br><br>    # Replace "/wiki/ with the path to your MediaWiki installation.<br><span class="yiv356626434tab">    </span># CONCERN: is the following line robust?<br>    if(req.url ~ "^/wiki/"){<br>        if(req.http.Cookie ~ "(session|UserID|UserName|Token|LoggedOut)") {<br>            # dont do anything, the user is logged in<br>        } else {<br>            # dont care about any other cookies<br>       
     unset req.http.Cookie;<br>        }<br>    }<br>    if (req.http.Authorization || req.http.Cookie) {<br>         /* Not cacheable by default */<br>         return (pass);<br>    }<br><br><span class="yiv356626434tab">    # Legacy: (</span>I've marked some commented out code as Legacy. This is code that was 
found in the existing manual page, but which I'm planning on dropping in
 the new version. If you think it's worth keeping, please let me know.)<br>#    if (req.http.If-None-Match)<br>#        {return(pass);}<br><br>    # Force lookup if the request is a no-cache request from the client.<br>    if (req.http.Cache-Control ~ "no-cache") {<br>        set req.hash_always_miss = true;    <br>        # https://www.varnish-cache.org/trac/wiki/VCLExampleEnableForceRefresh<br>#        ban_url(req.url);<br>    }<br>    return (lookup);<br> }<br><br>sub vcl_pipe {<br>    # This is otherwise not necessary if you do not do any request rewriting.<br>    set bereq.http.connection = "close";<br>}<br><br>sub vcl_hit {<br>    if (req.request == "PURGE") {<br>   
     purge;<br>        error 200 "Purged";<br>    }<br><br>    # Legacy:<br>    # if (!obj.cacheable) {<br>    #     return(pass);<br>    # }<br>}<br><br>sub vcl_miss {<br>    if (req.request == "PURGE") {<br>        error 200 "Not in cache";<br>    }<br>}<br><br>sub vcl_fetch {<br>    # For debugging only. Varnish's internal Time To Live for cached object<br>    set beresp.http.X-orig-ttl = beresp.ttl;<br>    # I think the following is redundant because caches aren't allowed to change Cache Control headers<br>    set beresp.http.X-Orig-Cache-Control = beresp.http.Cache-Control;<br><br>    # set minimum timeouts to auto-discard stored objects<br>#    set beresp.prefetch =
 -30s;<br>    set beresp.grace = 120s;<br><br>    # Legacy:<br>    # if (beresp.http.Cache-Control ~ "(private|no-cache|no-store)") {<br>    #    return(hit_for_pass);<br>    # }<br><br>    # Legacy:<br>    # if (req.http.Authorization && !beresp.http.Cache-Control ~ "public") {<br>    #    return(hit_for_pass);<br>    # }<br>}<br><br><br>sub vcl_deliver {<br>    # For debugging only.<br>    # The approximate number of times the object has been delivered. A value of 0 indicates a cache miss.<br>    set resp.http.X-obj-hits = obj.hits;<br>}<br></div></body></html>