nginx 缓存删除


nginx 缓存删除

 

nginx除了可以手动删除外,还可以使用三方模块ngx_cache_purge删除指定的url缓存

dockerfile文件:https://github.com/procraft/nginx-purge-docker

 

 

***************************

配置文件

 

nginx.conf

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    proxy_cache_path /usr/share/nginx/cache levels=1:2 keys_zone=cache_one:50m inactive=1m max_size=500m;
    include /etc/nginx/conf.d/*.conf;
}
 

 

default.conf

server {
    listen       80;
    server_name  localhost;
 
    add_header X-via $server_addr;                    #代理服务器的ip地址
    add_header X-cache-status $upstream_cache_status; #缓存状态
 
    location ~ /purge(/.*) {
        proxy_cache_purge cache_one $host-$1-$is_args-$args;
    }                                                 #删除指定缓存,cache_one:缓存区的名称,$host-$1-$is_args-$args:缓存的key

    location / {
        proxy_cache cache_one;                        #缓存区的名称
        proxy_cache_key $host-$uri-$is_args-$args;    #缓存key值,$host:主机名,$uri:请求地址,$is_args:是否含有参数,若有则为?,没有则为空,$args:参数,没有则为空
 
        proxy_cache_valid 200 10m;                    #缓存有效期,200为10m
        proxy_cache_valid 304 1m;                     #缓存有效期,304为1m
        proxy_cache_valid 301 302 1h;                 #缓存有效期,301、302为1h
        proxy_cache_valid any 1m;                     #缓存有效期,其余为1m
 
        proxy_temp_path /usr/share/nginx/cache_temp;  #临时缓存目录
 
        proxy_pass http://192.168.57.10:8080;         #代理应用
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}

 

 

***************************

创建容器

 

docker run -it -d --net fixed3 --ip 192.168.57.10 \
-v /usr/nginx/test/hello-world.jar:/usr/local/app.jar \
--name hello-world common
 
docker run -it -d --net fixed3 --ip 192.168.57.11 \
-v /usr/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf --name nginx nginx-purge
 

 

 

***************************

测试输出

 

192.168.57.11:80/hello

      nginx 缓存删除_第1张图片

 

缓存查看

      nginx 缓存删除_第2张图片

 

 

192.168.57.11:80/purge/hello

      nginx 缓存删除_第3张图片

 

缓存查看

      nginx 缓存删除_第4张图片

缓存已经删除

 

 

你可能感兴趣的:(nginx)