Time-of-day caching?

Quick note of intro: I’m certainly not a highly-experienced sys admin or
developer – more of a “gifted amateur” as a friend once called me :slight_smile:
Anyway:

I have a large and fairly complex WordPress-based site that’s become a
lot more popular than expected, and is often close to maxing out my
hardware. My challenge in optimizing the site is that all of the most
popular pages/files are very time-sensitive – they’re updated at
specific times of day, and are very heavily downloaded as soon as
they’re available. So: I need those pages/files to be flushed from the
cache the moment they change, while everything else is cached more
aggressively.

I haven’t found a good solution for this with WordPress plug-ins such as
SuperCache – they seem to be too “all-or-nothing”, flushing too much or
too little.

I’ve been tinkering with Nginx as a reverse proxy sitting in front of
Apache. I’m getting very good results so far with regular caching, but I
don’t see a way to get that “time-based cache granularity” I need. But
as I say, I’m no expert.

Any thoughts? Thanks.

Posted at Nginx Forum:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/28/2011 04:53 PM, Brent wrote:

So: I need those pages/files to be flushed from the
cache the moment they change, while everything else is cached more
aggressively.

You can do this with Expires header.

I haven’t found a good solution for this with WordPress plug-ins such as
SuperCache – they seem to be too “all-or-nothing”, flushing too much or
too little.

SuperCache will create static files from your dynamic pages or “posts”
and everything else is configured by Expires.

I’m not nginx guru, but I’ve this setup:

    • Varnish as cache
    • nginx as webserver with spawn-fcgi
  • “static files generated by SuperCache”: posts.html with 10 minuts of
    Expires
  • static files: images, js and everything else with ‘max’

http://pastebin.com/jfy9GgkN

Hope this helps.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1sBkkACgkQNRQApncg297GMQCgkd6DScym6Q2EXipM1+rg18Sd
9u0An1NqXlXsKdg+tZcytMZzqMUOfnXF
=hdPD
-----END PGP SIGNATURE-----

On 28 Fev 2011 19h53 WET, [email protected] wrote:

the cache the moment they change, while everything else is cached
more aggressively.

I’m not much of a WP guy, mostly Drupal ;), but I think that this WP
plugin can be of assistance in your quest:

You need to install the cache purge 3rd party module:

Note that this is a route that voids solutions like SuperCache. It’s
more complex to setup, but ultimately much more flexible.

I haven’t found a good solution for this with WordPress plug-ins
such as SuperCache – they seem to be too “all-or-nothing”, flushing
too much or too little.

I’ve been tinkering with Nginx as a reverse proxy sitting in front
of Apache. I’m getting very good results so far with regular
caching, but I don’t see a way to get that “time-based cache
granularity” I need. But as I say, I’m no expert.

Any thoughts? Thanks.

Since you’re already using Nginx as a reverse proxy is a “natural”
fit.

No need to use Varnish.

My $.02
— appa

Hi there,

We are currently using wordpress (many of them) running in apache with
nginx in front of it using proxy_cache.

Currently we are not managing anymore the wordpress and our customers
do with them as they wish, some have cache enabled, some not.
We wanted to use that module but as our customer have many different
versions of wordpress it didn’t fit for us.

Right now we cache every static file for a day and all posts for 10
minutes. To deal with the dynamic content (comments) we create a
cache_key that uses the wordpress comment author cookie, so users do
not feel the cache of the posts when submitting comments but it’s just
a perception, in fact the 10 minutes it’s still happening it’s just
they do not see it.

Our current config for this it’s not rocket science but if anyone want
it I would gladly share it.

Regarding drupal may I send you later an e-mail asking for a tip? I
want to cache a few hundred drupal sites with nginx and right now I’m
passing to apache all pages requests as I’ve not found yet a nice way
to deal with drupal creating a php session cookie for every user and
as comments do not go to a specific url neither create a unique
cookie, I can’t detect them so easy as in wordpress.

By the way, does anyone have in hand a simple way to use proxy_cache
for all requests except POST ?

Have a nice day

Guzmn

On Mon, Feb 28, 2011 at 10:03 PM, Antnio P. P. Almeida

Flavio, António and Guzmán:

Thanks for your very useful suggestions. I think I see a possible
solution based on your input. I’ll be sure to post back if things work
as I hope they will.

Posted at Nginx Forum:

On 1 Mar 2011 17h04 WET, [email protected] wrote:

Hi there,

We are currently using wordpress (many of them) running in apache
with nginx in front of it using proxy_cache.

Currently we are not managing anymore the wordpress and our
customers do with them as they wish, some have cache enabled, some
not. We wanted to use that module but as our customer have many
different versions of wordpress it didn’t fit for us.

Yes. It’s tricky. But a very good solution, since you can just use
“one cache to rule them all”.

Right now we cache every static file for a day and all posts for 10
minutes. To deal with the dynamic content (comments) we create a
cache_key that uses the wordpress comment author cookie, so users do
not feel the cache of the posts when submitting comments but it’s
just a perception, in fact the 10 minutes it’s still happening it’s
just they do not see it.

Do you use ajax to update only fragments of the page and leave the
remainder unchanged?

Our current config for this it’s not rocket science but if anyone
want it I would gladly share it.

Regarding drupal may I send you later an e-mail asking for a tip? I
want to cache a few hundred drupal sites with nginx and right now
I’m passing to apache all pages requests as I’ve not found yet a
nice way to deal with drupal creating a php session cookie for every
user and as comments do not go to a specific url neither create a
unique cookie, I can’t detect them so easy as in wordpress.

Sure you can. May I suggest also for you to drop by the Nginx group at
groups.drupal.org? Nginx | Drupal Groups

Drupal 7 serves no cookie for anon users. Drupal 6 does. Unless you’re
using pressflow, which doesn’t. The removal of the cookie is related
to caching strategies.

When you post a comment there are specific patterns on the URI. Other
option is creating a module that sets a cookie when posting comments,
similar to what WP does.

By the way, does anyone have in hand a simple way to use proxy_cache
for all requests except POST ?

How about setting a var that busts the cache when non zero. E.g.:

if ($request_method = POST) {
set $no_cache 1;
}

then use it in Module ngx_http_proxy_module

— appa