Mild-Mannered Canadian Fury

Doug Stephen is Politely Peeved

Using nginx as a Reverse Caching Proxy for Wordpress

Wed, 04 Apr 2012 Ā«permalinkĀ»

Darian Moody describes how to set up an nginx reverse caching proxy for WordPress on Apache.

I use nginx as the primary HTTP server for all of the sites that I run both for myself and for clients. It’s phenomenally fast and performant as a static asset server. However, it’s relatively immature and its event-loop model less tuned to performing harder lifting such as dynamic content generation; PHP or Ruby for example. While there are modules out there that allow you to bake this ability in to nginx, you are more often than not better served by sticking with the tried and true Apache HTTP server, set up to live behind nginx handling requests for dynamic processing.

Using this configuration, you simply set nginx to listen to requests coming in from the Internet (port 80), and Apache listens for local requests from nginx (port 81 on localhost, for example). You then sweeten the pot by having nginx cache the HTML output from Apache in cases where it makes sense (the landing page of your blog, or the permalink for an article, for example).

I’ve created a few WordPress sites that follow this exact structure, but up until this point I had always used the WPSuperCache plugin to cache dynamic output to static assets; this has the downside that Apache is handling the static requests and bubbling them up to nginx. It is still faster than just having Apache listening on port 80, but not as fast as it could be. This is because the documentation for nginx is, in my opinion, a little lacking for people who don’t have experience with other servers. nginx seems to have positioned itself as a server to graduate to, not a server to start off with. Unfortunately, my career on the web had me start with nginx, as I have only been working as a site developer for a couple of years and by that time nginx had already established itself as a killer server.

This article by Darian Moody elocutes perfectly the information you need to set up a flexible, performant, cached WordPress site using nginx and Apache, with all consideration in place for things like the admin panel and login screens where you wouldn’t typically want to have a cache in place. Definitely worth a read if you do any sort of work with web servers.