Recompiling Nginx to Add ngx_cache_purge Module
This guide explains how to recompile Nginx in an LNMP environment to add the ngx_cache_purge module and configure FastCGI caching for WordPress performance optimization.
Check Module Status
First, verify if the module is already installed:
nginx -V 2>&1 | grep -o ngx_cache_purge
If no output appears, proceed with installation.
Install ngx_cache_purge Module
Using LNMP 1.2 as an example (adjust paths for other versions):
- Navigate to source directory and download module:
cd /root/lnmp1.2-full/src wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar xzf ngx_cache_purge-2.3.tar.gz cd nginx-1.8.0 - Check current compilation arguments:
nginx -VCopy the
configure argumentsline. - Add module to arguments and reconfigure:
./configure [your_copied_arguments] --add-module=../ngx_cache_purge-2.3 - Compile without installing:
make - Backup and replace binary:
mv /usr/local/nginx/sbin/nginx{,_$(date +%F)} cp objs/nginx /usr/local/nginx/sbin - Verify installation:
nginx -V 2>&1 | grep -o ngx_cache_purge
Configuring FastCGI Cache for WordPress
Cache Path Considerations
For optimal performance, place cache in tmpfs (memory filesystem):
- Faster: RAM access exceeds disk speed.
- Temporary: Data clears on reboot.
- Default paths: CentOS:
/dev/shm/; Ubuntu/Debian:/run/shm.
Modify Nginx Configuration
Edit your site's Nginx config file (e.g., /usr/local/nginx/conf/vhost/site_name.conf).
Part 1: Cache Definition
fastcgi_cache_path /dev/shm/ levels=1:2 keys_zone=WORDPRESS:200m inactive=1d max_size=350M;
fastcgi_temp_path /dev/shm/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
Part 2: Bypass Conditions
set $skip_cache 0;
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; }
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; }
Part 3: PHP Location Block
location ~ [^/].php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache "$upstream_cache_status From $host";
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
}
Part 4: Cache Purging (Optional)
location ~ /purge(/.*) {
allow 127.0.0.1;
allow YOUR_SERVER_PUBLIC_IP; # Replace with actual IP
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
Save and restart Nginx: service nginx reload.
Handle tmpfs After Reboot
Add to /etc/init.d/nginx in the start section:
if [ ! -d '/dev/shm/your_cache_folder' ]; then
mkdir /dev/shm/your_cache_folder
chown -R ${user}.$user /dev/shm/your_cache_folder
fi
WordPress Configuration
Install Nginx Helper Plugin
Install and activate the Nginx Helper plugin from WordPress admin for automatic cache purging.
Modify wp-config.php
Add to your WordPress root wp-config.php:
define('RT_WP_NGINX_HELPER_CACHE_PATH', '/dev/shm');
Restart Nginx for changes to take effect.