If you are running WordPress on a VPS or dedicated server, you may want to install the nginx HTTP server to act as a proxy cache for WordPress. This is a guide for building nginx from source so that 2 modules can be added.  The first module allows purging content from the proxy cache and the second module allows limiting the max concurrent requests to apache. This guide assumes you are running:

  • Ubuntu Server 10.04
  • Apache HTTP Server Version 2.0

1. Configure ubuntu with needed development packages not installed by default and update/upgrade ubuntu [sourcecode]apt-get update apt-get upgrade apt-get install nginx build-essential libpcre3-dev libssl-dev zlib1g-dev cd /usr/src/[/sourcecode] 2. Get latest nginx [sourcecode]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[/sourcecode] 3. Get ngx_cache_purge module - adds ability to purge content from proxy cache [sourcecode]wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz tar -xvf ngx_cache_purge-*.tar.gz rm ngx_cache_purge-*.tar.gz mv ngx_cache_purge-* ngx_cache_purge[/sourcecode] 4. Get nginx-ey-balancer module - adds a request queue that allows limiting of concurrent requests to apache [sourcecode]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[/sourcecode] 5. Patch and configure nginx [sourcecode]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[/sourcecode] 6. Build nginx [sourcecode]/etc/init.d/nginx stop make && make install[/sourcecode] 7. Configure nginx [sourcecode]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/*; }[/sourcecode] 8. Configure nginx server replacing listen with your public ip [sourcecode]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; } }[/sourcecode] 9. 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 [sourcecode]vim /etc/apache2/ports.conf vim /etc/apache2/sites-enabled/000-default[/sourcecode] 10. Restart nginx and apache [sourcecode]/etc/init.d/nginx restart /etc/init.d/apache2 restart[/sourcecode] 11. Test

  1. You should now be able to go to http://www.example.com/ and your page will be served and saved in the proxy cache
  2. After visiting a page at http://www.example.com/blog/ you can clear the proxy cache by going to http://www.example.com/purge/blog/
  3. If you are logged in to WordPress or have left a comment you will always receive a non-cached page from apache

12. You installed nginx via apt-get to take advantage of preconfigured directories, init scripts, etc.  Because of this you may want to prevent apt-get from upgrading nginx in the future. [sourcecode]vim /etc/apt/preferences.d/nginx Package: nginx Pin: version 0.8.52* Pin-Priority: 1000[/sourcecode] I hope this guide has been useful, if you have any questions or corrections please leave a comment. The following were most valuable in providing the basis for the above guide.