fbpx

NGINX

Supercharging WordPress with NGINX Cache Purge – Say Goodbye to Mounts and Permissions Hassles!

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

Welcome to the world of powerful and efficient NGINX caching for WordPress on RPM-based systems! If you’re a GetPageSpeed RPM repository subscriber, you’re already one step ahead. With access to the ngx_cache_purge, you’re empowered to manage cache purges in an incredibly seamless and robust way—without the typical complexity of permissions and mount setups. In this guide, we’ll explore how to set up and use ngx_cache_purge efficiently, show you how to integrate it with WordPress plugins, and explain why it’s far superior to alternative, convoluted setups.

Why use ngx_cache_purge?

NGINX caching is one of the best ways to boost performance for WordPress by storing responses for quick access. When set up with ngx_cache_purge and corresponding website integrations, NGINX can automatically remove outdated cached content whenever you update posts, comments, or pages. Say goodbye to permission headaches, file mounts, or bindfs configurations—the ngx_cache_purge module makes cache purging simple and hassle-free.

How to install ngx_cache_purge in CentOS/RHEL or Amazon Linux

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install nginx-module-cache-purge

To enable this module, add the following to /etc/nginx/nginx.conf and reload nginx:

load_module modules/ngx_http_cache_purge_module.so;

Setting Up FastCGI Caching with ngx_cache_purge

Now, let’s configure NGINX to cache your WordPress content and enable efficient purging.

1. Define Your Cache Zone

In your main NGINX configuration file (/etc/nginx/nginx.conf), add the following to set up a caching path and define the cache keys:

http {
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=wordpress_cache:10m inactive=60m use_temp_path=off;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
}

2. Configure Your Server Block for WordPress Caching

In your WordPress site’s server block (usually under /etc/nginx/sites-available.d/), apply the following settings:

server {
    server_name example.com www.example.com;

    location / {
        fastcgi_pass 127.0.0.1:9000;  # Replace with your PHP-FPM server address
        fastcgi_cache wordpress_cache;
        fastcgi_cache_valid 200 60m;  # Cache successful responses for 60 minutes
        fastcgi_cache_use_stale error timeout updating;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_lock on;
        fastcgi_cache_purge PURGE from 127.0.0.1;  # Allow purge from localhost
    }
}

This setup ensures that all cache keys are generated based on the URL and are purged with a simple PURGE request.

Integrating with WordPress

WordPress Proxy Cache Purge plugin make this setup even smoother. With this plugin, you won’t have to manually purge cache each time you make updates to your website. Here’s how to integrate effectively:

1. Install the Proxy Cache Purge Plugin:

The Proxy Cache Purge plugin is perfect for NGINX users. It automatically purges cache when content updates happen, and no extra permissions or mounts are needed!
Install the plugin from WordPress by navigating to Plugins > Add New and searching for Proxy Cache Purge.

2. Configure Proxy Cache Purge:

Once installed, go to Proxy Cache > Settings in WordPress and specify 127.0.0.1 in the “Set Custom IP” field.
This way, the plugin will send PURGE requests directly to your NGINX server at the configured cache zone.

Advanced ngx_cache_purge Configurations

For even more control, ngx_cache_purge offers powerful configurations. Here are some additional options to explore:

Partial Purging:

If you want to purge multiple pages with a single request, ngx_cache_purge supports wildcards. Simply add an asterisk at the end of the URL to purge by pattern.

curl -X PURGE "https://yoursite.com/page/*"

IP-Based Purging:

Restrict purging capabilities to specific IP addresses for security. This way, only requests from authorized locations will be able to clear the cache.

fastcgi_cache_purge PURGE from 127.0.0.1 192.168.1.0/24;

Custom Cache Purge Response Types:

Tailor the response format for successful purges using cache_purge_response_type directive of the NGINX module:

cache_purge_response_type json;

Wrapping Up: No More Permissions Nightmares!

Unlike other convoluted setups that attempt to “fix” permissions using bindfs, ACLs, or other hacks, ngx_cache_purge is a purpose-built solution for managing cache purges directly in NGINX. It works out-of-the-box with WordPress, integrates with plugins, and saves you hours of headaches.

If you’re serious about WordPress performance, this setup with GetPageSpeed’s repository makes it simple, powerful, and secure. Avoid the madness of mounts, permissions, and file ownership issues by embracing ngx_cache_purge — your WordPress site and your sanity will thank you.

For more information on GetPageSpeed’s repository or to access even more NGINX modules, visit our NGINX Extras page.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.