Manual talk:Varnish caching

Latest comment: 2 years ago by Pspviwki in topic Automatic purge after page update

if-none-match support added?

edit

Given this changeset purportedly added support, should we remove the section on "if-none-match" in vcl_recv? GreenReaper 04:14, 9 August 2010 (UTC)Reply

The advice here, helped, but I had a lingering problem with 127.0.0.1 in my recent changes. adding

    remove req.http.X-Forwarded-For;
    set    req.http.X-Forwarded-For = client.ip;

to my vcl_recv seemed to do the trick. Cariaso 18:55, 9 July 2011 (UTC)Reply

Varnish 3.0

edit

The Varnish 3.0 package on Ubuntu installs the initial config file under /etc/default/varnish. It also requires you to explicitly state "return" - such as:

return(lookup);

More info and a few other things (like "purge_url" is now "ban_url"):

Upgrading from Varnish 2.1 to 3.0

Varnish 3.0 config file example?

edit

Hi, where can I get a *good* example of a varnish 3.0 configuration file? The one on the manual did not helped me. Another question: what should one insert/replace for "^images.example.org$"? A subdomain of my domain where all images are stored?

Thank you and best regards, --Mrnett1974 17:15, 21 January 2012 (UTC)Reply

The "images" server only applies to a site where the content is split among multiple webservers on different domains, with (typically) the most obsolete hardware (P4, Celeron...) and small, low-powered server software (lighttpd, nginx) used to serve images and static content while more powerful servers handle the database, Apache and dynamic wiki content. A site using a content delivery network would need this - in Wikimedia's case, the image servers are on 'upload.wikimedia.org' and the wiki is on '*.wikipedia.org'. If your site doesn't need it (and many single-server sites do not), don't use it. --Carlb (talk) 21:17, 24 February 2012 (UTC)Reply

Purge updated pages with Varnish 2.x

edit

I'm using MW 1.19.0 and Varnish 2.1.5. Automatic purging of updated pages did not work until I applied the patch described in this post. For your convenience, here's my patch adapted from the one in the above post:

--- a/includes/SquidPurgeClient.php
+++ b/includes/SquidPurgeClient.php
@@ -148,7 +148,21 @@
         */
        public function queuePurge( $url ) {
                $url = SquidUpdate::expand( str_replace( "\n", '', $url ) );
-               $this->requests[] = "PURGE $url HTTP/1.0\r\n" .
+               # Modify PURGE request for Varnish 2.x and up
+               # See http://www.mediawiki.org/wiki/Thread:Project:Support_desk/Varnish_purging_not_working_since_Varnish_2.x
+               # Basically, we need to convert 
+               #    PURGE http://wiki.example.com/mediawiki/index.php?title=Sandbox&action=history HTTP/1.0
+               # to
+               #    PURGE /mediawiki/index.php?title=Sandbox&action=history HTTP/1.0
+               #    Host: wiki.example.com
+               # in the purge request header.
+               $urlArr = parse_url( $url );
+               if( $urlArr['query'] != "" )
+                       $urlQuery = "?" . $urlArr['query'];
+               else
+                       $urlQuery = "";
+               $this->requests[] = "PURGE " . $urlArr['path'] . $urlQuery . " HTTP/1.0\r\n" .
+                       "Host: " . $urlArr['host'] . "\r\n" .
                        "Connection: Keep-Alive\r\n" .
                        "Proxy-Connection: Keep-Alive\r\n" .
                        "User-Agent: " . Http::userAgent() . ' ' . __CLASS__ . "\r\n\r\n";

Varnish 4 purging

edit

I don't think the vcl_hit/vcl_miss is necessary the official documentation describes a new way to do it using return (purge);. Daniel Friesen (Dantman) (talk) 01:56, 1 March 2015 (UTC)Reply

Varnish 4.x not working & mobile example?

edit

Hi! I refer to my question on serverfault.com (where i asked because i thought it was an error from me :)), but it seems, that the example Varnish 4.x configuration doesn't work and has an error. After uncommenting this directive:

if (beresp.ttl < 48h) {
   set beresp.uncacheable = true;
   return (deliver);
}

pages are cached by varnish. Before that the backend delivered the actual page. Now the question: Why was this directive added? What should it do?

And another question: Can anyone give an example how to handle requests from mobile devices, if MobileFrontend is installed? I couldn't find any examples (and not the implementation of the wmf?)? Best, Florianschmidtwelzow (talk) 23:37, 10 May 2015 (UTC)Reply

purging does not work with Varnish 4

edit

https://www.mediawiki.org/wiki/Manual:Varnish_caching#Configuring_Varnish_4.x

If you use the above setting before 8/17/2015 . The purging function does not work at all.

