Nginx Self-Hosted CDN and ngx_cache_purge Cache Purging

nginx cache purgeself-hosted cdnngx_cache_purge modulewordpress cache refreshnginx proxy cache
Published·Modified·

Nginx self-hosted CDN was introduced in Smart Resolution + Nginx Reverse Proxy, Building CDN Acceleration Nodes. Interested readers can refer to that article, but you can further improve it on this basis, such as adding the ngx_cache_purge module to clear cache.

Cache Diagram

Compile the ngx_cache_purge Module

Clearing Nginx cache requires the ngx_cache_purge module. You can run the command nginx -V to check the compiled modules. If ngx_cache_purge is not listed, the module is not installed, and you need to recompile Nginx.

Nginx Version Check

The module download link is ngx_cache_purge-2.3.tar.gz. For instructions on how to compile Nginx, refer to: Nginx Compilation Installation of Fancy Index Module to Achieve Beautiful Index Directories.

Configure ngx_cache_purge

Add the following configuration within the server block and reload Nginx. Note that the cache_one value here must match the keys_zone definition value, otherwise Nginx will fail to start.

location ~ /purge(/.*) {
    allow all;
    proxy_cache_purge cache_one $proxy_host$1$is_args$args;
    error_page 405 =200 /purge$1;
}

To clear the cache, add the purge parameter, such as https://blog.xiaoz.org/purge/xxx.png. If the file has a cache, the following screenshot will be displayed. If no cache exists, it returns 404. If it returns 404 in all cases, the configuration may have failed.

Purge Result

WordPress Automatic Cache Refresh

My blog uses the WordPress program. If the page is cached after enabling CDN, user comments cannot be displayed immediately after submission. You can use an Ajax asynchronous request to the ngx_cache_purge interface to clear the page cache when a user submits a comment. Simply add the following JavaScript code to footer.php:

<script>
    $(document).ready(function(){
        $("#submit").click(function(){
            var uri = "https://blog.xiaoz.org/purge" + window.location.pathname;
            $.get(uri,function(data,status){
                return true;
            });
        });
    });
</script>

Replace https://blog.xiaoz.org in the code with your own domain name. Below is the complete CDN configuration for my blog, for reference only:

proxy_cache_path /data/caches levels=1:2 keys_zone=xiaozcdn:100m inactive=30m max_size=100m;
server
    {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl on;
    ssl_certificate /xxx/www_xiaoz_me.crt;
    ssl_certificate_key /xxx/www_xiaoz_me.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
    ssl_prefer_server_ciphers on;
    ssl_stapling on;
    ssl_stapling_verify on;

    server_name     www.xiaoz.me;
    charset utf-8,gbk;

    # Delete cache
    location ~ /dcache(/.*) {
        allow all;
        proxy_cache_purge xiaozcdn $proxy_host$1$is_args$args;
        error_page 405 =200 /purge$1;
    }

    location / {
        #proxy_set_header Accept-Encoding "";
        proxy_pass https://blog.xiaoz.org;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache xiaozcdn;
        proxy_cache_valid  200 304  30m;
        proxy_cache_valid  301 24h;
        proxy_cache_valid  500 502 503 504 0s;
        proxy_cache_valid any 1s;
        # How many times has it been cached?
        proxy_cache_min_uses 1;
        expires 12h;
        proxy_cache_key    $uri$is_args$args;
    }
}
server
{
    listen 80;
    server_name www.xiaoz.me;
    rewrite ^(.*) https://blog.xiaoz.org$1 permanent;
}

Summary

Nginx is widely used and very powerful, but it has many parameters. What I know is just a drop in the bucket. The above solution has been applied to my blog, and the specific effects remain to be observed and improved. Please provide feedback if there are any issues.