nginx Clear Cache Module Configuration Tutorial

Test environment:

NGINX version and compiled modules:

Clear Cache Module Version: ngx_cache_purge-2.3

1. Download unzipped ngx_cache_purge module

[root@a ~]#cd /root
[root@a ~]#wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
[root@a ~]#tar zxf 2.3.tar.gz

2. Dependencies needed to create NGINX:

[root@a ~]yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel

Compile and install NGINX:

[root@a ~]#cd nginx-1.14.2
[root@a nginx-1.14.2]# ./configure --add-module=/root/ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
[root@a nginx-1.14.2]#make
[root@a nginx-1.14.2]#make install

3. Create a directory for the cache:

[root@a nginx-1.14.2]#mkdir /usr/local/nginx/proxy_cache_path

4. Create a home page file to act as the default home page for the back-end server:

[root@a nginx]#mkdir /usr/local/nginx/htmltest
[root@a nginx]#vim /usr/local/nginx/htmltest/index.html

5. Configure nginx caching (for simplicity, I have only cache-related configurations left in the configuration file)

worker_processes  1;
events {
        worker_connections  1024;
}
http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        proxy_temp_path /usr/local/nginx/proxy_temp_path;
        proxy_cache_path /usr/local/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;

proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 32 128k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
server {
    listen       8080;
    server_name  127.0.0.1;  ##Act as a back-end server
    root /usr/local/nginx/htmltest;
    index index.html;
}

server {
listen 80;
server_name 192.168.10.10;
root html;
index index.html;
  location ~ /purge(/.*)
{
        allow            all;
        proxy_cache_purge    cache_one  $host$1$is_args$args;
               } 

 location ~ .*\.(png|jpg)$
    {
    proxy_cache cache_one;
    proxy_cache_valid 200 302 304 1h;
    proxy_cache_key $host$uri$is_args$args;
                proxy_set_header Host $host;
    proxy_set_header X-Forward-For $remote_addr;
    proxy_pass http://127.0.0.1:8080;##Transfer the request to the back-end server
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
}

Detailed parameters:

 proxy_connect_timeout 500;
#Timeout for connection to back-end server_Initiate handshake waiting for response timeout
proxy_read_timeout 600;
#After successful connection _Time to wait for backend server response_Actually queued for processing which has entered the backend
proxy_send_timeout 500;
#Backend Server Data Return Time_It is within the specified time that the back-end server must pass all the data
proxy_buffer_size 128k;
#Proxy Request Cache_This cache range holds the user's header information for Nginx's rule processing_As long as the next information can be saved  
proxy_buffers 4 128k;
#Same as above to tell Nginx how much space to use to save a few Buffer s for a single use
proxy_busy_buffers_size 256k;
#You can request a larger proxy_if your system is busyOfficial buffers recommendation*2
proxy_temp_file_write_size 128k;
#Size of proxy cache temporary file
proxy_temp_path /usr/local/nginx/temp;
#Used to specify a local directory to buffer larger proxy requests
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;
#Set the web cache name to cache_one, memory cache size of 200M, automatically clears cache data that has not been accessed for more than a day, hard disk cache size of 1g
proxy_cache_path
#Define cache storage location
levels=1:2
#Set the cache directory depth to create up to three layers.
keys_zone=cache_one:200m
#Define the cache area name and memory cache size.
max_size=1g
#Maximum disk cache space usage, delete the least used cache files when the quota is reached.
inactive=60m
#Set the cache time and delete it without being accessed for 60 minutes.
use_temp_path=off
#Do not use temp_path specifies the temporary storage path, writing the cache file directly to the specified cache file, off is recommended.
proxy_cache cache_one
#Cache area name, to be keys_zone Definition Name Consistent
proxy_cache_valid 200 302 304 60m
#Set status code to 200 302 304 expiration time to 60 minutes
proxy_cache_key $host$uri$is_args$args
#Set the key of the cache, where the key value of the web cache is composed of domain name, URI, and parameters, and hash the cache content into the secondary cache directory based on the key value
expires 3d
#Cache time 3 days
location ~ /purge(/.*) 
#Used to clear cache manually, allow indicates that only specified IP is allowed to clear URL cache
location ~ .*\.(jsp|php)?$
#Do not cache extensions ending in jsp or php

6. Practice

This is the default home page for our NGINX server:

Let's visit the specified file:

Yes, but we don't have this picture on our nginx server

This is because when we configure, we transfer the request to the back-end server and we can go to the back-end server to see if this file exists.

We found pictures on the back-end server, so now let's delete them to see if they're still accessible.

[root@a nginx]# rm -rf ./htmltest/jingyu.png 

Now let's go to the browser to access the specified file:

Didn't the picture be deleted?Why can I still access it?This is because we have configured the cache and we can go to the configured path to see if there are any files:

You can see that the cache file has been generated.If we want to clear the cache, just add purge before clearing the URI address of the cached page.
For example: 192.168.10.10/purge/jingyu.png

No files will be found if you access them again at this time

This completes clearing the cache module.

Tags: Linux Nginx OpenSSL zlib JSP

Posted on Tue, 19 May 2020 10:28:56 -0700 by mobilekid