varnish and wordpress

Miguel González miguel_3_gonzalez at yahoo.es
Fri Jan 27 10:43:10 CET 2017


I kept on researching and I found this:

http://serverfault.com/questions/780590/inconsistent-cache-and-expires-headers-pragmanocache/

The last answer mentions an add-headers plugin, but this one is deprecated.

I will explore the php_set path or using wp-config.php just for one
website before thinking of making the change system wide.

Regards,

Miguel


On 01/27/17 1:08 AM, Carlos Fernandez wrote:
> I think you could do that through the php_set command or its analog in
> Nginx (we use Apache). Those settings can also be modified on a per
> session basis through PHP functions so that an application could control
> it (WordPress doesn't), so you could in theory put them in
> wp-config.php. You do have to set cache_limiter first, otherwise PHP
> will ignore the expiration time setting. The idea here is that it's PHP
> that generates the Expires and Cache-Control headers when a backend
> request goes to WordPress, not the web server (Apache/Nginx/IIS/etc).
> 
> 
> On Thu, Jan 26, 2017 at 18:33 Miguel González
> <miguel_3_gonzalez at yahoo.es <mailto:miguel_3_gonzalez at yahoo.es>> wrote:
> 
>     thanks for this information.
> 
>     Wouldn´t it help just setting in .htaccess:
> 
>     ExpiresByType text/html "access 1 month"
> 
>     ?
> 
>     Miguel
> 
> 
> 
>     On 01/26/17 10:42 PM, Carlos Fernandez wrote:
>     > Per php.net <http://php.net> <http://php.net>:
>     >
>     > "The cache limiter defines which cache control HTTP headers are
>     sent to
>     > the client. These headers determine the rules by which the page
>     content
>     > may be cached by the client and intermediate proxies. Setting the
>     cache
>     > limiter to /nocache/disallows any client/proxy caching. A value
>     > of /public/ permits caching by proxies and the client,
>     > whereas /private/ disallows caching by proxies and permits the
>     client to
>     > cache the contents."
>     >
>     > Varnish obeys the cache control headers in the backend response,
>     > therefore this setting (and cache_expires) indirectly affects
>     Varnish by
>     > way of the headers. You could alter them in VCL, but that takes
>     more work.
>     >
>     >
>     > On Thu, Jan 26, 2017 at 16:29 Miguel González
>     > <miguel_3_gonzalez at yahoo.es <mailto:miguel_3_gonzalez at yahoo.es>
>     <mailto:miguel_3_gonzalez at yahoo.es
>     <mailto:miguel_3_gonzalez at yahoo.es>>> wrote:
>     >
>     >     What is the relation of this setting with Varnish? I´m not
>     finding any
>     >     information about this setting and Varnish.
>     >
>     >     Thanks!
>     >
>     >     Miguel
>     >
>     >
>     >     On 01/26/17 9:22 PM, Carlos Fernandez wrote:
>     >     > That VCL looks similar to what we use. What does the
>     >     > "session.cache_limiter" setting in your php.ini file say? If
>     it's
>     >     set to
>     >     > "nocache" (the default), then PHP will always respond with
>     an Expires
>     >     > header set in the past so that Varnish will not cache it.
>     >     >
>     >     > On Thu, Jan 26, 2017 at 2:25 PM, Miguel González
>     >     > <miguel_3_gonzalez at yahoo.es
>     <mailto:miguel_3_gonzalez at yahoo.es>
>     <mailto:miguel_3_gonzalez at yahoo.es <mailto:miguel_3_gonzalez at yahoo.es>>
>     >     <mailto:miguel_3_gonzalez at yahoo.es
>     <mailto:miguel_3_gonzalez at yahoo.es>
>     >     <mailto:miguel_3_gonzalez at yahoo.es
>     <mailto:miguel_3_gonzalez at yahoo.es>>>> wrote:
>     >     >
>     >     >     Dear all,
>     >     >
>     >     >       Probably since I´m a newbie I assume a cache system
>     makes a
>     >     "static"
>     >     >     copy of a web and serve it to the browser. I have several
>     >     Wordpress
>     >     >     sites, same backend and speed load differs quite much
>     (from 1
>     >     second to
>     >     >     6 seconds). Obviously size matters but shouldn´t load all
>     >     items (CSS,
>     >     >     JS, images) almost at the same time? Webpagetest shows it´s
>     >     not the
>     >     >     case.
>     >     >
>     >     >       I have tried to use together with Varnish other plugin
>     >     caches as W3
>     >     >     Total Cache but the performance is even worse.
>     >     >
>     >     >       Maybe my assumptions are wrong or my VCL is wrong.
>     >     >
>     >     >       I have 4.1 as was suggested by someone from the list.
>     >     >
>     >     >      Thanks!
>     >     >
>     >     >      Miguel
>     >     >
>     >     >     my default.vcl:
>     >     >
>     >     >     #
>     >     >     # This is an example VCL file for Varnish.
>     >     >     #
>     >     >     # It does not do anything by default, delegating control
>     to the
>     >     >     # builtin VCL. The builtin VCL is called when there is
>     no explicit
>     >     >     # return statement.
>     >     >     #
>     >     >     # See the VCL chapters in the Users Guide at
>     >     >     https://www.varnish-cache.org/docs/
>     >     >     <https://www.varnish-cache.org/docs/>
>     >     >     # and http://varnish-cache.org/trac/wiki/VCLExamples
>     >     >     <http://varnish-cache.org/trac/wiki/VCLExamples> for more
>     >     examples.
>     >     >
>     >     >     # Marker to tell the VCL compiler that this VCL has been
>     >     adapted to the
>     >     >     # new 4.0 format.
>     >     >     vcl 4.0;
>     >     >
>     >     >     import std;
>     >     >
>     >     >     # Default backend definition. Set this to point to your
>     >     content server.
>     >     >     backend default {
>     >     >         .host = "127.0.0.1";
>     >     >         .port = "82";
>     >     >         .connect_timeout = 600s;
>     >     >         .first_byte_timeout = 600s;
>     >     >         .between_bytes_timeout = 600s;
>     >     >
>     >     >
>     >     >     }
>     >     >
>     >     >     acl purge {
>     >     >             "localhost";
>     >     >             "127.0.0.1";
>     >     >     }
>     >     >
>     >     >
>     >     >     # This function is used when a request is send by a HTTP
>     client
>     >     >     (Browser)
>     >     >     sub vcl_recv {
>     >     >
>     >     >             # remove ?ver=xxxxx strings from urls so css and js
>     >     files are
>     >     >     cached.
>     >     >             # Watch out when upgrading WordPress, need to
>     restart
>     >     Varnish or
>     >     >     flush cache.
>     >     >             set req.url = regsub(req.url, "\?ver=.*$", "");
>     >     >
>     >     >             # Remove "replytocom" from requests to make caching
>     >     better.
>     >     >             set req.url = regsub(req.url,
>     "\?replytocom=.*$", "");
>     >     >
>     >     >             #We pass real IP and Port to the backend
>     >     >
>     >     >             if (req.http.X-Forwarded-Proto == "https" ) {
>     >     >                set req.http.X-Port = "443";
>     >     >             } else {
>     >     >                set req.http.X-Port = "80";
>     >     >             }
>     >     >
>     >     >             set req.http.X-Forwarded-For =
>     >     regsub(req.http.X-Forwarded-For,
>     >     >     "^([^,]+),?.*$", "\1");
>     >     >
>     >     >
>     >     >             # Normalize the header, remove the port (in case
>     >     you're testing
>     >     >     this on various TCP ports)
>     >     >
>     >     >             set req.http.Host = regsub(req.http.Host,
>     ":[0-9]+", "");
>     >     >
>     >     >             # Remove has_js and CloudFlare/Google Analytics __*
>     >     cookies.
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     >     "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
>     >     >             # Remove a ";" prefix, if present.
>     >     >             set req.http.Cookie = regsub(req.http.Cookie,
>     "^;\s*",
>     >     "");
>     >     >
>     >     >
>     >     >             # Allow purging from ACL
>     >     >             if (req.method == "PURGE") {
>     >     >                     # If not allowed then a error 405 is
>     returned
>     >     >                     if (!client.ip ~ purge) {
>     >     >                             return(synth(405, "This IP is not
>     >     allowed to
>     >     >     send PURGE requests."));
>     >     >                     }
>     >     >                     # If allowed, do a cache_lookup ->
>     vlc_hit() or
>     >     >     vlc_miss()
>     >     >                     return (purge);
>     >     >             }
>     >     >
>     >     >             # Post requests will not be cached
>     >     >             #if (req.http.Authorization || req.method ==
>     "POST") {
>     >     >             #       return (pass);
>     >     >             #}
>     >     >
>     >     >             # Pass anything other than GET and HEAD directly.
>     >     >             if (req.method != "GET" && req.method != "HEAD") {
>     >     >                     return( pass );
>     >     >             }      /* We only deal with GET and HEAD by
>     default */
>     >     >
>     >     >
>     >     >             #Woocommerce don't cache :
>     >     >             if (req.url ~
>     >     >
>     >     
>     "^/(cart|my-account/*|checkout|addons|logout|lost-password|product/*)")
>     >     >     {
>     >     >               return (pass);
>     >     >             }
>     >     >
>     >     >             #Woocommerce add to cart pass :
>     >     >             if (req.url ~ "\?add-to-cart=" ) {
>     >     >               return (pass);
>     >     >             }
>     >     >             if (req.url ~ "/wp-cron.php" || req.url ~
>     >     "preview=true") {
>     >     >               return (pass);
>     >     >             }
>     >     >
>     >     >             # Woocommerce
>     >     >             if (req.url ~ "(cart|my-account|checkout|addons)") {
>     >     >               return (pass);
>     >     >             }
>     >     >             if ( req.url ~ "\?add-to-cart=" ) {
>     >     >               return (pass);
>     >     >             }
>     >     >
>     >     >
>     >     >             # --- WordPress specific configuration
>     >     >             # Did not cache the admin and login pages
>     >     >             if (req.url ~
>     >     >
>     >     
>     "nocache|cart|my-account|checkout|addons|tienda|mi-cuenta|carro|producto/*|login|wp-admin|wp-(comments-post|login|signup|activate|mail|cron)\.php|preview\=true|admin-ajax\.php|xmlrpc\.php|bb-admin|whm-server-status|server-status|control\.php|bb-login\.php|bb-reset-password\.php|register\.php")
>     >     >     {
>     >     >                 return (pass);
>     >     >             }
>     >     >
>     >     >             if (req.url ~ "(ajax|dynamic|custom)") {
>     >     >                  return(pass);
>     >     >             }
>     >     >
>     >     >             # Remove the "has_js" cookie
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     "has_js=[^;]+(;
>     >     >     )?", "");
>     >     >
>     >     >             # Remove any Google Analytics based cookies
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     "__utm.=[^;]+(;
>     >     >     )?", "");
>     >     >
>     >     >             # Remove the Quant Capital cookies (added by
>     some plugin,
>     >     >     all __qca)
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     "__qc.=[^;]+(;
>     >     >     )?", "");
>     >     >
>     >     >             # Remove the wp-settings-1 cookie
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     >     "wp-settings-1=[^;]+(; )?", "");
>     >     >
>     >     >             # Remove the wp-settings-time-1 cookie
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     >     "wp-settings-time-1=[^;]+(; )?", "");
>     >     >
>     >     >             # Remove the wp test cookie
>     >     >             set req.http.Cookie = regsuball(req.http.Cookie,
>     >     >     "wordpress_test_cookie=[^;]+(; )?", "");
>     >     >
>     >     >             # Are there cookies left with only spaces or
>     that are
>     >     empty?
>     >     >             if (req.http.cookie ~ "^ *$") {
>     >     >                         unset req.http.cookie;
>     >     >             }
>     >     >
>     >     >             # Cache the following files extensions
>     >     >             if (req.url ~
>     "\.(txt|css|js|png|gif|jp(e)?g|swf|ico)") {
>     >     >                     unset req.http.cookie;
>     >     >             }
>     >     >
>     >     >             # Normalize Accept-Encoding header and compression
>     >     >             #
>     >     https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
>     >     >     <https://www.varnish-cache.org/docs/3.0/tutorial/vary.html>
>     >     >             if (req.http.Accept-Encoding) {
>     >     >                     # Do no compress compressed files...
>     >     >                     if (req.url ~
>     >     >     "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
>     >     >                                     unset
>     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 {
>     >     >                             unset req.http.Accept-Encoding;
>     >     >                     }
>     >     >             }
>     >     >
>     >     >             # Check the cookies for wordpress-specific items
>     >     >             if (req.http.Cookie ~ "wordpress_" ||
>     req.http.Cookie ~
>     >     >     "comment_") {
>     >     >                     return (pass);
>     >     >             }
>     >     >             if (!req.http.cookie) {
>     >     >                     unset req.http.cookie;
>     >     >             }
>     >     >
>     >     >             # --- End of WordPress specific configuration
>     >     >
>     >     >             # Did not cache HTTP authentication and HTTP Cookie
>     >     >             if (req.http.Authorization || req.http.Cookie) {
>     >     >                     # Not cacheable by default
>     >     >                     return (pass);
>     >     >             }
>     >     >
>     >     >             # Cache all others requests
>     >     >             return (hash);
>     >     >     }
>     >     >
>     >     >     sub vcl_pipe {
>     >     >             return (pipe);
>     >     >     }
>     >     >
>     >     >     sub vcl_pass {
>     >     >             return (fetch);
>     >     >     }
>     >     >
>     >     >     # The data on which the hashing will take place
>     >     >     sub vcl_hash {
>     >     >             hash_data(req.url);
>     >     >             if (req.http.host) {
>     >     >             hash_data(req.http.host);
>     >     >             } else {
>     >     >             hash_data(server.ip);
>     >     >             }
>     >     >
>     >     >             # If the client supports compression, keep that in a
>     >     >     different cache
>     >     >             if (req.http.Accept-Encoding) {
>     >     >                     hash_data(req.http.Accept-Encoding);
>     >     >             }
>     >     >
>     >     >             return (lookup);
>     >     >     }
>     >     >
>     >     >     # This function is used when a request is sent by our
>     backend
>     >     (Nginx
>     >     >     server)
>     >     >     sub vcl_backend_response {
>     >     >             # Remove some headers we never want to see
>     >     >             unset beresp.http.Server;
>     >     >             unset beresp.http.X-Powered-By;
>     >     >
>     >     >             if (beresp.http.content-type ~
>     >     >     "(text|javascript|application/x-font-woff)") {
>     >     >               set beresp.do_gzip = true;
>     >     >             }
>     >     >
>     >     >             # For static content strip all backend cookies
>     >     >             if (bereq.url ~
>     "\.(css|js|png|gif|jp(e?)g)|swf|ico") {
>     >     >                     unset beresp.http.cookie;
>     >     >             }
>     >     >             # Don't store backend
>     >     >             if (bereq.url ~ "wp-(login|admin)" || bereq.url ~
>     >     >     "preview=true") {
>     >     >                     set beresp.uncacheable = true;
>     >     >                     set beresp.ttl = 30s;
>     >     >                     return (deliver);
>     >     >             }
>     >     >
>     >     >             # Only allow cookies to be set if we're in admin
>     area
>     >     >                     if (!(bereq.url ~
>     >     >
>     >     
>     "(wp-login|cart|my-account|checkout|addons|tienda|mi-cuenta|carro|producto/*|login|wp-admin|preview=true)"))
>     >     >     {
>     >     >                     unset beresp.http.set-cookie;
>     >     >             }
>     >     >
>     >     >             # don't cache response to posted requests or
>     those with
>     >     >     basic auth
>     >     >             if ( bereq.method == "POST" ||
>     >     bereq.http.Authorization ) {
>     >     >                     set beresp.uncacheable = true;
>     >     >                     set beresp.ttl = 120s;
>     >     >                     return (deliver);
>     >     >             }
>     >     >
>     >     >             # don't cache search results
>     >     >             if ( bereq.url ~ "\?s=" ){
>     >     >                     set beresp.uncacheable = true;
>     >     >                     set beresp.ttl = 120s;
>     >     >                     return (deliver);
>     >     >             }
>     >     >
>     >     >             # only cache status ok
>     >     >             if ( beresp.status != 200 ) {
>     >     >                     set beresp.uncacheable = true;
>     >     >                     set beresp.ttl = 120s;
>     >     >                     return (deliver);
>     >     >             }
>     >     >
>     >     >             # A TTL of 24h
>     >     >             set beresp.ttl = 24h;
>     >     >             # Define the default grace period to serve
>     cached content
>     >     >             #set beresp.grace = 30s;
>     >     >             set beresp.grace = 1h;
>     >     >
>     >     >             return (deliver);
>     >     >     }
>     >     >
>     >     >     # The routine when we deliver the HTTP request to the user
>     >     >     # Last chance to modify headers that are sent to the client
>     >     >     sub vcl_deliver {
>     >     >             if (obj.hits > 0) {
>     >     >                     set resp.http.X-Cache = "cached";
>     >     >             } else {
>     >     >                     set resp.http.x-Cache = "uncached";
>     >     >             }
>     >     >
>     >     >             # Remove some headers: PHP version
>     >     >             unset resp.http.X-Powered-By;
>     >     >
>     >     >             # Remove some headers: Apache version & OS
>     >     >             unset resp.http.Server;
>     >     >
>     >     >             # Remove some heanders: Varnish
>     >     >             unset resp.http.Via;
>     >     >             unset resp.http.X-Varnish;
>     >     >
>     >     >             return (deliver);
>     >     >     }
>     >     >
>     >     >     sub vcl_init {
>     >     >             return (ok);
>     >     >     }
>     >     >
>     >     >     sub vcl_fini {
>     >     >             return (ok);
>     >     >     }
>     >     >
>     >     >     _______________________________________________
>     >     >     varnish-misc mailing list
>     >     >     varnish-misc at varnish-cache.org
>     <mailto:varnish-misc at varnish-cache.org>
>     >     <mailto:varnish-misc at varnish-cache.org
>     <mailto:varnish-misc at varnish-cache.org>>
>     >     <mailto:varnish-misc at varnish-cache.org
>     <mailto:varnish-misc at varnish-cache.org>
>     >     <mailto:varnish-misc at varnish-cache.org
>     <mailto:varnish-misc at varnish-cache.org>>>
>     >     >   
>      https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>     >     >
>     >     
>     <https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc>
>     >     >
>     >     >
>     >     >
>     >     >
>     >     > --
>     >     >
>     >     > Best regards,
>     >     >
>     >     > --
>     >     >
>     >     > Carlos M. Fernández
>     >     >
>     >     > Enterprise Systems Manager
>     >     >
>     >     > *Saint Joseph’s University*
>     >     >
>     >     > Philadelphia PA 19131
>     >     >
>     >     > T: +1 610 660 1501
>     >     >
>     >
>     >
>     >     _______________________________________________
>     >     varnish-misc mailing list
>     >     varnish-misc at varnish-cache.org
>     <mailto:varnish-misc at varnish-cache.org>
>     <mailto:varnish-misc at varnish-cache.org
>     <mailto:varnish-misc at varnish-cache.org>>
>     >     https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>     >
>     > --
>     >
>     > Best regards,
>     >
>     > --
>     >
>     > Carlos M. Fernández
>     >
>     > Enterprise Systems Manager
>     >
>     > *Saint Joseph’s University*
>     >
>     > Philadelphia PA 19131
>     >
>     > T: +1 610 660 1501
>     >
> 
> 
>     _______________________________________________
>     varnish-misc mailing list
>     varnish-misc at varnish-cache.org <mailto:varnish-misc at varnish-cache.org>
>     https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
> 
> -- 
> 
> Best regards,
> 
> --
> 
> Carlos M. Fernández
> 
> Enterprise Systems Manager
> 
> *Saint Joseph’s University*
> 
> Philadelphia PA 19131
> 
> T: +1 610 660 1501
> 




More information about the varnish-misc mailing list