The correct configue should be return(purge);} not return hash in 4.x version. (Under # This uses the ACL action called "purge". Basically if a request to.......)

I have changed the Manual and Now the new setting is correct. --Zoglun (talk) 20:52, 17 August 2015 (UTC)Reply

Is it possible to deliver stale content to login user with Varnish?

edit

Hello,

I am trying to server anonymous cached page to login user when back-end down.

So I have read this page Manual:Varnish_caching and set up Varnish fro Mediawiki successfully. However the back end were not very stable, especially when high traffic comming in suddenly. Based on this article: https://info.varnish-software.com/blog/configure-saint-mode-grace-varnish-4.1 , I got the grace mode work for anonymous user. However, due to the fact that login user will pass to back-end. I can't server anonymous page to login user.

if (req.http.Authorization || req.http.Cookie ~ "session" || req.http.Cookie ~ "Token") { return (pass); } #(varnish sitting cite from Manual:Varnish_caching)

How should I do to achive this goal?

Thank you!

Caching Special:Random

edit

On two wikis I've configured to use Varnish 6 with the instructions on this page, I discovered that Special:Random always pointed to one page for logged-out users because Varnish was caching it. I was not able to find any information about this situation. After searching for how to exclude a particular URL from Varnish, I managed to solve the problem by inserting this piece of code into default.vcl (in the sub vcl_recv block, above the return (hash); line):

	if (req.url ~ "^/wiki/Special:Random") {
		return (pass);
	}

I hope this helps someone else. I don't know what the accepted solution is; this is quite likely not it, but it gets the job done. 9cfilorux (talk) 23:00, 11 October 2019 (UTC)Reply

Varnish not starting (default.vcl' Line 76 Pos 21)

edit

The backend server is ubuntu 20.04 LTS with varnish 4 installed.

Using the suggested configuration caused this error:

  • Feb 18 15:45:34 mainhost varnishd[1750650]: Error:
  • Feb 18 15:45:34 mainhost varnishd[1750650]: Message from VCC-compiler:
  • Feb 18 15:45:34 mainhost varnishd[1750650]: Expected return action name.
  • Feb 18 15:45:34 mainhost varnishd[1750650]: ('/etc/varnish/default.vcl' Line 76 Pos 21)
  • Feb 18 15:45:34 mainhost varnishd[1750650]: return (miss);
  • Feb 18 15:45:34 mainhost varnishd[1750650]: --------------------####--
  • Feb 18 15:45:34 mainhost varnishd[1750650]: Running VCC-compiler failed, exited with 2
  • Feb 18 15:45:34 mainhost varnishd[1750650]: VCL compilation failed
  • Feb 18 15:45:34 mainhost systemd[1]: varnish.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
  • Feb 18 15:45:34 mainhost systemd[1]: varnish.service: Failed with result 'exit-code'.

Nginx Cache

edit

On my shared hosting, there is a website accelerator with Nginx. Is it possible to add in the localsettings.php for a better service

$wgUseCdn = true;
$wgCdnServers = array();
$wgCdnServers[] = "127.0.0.1";

Gribouillot (talk) 07:33, 13 April 2021 (UTC)Reply

No, this won't work. I tried it years ago, to use the proxy response caching of nginx, but it only supports static files. It doesn't support HTTP purges. I originally wrote phab:T178629 about that but it wasn't accepted. nginx is not a valid cache proxy for dynamic content. Use varnish for that. --Ciencia Al Poder (talk) 09:13, 13 April 2021 (UTC)Reply

Visual Editor

edit

To solve Visual Editor error 405, just configure Varnish to pass rest.php and api.php requests directly:

    # Visual Editor API calls
    if (req.url ~ "api.php" || req.url ~ "rest.php") {
        return (pass);
    }

Automatic purge after page update

edit

Varnish 7 and mediawiki (1.37, 1.38) work fine except automatic purge after page update. The setting $wgUseCdn = true; $wgCdnServers = [ '127.0.0.1']; or any permutation of that does not invoke anything in varnish, varnishstat does not indicate any purge/ban request. There is workaround like Manual:Job queue reading recent changes regularly and sending purge requests to varnish but it would be nice to achieve that natively. Pspviwki (talk) 17:45, 25 August 2022 (UTC)Reply

If you set a debug log, you should see the PURGE URLs being sent to varnish. Try to request them with curl and see if that works. Ciencia Al Poder (talk) 17:36, 9 September 2022 (UTC)Reply
Nothing in there. It logs other actions such as search or page access, but nothing for purge. I check the purge (success) in access_log, error_log, varnishstat, varnishncsa but not in debug log. Pspviwki (talk) 21:08, 17 September 2022 (UTC)Reply
Configuration that makes mediawiki communicate successfully with varnish is
$wgInternalServer = "http://ip address:port";
$wgUseCdn = true;
$wgCdnServers = array();
$wgCdnServers[] = "ip address:port"; It shows purges sent successfully in debug log file. Up to varnish to handle them. Purges from external script do not show as purges in debuglog but work. Pspviwki (talk) 20:59, 29 September 2022 (UTC)Reply
Return to "Varnish caching" page.