Nginx purge (invalidate) cache

How to purge cache record in ngix cache via http request.
Today we install awesome nginx plugin in production https://github.com/FRiCKLE/ngx_cache_purge/
How to use it – real life example:

1. Our cache location and proxy settings:

proxy_cache_path /var/cache/nginx/proxy_cache_quick levels=1:2 keys_zone=quick_cache:300m max_size=2m inactive=7d;

upstream        mt {
    server 1.1.1.1;
    server 2.2.2.2;
}
server {

        listen   80;
        root /usr/share/nginx/www;
        index index.html index.htm;
        server_name js.inf.randomthemes.com;

location / {
        access_log  off;
        proxy_set_header Host "randomthemes.com";
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://mt/informers/;
        proxy_redirect off;
        proxy_cache_valid   2y;
        proxy_cache_valid 404      1m;
        proxy_cache quick_cache;
        proxy_ignore_headers Expires Cache-Control Set-Cookie;
   
        proxy_cache_key "$host|$request_uri";
    }

}

2. Purge cache location

server {
    listen 8079;
    location ~ /purge(/.*) {
                access_log /var/log/nginx/purge.log;
                error_log /var/log/nginx/purge-error.log;

                auth_basic "Restricted";                           #For Basic Auth
                auth_basic_user_file /etc/nginx/conf.d/.htpasswd;  #For Basic Auth

                proxy_cache_purge  quick_cache js.inf.randomthemes.com|$1;
    }
}

3. How to create password file

 # aptitude install apache2-utils
 # htpasswd -c /etc/nginx/conf.d/.htpasswd purge

4. How to use URL via curl + auth.

curl -u purge:MEGAPASSWORD http://cdn1-1.randomthemes.com:8079/purge/20115364245.js

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>