Everyone loves speed. That includes your site’s visitors. If you run a WordPress site, WP Super Cache is a pretty cool plugin that generates static files from your dynamic content, and serves those to your users instead of dynamically generating the same page for each user, which can really put your database to work.
If your current WordPress installation runs on Apache, and you want to give it an easy dose of speed, there’s a solution for you. nginx is a very lightweight and performant webserver that not only serves static files fast, but it caches, too. We can put both of those features to use to make your site faster than ever.
First, download and install WP Super Cache. Next, you’ll need to install nginx (I use the official nginx repos, but any recent build should be good). Don’t start it yet. Take the config file below, and adjust it to your needs (mainly the section at the top). Pop it into the nginx conf.d directory as mysite.com (adjust the name, of course). Next, adjust your Apache config’s Listen directive to listen on port 8080. In your virtualhost config for your site (which you can find with apachectl -S), update the port from 80 to 8080.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
server { listen *:80; server_name *.mysite.com; root /var/www/mysite; access_log /var/log/nginx/mysite_access_log main; error_log /var/log/nginx/mysite_error_log; # Cache by default set $no_cache ""; # Don't cache uris containing the following segments if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $no_cache 1; } # Don't use the cache for logged in users or recent commenters if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $no_cache 1; } if ($request_method != GET) { set $no_cache 1; } # Force cache the following URLs if ($request_uri ~* "^/css/|^/js/") { set $no_cache 0; } location / { add_header X-WP-SC "True"; try_files /wp-content/cache/supercache/$host$request_uri$no_cache/index.html @proxy; } location @proxy { add_header X-Proxy-No-Cache $no_cache; add_header X-Proxy-Cache-Status $upstream_cache_status; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache main; set $cache_key $scheme$host$uri$is_args$args; # Set the proxy cache key proxy_cache_key $cache_key; proxy_cache_valid 200 15m; # 200s will be cached. proxy_cache_use_stale error timeout updating invalid_header http_500 http_502 http_504 http_404; proxy_cache_bypass $no_cache; # Do not cache the response. proxy_no_cache $no_cache; proxy_pass http://<Apache IP>:8080; } } |
Now, do the following:
1 |
nginx -t && apachectl restart && service nginx start |
The above will test the nginx and Apache configs (apachectl will do a configtest before a restart), and start nginx if all is well.
In case the nginx config wasn’t clear, it will serve the static pages created by WP Super Cache directly from the filesystem if they exist. Otherwise, the request will be proxied to Apache, where the page will be generated (and in most cases, WP Super Cache will create a static file for it) and returned to nginx. nginx will cache that request for 15 minutes. If a user requests the same page again, if WP Super Cache indeed cached that page, it’ll be served from the filesystem, otherwise nginx will serve it from its cache (if it’s within the 15 minute window), or it’ll proxy it back to Apache.
Users who are logged in or who have left comments will not be served any cached data. This is determined via a cookie check. So, if an anonymous user has a blazing user experience, then comments on a post, he/she can suffer with slower speeds until the comment cookie expires (typically 5-30 minutes) since each request will be proxied to Apache for processing. Just something to keep in mind.
If you try this, let me know how it goes!
Leave a Reply