Home » WordPress Development » Caching content and bringing it closer to home

Caching content and bringing it closer to home

I wrote a little while ago about my first Amazon setup for hosting a WordPress install, and one of the objectives was to have it hosted out of the USA, but use Amazon’s Route53 service to geo-route the DNS to an Australian proxy server.

Until now, the nginx server based in Sydney has only being doing a passthrough, which gave a quicker initial connect time for Australian visitors to the site, but didn’t really increase the page speed much..

Setting up nginx

To set this up, I used some of the methods Darian Moody documented on his blog post: Using Nginx as a reverse caching proxy for WordPress: the setup. Essentially, you need to tell nginx when not to do caching like in most standard WordPress nginx caching. This means don’t cache logged in users, or people who have just left a comment, etc.

To test this, I ran WebPageTest.org from the Sydney location, and it scored a D for first byte, as the request had to go to the USA (via the AU server). Once the nginx configuration updates were made, I ran the test twice again, and it got an A and B (respectfully).

Another thing I wanted to do, was add a header into the nginx result to passthrough if it was a cache hit or miss, so both the client and I could check that it was working from the browser. Thankfully, other people have had the same request, and it’s as easy as adding the following line to your nginx configuration

add_header X-Cached $upstream_cache_status;

This returns either HIT or BYPASS. HIT when I’m using chrome in privacy mode (no auth cookies set), or BYPASS when logged in.

How long to cache for?

This is something I did differently to Darian had specified, which was 30 minutes. To me, 30 minutes for a guest can be a long time. I set most page caches to 5 minutes.

Firstly, the chance of something being posted and then quickly updated if there was an error isn’t good (Yes, you could setup WordPress to ping the nginx server and flush it’s cache, but I’m talking out of the box)

Secondly, things like comments and social sharing assets could become irrelevant quickly. EG, if a post is new and takes up to half an hour for comments to show, or not, it could be useless for the user. Again, you could tell WordPress to ping the nginx server and purge the cache, but if there are lots of comments going in, you then need to make sure you’re not attacking the nginx servers with purge requests.

Again, the reason I’m doing this caching is because I want to speed up the page load for Australian users, and to also prevent load to the server if the site did get flooded with a short burst of traffic, this AU proxy would take most of the load.

Ben May

Ben is Managing Director of The Code Company. He is passionate about working with publishers on clever and innovative ways to solve complex problems. He works with The Code Company team on all projects, bringing his perspective and problem solving skills to deliver great outcomes.

Do you work in digital media or publishing? Join our newsletter.

If you've made it this far on one of our posts, you're probably someone who'd find value in our email newsletter where we send analysis, insights and curated links for the digital publishing and media world. You can subscribe below and opt out any time.