How to Add ngx_cache_purge:

I spent weeks to understand Nginx with ngx_cache_purge

One of the old and gold technology used in VPs hosting cloud for WordPress  that researched involved with Nginx;s FastCGI support, ngx_cache_purge module that allows for targeted cache purging.

rtCamp have published a great starting tutorial on this, fastcgi_cache with conditional purging, but its initial steps But not saying how to compiling Nginx plus ngx_cache_purge. You can install and complie Nginx and ngx_cache_purge module from the scratch.


I’m sure if you installing and compiling then you familiar with command lines. Step-by-step instructions are below, but to summarise, I downloaded the source into /tmp, compiled and installed the new binary, configured Nginx, and restarted the service.

The best bit use: su, to root, and sudo will work perfectly.

Step 1: prepare your tools for CentOS first

We need developer tools before compiling, here is the commands


sudo yum groupinstall "Development Tools"

After installed, Get required libraries becuase Nginx wont work with these:

sudo yum install pcre-devel zlib-devel openssl-devel

Step 2: get your source

Connect your Server via SSH, then switch to the tmp directory:

cd /tmp


Now we need Nginx and ngx_cache_purge code:


Dont forget to change and check your latest version from : Nginx Version Download

And for ngx_cache_purge module:


Download latest version: ngx_cache_purge module

Step 3: Elate and Buildup

Extract tarred, gzipped archives files before we need to compile.

sudo tar -xzf nginx-1.6.0.tar.gz
sudo tar -xzf ngx_cache_purge-2.3.tar.gz

p.s check both version before compile


cd Nginx-1.6.0

Compiling and installing repository from source involves three steps:

configure, in which the source code is prepared with the various modules and options we want to enabled; make, in which the binary is prepared; and make install, in which the software is installed to a previously configured destination.

So, first configure (we’re sticking with conventions, and adding our ngx_cache_purge module at the end. Be sure to update the path if you get a newer version):

sudo ./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/ --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-ipv6 --add-module=/tmp/ngx_cache_purge-2.3

If doesn’t work try without sudo.

After Long list of commands appeared, we have to stop nginx services before need to make install, the script will replace your existing Nginx binary.

sudo service nginx stop
sudo make && make install

After log list of command will display again check the nginx version, you will see ngx_cache_purge in the end of the list 🙂

nginx -V
  • ngx_cache_purge-2.3 etc. 

Now you can go back to the rtCamp post and continue from “Install Nginx Helper Plugin



Here is a guide for building nginx from source so that 2 modules.

  1. The first Module allows purging content from the proxy cache on Ubuntu
  2. The second module allows limiting the max concurrent requests to apache on Ubuntu.

Make sure using Ubunutu 10.04.0 or higher and Apache HTTP Server Version 2.0 or higher.

Step 1: prepare your tools for Ubuntu

  • Configure and install development tools packages (Packages are not installed by default).

Here we using src folder as a temp

apt-get update
apt-get upgrade
apt-get install nginx build-essential libpcre3-dev libssl-dev zlib1g-dev
cd /usr/src/


tar -xvf nginx-*.tar.gz
rm nginx-*.tar.gz
mv nginx-* nginx-core

Make sure check your version before install from Nginx site.


  • Get ngx_cache_purge module – adds ability to purge content from proxy cache
tar -xvf ngx_cache_purge-*.tar.gz
rm ngx_cache_purge-*.tar.gz
mv ngx_cache_purge-* ngx_cache_purge

Check the version please.


  • Download nginx-ey-balancer module – adds a request queue that allows limiting of concurrent requests to apache on Ubuntu
wget -O nginx-ey-balancer.tar.gz
tar -xvf nginx-ey-balancer.tar.gz
rm nginx-ey-balancer.tar.gz
mv ry-nginx-ey-balancer-* nginx-ey-balancer


  • Patch and configure nginx
cd nginx-core
patch -p0 < ../nginx-ey-balancer/patches/nginx-0.8.32.patch
./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log --pid-path=/var/run/
--lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug
--with-http_stub_status_module --with-http_flv_module --with-http_ssl_module
--with-http_dav_module --with-http_gzip_static_module --with-ipv6
--add-module=/usr/src/ngx_cache_purge --add-module=/usr/src/nginx-ey-balancer


  • Stop and Build nginx
/etc/init.d/nginx stop
make && make install


  • Configure and compile nginx
vim /etc/nginx/nginx.conf
# main nginx config
user www-data;
worker_processes 4; #based on number of CPU cores
error_log /var/log/nginx/error.log;
pid /var/run/;
events {
worker_connections  1024;
http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
client_body_temp_path /var/lib/nginx/body 1 2;
gzip_buffers 32 8k;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_types text/css text/xml application/x-javascript
application/atom+xml text/plain application/rtf;
include /etc/nginx/sites-enabled/*;


  • Configure nginx server replacing with your public IP addresses please:
vim /etc/nginx/sites-enabled/default
# server setup
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=staticfilecache:180m
proxy_read_timeout 120;
proxy_send_timeout 120;
proxy_cache_key "$scheme://$host$request_uri";
upstream apache {
server weight=1 fail_timeout=120s;
max_connections 3;
server {
#Listen to your public IP
log_format custom '$host $uri $remote_addr [$time_local] '
'$status $bytes_sent [$request]';
access_log /var/log/nginx/access.log custom;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
proxy_cache_valid 200 20m;
location / {
# If logged in, don't cache.
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_cache staticfilecache;
proxy_pass http://apache;
proxy_cache_valid 200 20m;
location ~ /purge(/.*) {
proxy_cache_purge staticfilecache "$scheme://$host$1";
location ~* wp-.*.php|wp-admin {
proxy_pass http://apache;
location ~* .(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx|ico)$ {
proxy_cache staticfilecache;
proxy_pass http://apache;
proxy_cache_valid 200 120m;
expires 15552000;
location ~* /[^/]+/(feed|.xml)/? {
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_cache staticfilecache;
proxy_pass http://apache;
proxy_cache_valid 200 45m;
expires 3600;
location = /50x.html {
root /var/www/nginx-default;
# No access to .htaccess files.
location ~ /.ht {
deny all;


  • Configure apache to listen on port 8080, also if you have expires rules set up in apache you will need to disable them for the proxy cache to work.
vim /etc/apache2/ports.conf
vim /etc/apache2/sites-enabled/000-default


  • Restart both nginx and apache
/etc/init.d/nginx restart
/etc/init.d/apache2 restart


  • Test, test and test


  1. Go and check your site,  your site will be displayed and saved in the proxy cache
  2. Clear the proxy cache after visiting your page by going to
  3. Always non-cached page receive from apachee if you logged in to WordPress.


  • We installed nginx via apt-get to take full advantage of pre-configured directories and other scripts, etc.  Maybe you want to prevent apt-get from upgrading nginx in the future.
vim /etc/apt/preferences.d/nginx
Package: nginx
Pin: version 0.8.52*
Pin-Priority: 1000

All done!! now you are up and running with Nginx with ngx_cache_purge modeul.

Option 2:

We also can use easyengine which will install nginx (with ngx_cache_purge ) module, php5-fpm mysql and postfix on one command.

Alternatively you can try ppa:rtcamp/nginx which will install nginx with ngx_cache_purge module

sudo add-apt-repository -y ppa:rtcamp/nginx 
sudo apt-get update
sudo apt-get install nginx-custom



References: John Levandowski and Ubuntu