Note: This article was originally published in 2013. Some steps, commands, or software versions may have changed. Check the current NginX documentation for the latest information.

In this step-by-step guide, you’ll learn purge http and https caches in nginx with nginx helper. Nginx is a high-performance HTTP server and reverse proxy, known for its stability, rich feature set, and low resource consumption.

Prerequisites

Before you begin, make sure you have:

  • A Linux server with Nginx installed
  • Root or sudo access to the server
  • Basic understanding of web server configuration

How to: Purge Http and Https caches in (http://nginx.org/ “Nginx”) with NginX Helper

If I had a wish list, it would be remote purge and supporting custom purging expressions/keys. Regardless, If you edit the file nginx-helper/purger.php and modify some lines you can send your purge request to both your http and https server. In my case my caching key uses the scheme as the referenced elements might be using https if you visit the site using https. Here is the function that send the purge request that you can modify as I have highlighted:  

function purgeUrl( $url, $feed = true ) { $this->log( ”- Purging URL | ” . $url ); $parse = parse_url( $url ); $_url_purge_base = ‘http://’ . $parse . ‘/purge’ . $parse; $_url_purge = $_url_purge_base; if ( isset( $parse ) && $parse != ” ) { $_url_purge .= ’?’ . $parse; } $this->_do_remote_get( $_url_purge ); $_url_purges_base = ‘https://’ . $parse . ‘/purge’ . $parse; $_url_purges = $_url_purges_base; if ( isset( $parse ) && $parse != ” ) { $_url_purges .= ’?’ . $parse; } $this->_do_remote_get( $_url_purges ); if ( $feed ) { $feed_url = rtrim( $_url_purge_base, ’/’ ) . ‘/feed/’; $this->_do_remote_get( $feed_url ); $this->_do_remote_get( $feed_url . ‘atom/’ ); $this->_do_remote_get( $feed_url . ‘rdf/’ ); $feed_urls = rtrim( $_url_purges_base, ’/’ ) . ‘/feed/’; $this->_do_remote_get( $feed_urls ); $this->_do_remote_get( $feed_urls . ‘atom/’ ); $this->_do_remote_get( $feed_urls . ‘rdf/’ ); } }

The result as expected is that both http and https caches now are purged:

2013-12-16 04:33:32 | INFO | * * * * * 2013-12-16 04:33:32 | INFO | * Blog :: Knowledge eXchange (#). 2013-12-16 04:33:32 | INFO | * Post :: How to: Purge Http and Https caches in NginX with NginX Helper (1234). 2013-12-16 04:33:32 | INFO | * Post (1234) published or edited and its status is published 2013-12-16 04:33:32 | INFO | * * * * * 2013-12-16 04:33:32 | INFO | Function purgePost BEGIN === 2013-12-16 04:33:32 | INFO | Purging homepage ‘http://kx.cloudingenium.com/’ 2013-12-16 04:33:32 | INFO | - Purging URL | http://kx.cloudingenium.com/ 2013-12-16 04:33:32 | INFO | - - http://kx.cloudingenium.com/purge/ *** PURGED *** 2013-12-16 04:33:32 | INFO | - - http://kx.cloudingenium.com/purge/ is currently not cached 2013-12-16 04:33:32 | INFO | - - http://kx.cloudingenium.com/purge/feed/ *** PURGED *** 2013-12-16 04:33:32 | INFO | - - http://kx.cloudingenium.com/purge/feed/atom/ is currently not cached 2013-12-16 04:33:32 | INFO | - - http://kx.cloudingenium.com/purge/feed/rdf/ is currently not cached 2013-12-16 04:33:33 | INFO | - - https://kx.cloudingenium.com/purge/feed/ is currently not cached 2013-12-16 04:33:34 | INFO | - - https://kx.cloudingenium.com/purge/feed/atom/ is currently not cached 2013-12-16 04:33:35 | INFO | - - https://kx.cloudingenium.com/purge/feed/rdf/ is currently not cached

 

(http://img.zemanta.com/zemified_h.png?x-id=2a62a4c3-770e-4225-bb6f-fbf4bde83f62)](http://www.zemanta.com/?px “Enhanced by Zemanta”)

Summary

You’ve successfully learned purge http and https caches in nginx with nginx helper. If you run into any issues, double-check the prerequisites and ensure your NginX environment is properly configured.