TL;DR — Quick Summary
Learn how to purge http and https caches in nginx with nginx helper. Step-by-step guide with examples.
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.
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 (https://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 ’/’ 2013-12-16 04:33:32 | INFO | - Purging URL | / 2013-12-16 04:33:32 | INFO | - - / *** PURGED *** 2013-12-16 04:33:32 | INFO | - - / is currently not cached 2013-12-16 04:33:32 | INFO | - - / *** PURGED *** 2013-12-16 04:33:32 | INFO | - - / is currently not cached 2013-12-16 04:33:32 | INFO | - - / is currently not cached 2013-12-16 04:33:33 | INFO | - - / is currently not cached 2013-12-16 04:33:34 | INFO | - - / is currently not cached 2013-12-16 04:33:35 | INFO | - - / is currently not cached