#nginx #cache #clear #delete #purge

bin+lib nginx-cache-purge

An alternative way to do proxy_cache_purge or fastcgi_cache_purge for Nginx

9 releases

0.1.8 Apr 21, 2021
0.1.7 Mar 11, 2021
0.1.6 Jul 28, 2020
0.1.5 Jun 29, 2020

#945 in Command line utilities

Download history 10/week @ 2021-08-05 11/week @ 2021-08-12 2/week @ 2021-09-02 1/week @ 2021-09-16 1/week @ 2021-09-23 9/week @ 2021-09-30 18/week @ 2021-10-14 2/week @ 2021-10-21 7/week @ 2021-10-28 17/week @ 2021-11-04 28/week @ 2021-11-11 2/week @ 2021-11-18

54 downloads per month

MIT license

17KB
308 lines

Nginx Cache Purge

CI

An alternative way to do proxy_cache_purge or fastcgi_cache_purge for Nginx.

Usage

Installation / Uninstallation

From crates.io,

cargo install nginx-cache-purge

# cargo uninstall nginx-cache-purge

From GitHub (Linux x86_64),

(curl -s https://api.github.com/repos/magiclen/nginx-cache-purge/releases/latest | sed -r -n 's/.*"browser_download_url": *"(.*\/nginx-cache-purge_'$(uname -m)')".*/\1/p' | wget -i -) && sudo mv nginx-cache-purge_$(uname -m) /usr/local/bin/nginx-cache-purge && sudo chmod +x /usr/local/bin/nginx-cache-purge

# sudo rm /usr/local/bin/nginx-cache-purge

Nginx + lua-nginx-module + Nginx Cache Purge

Assume we have already put the executable file nginx-cache-purge in /usr/local/bin/.

http {
    ...

    map $request_method $is_purge {                                                             
        default   0;
        PURGE     1;
    }

    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m;
    proxy_cache_key $scheme$proxy_host$request_uri;

    server {
        ...

        location / {
            if ($is_purge) {
                set $my_cache_key $scheme$proxy_host$request_uri;

                content_by_lua_block {
                    local exitStatus = os.execute("/usr/local/bin/nginx-cache-purge /path/to/cache 1:2 "..ngx.var.my_cache_key)
                     
                    if exitStatus == 0 then
                        ngx.exit(ngx.HTTP_OK)
                    else
                        ngx.exit(ngx.HTTP_BAD_REQUEST)
                    end
                } 
            }

            proxy_pass upstream;
            include proxy_params;
        }
    }
}

Remember to add your access authentication mechanisms to prevent strangers from purging your cache.

After finishing the settings:

  • Request PURGE /path/to/abc to purge the cache from GET /path/to/abc.
  • Request PURGE /path/to/* to purge all caches from GET /path/to/**/*.

License

MIT

Dependencies

~3MB
~61K SLoC

T`