How to Add ngx_cache_purge:

I spent weeks to understand and compile Nginx with ngx_cache_purge – Full backup require before start.

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 here, fastcgi_cache with conditional purging, but its initial steps and not saying how to compiling Nginx plus ngx_cache_purge. You can install and compile Nginx and ngx_cache_purge module from the scratch.

I’m sure if you installing and compiling then you are 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

CentOS

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:

wget http://nginx.org/download/nginx-1.6.0.tar.gz

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

And for ngx_cache_purge module:

wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz

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

Now,

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/nginx.pid --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


Ubuntu:

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
        1. 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/
        • If you have Plesk 12 Installed on your VPS then Nginx and FASTCGI already been installed, Just need to enable it from Plesk panel.

Installing and checking nginx :

If you perform a clean installation of Plesk, nginx will be turned on by default. If you upgrade from earlier versions, you can add the nginx component at any time after the upgrade in Tools & Settings > Updates & Upgrades > Add Components.

Once the component is added, you should run the Reverse Proxy Server (nginx) service in Tools & Settings > Services Management.You can view the version of the installed nginx server in Tools & Settings > Server Components.

Turning off/on nginx:

To return to the configuration with a single Apache web server, stop the Reverse Proxy Server (nginx) service in Tools & Settings > Services Management.

More info regarding Plesk click here: http://download1.parallels.com/Plesk/Doc/en-US/online/plesk-administrator-guide/index.htm?fileName=70837.htm

wget http://www.nginx.org/download/nginx-0.8.52.tar.gz
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
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
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 https://github.com/ezmobius/nginx-ey-balancer/tarball/master -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/nginx.pid
--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-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/nginx.pid;
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
max_size=500m;
proxy_read_timeout 120;
proxy_send_timeout 120;
proxy_cache_key "$scheme://$host$request_uri";
upstream apache {
server 127.0.0.1:8080 weight=1 fail_timeout=120s;
max_connections 3;
}
server {
#Listen to your public IP
listen 192.168.1.98:80;
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 http://www.example.com/,  your site will be displayed and saved in the proxy cache
        1. Clear the proxy cache after visiting your page by going to http://www.example.com/purge/blog/
        1. 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 can also 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

THAT'S ALL!

References: John Levandowski and Ubuntu
Tag: