Code Monkey home page Code Monkey logo

nginx-cache-purge's Introduction

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

nginx-cache-purge's People

Contributors

magiclen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.