nginx清除缓存ngx_cache_purge

本文介绍如何在Nginx中安装并配置ngx_cache_purge模块来实现缓存的高效管理。从安装Nginx到配置nginx.conf文件,再到最终的测试验证,详细记录了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx ip:172.18.3.184 web服务 ip:172.18.11.247

ngx_cache_purge模块github下载地址及使用说明

https://github.com/FRiCKLE/ngx_cache_purge/


一:nginx安装

yum install -y patch unzip gcc openssl openssl-devel pcre pcre-devel
tar fxz nginx-1.7.8.tar.gz
tar fxz ngx_cache_purge-2.2.tar.gz
cd nginx-1.7.8
./configure --prefix=/usr/local/nginx --add-module=../ngx_cache_purge-2.2/ --with-http_stub_status_module --with-http_ssl_module
make && make install

二:nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    proxy_cache_path  /tmp/cache  keys_zone=tmpcache:10m;    
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

        location / {
                proxy_pass http://172.18.11.247;
                proxy_read_timeout     2000s;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_cache        tmpcache;
                proxy_cache_key    $uri$is_args$args;
        }

        location ~ /purge(/.*) {
            allow              all;
            deny               all;
            proxy_cache_purge  tmpcache $1$is_args$args;
        }
    
        location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
            proxy_set_header Host  $host;
            proxy_set_header X-Forwarded-For  $remote_addr;
            proxy_pass http://172.18.11.247;
            proxy_cache tmpcache;
            proxy_cache_key $uri$is_args$args;
            add_header  OpenCDN-Cache "$upstream_cache_status";   #测试时可以查看cache状态
            proxy_cache_valid 200 304 12h;
            expires 1d; 
         }
}

三:测试

/usr/local/nginx/sbin/nginx -s reload

[root@purge cache]# curl --head 172.18.3.184/resources/style/common/css/images/ItemsIco02.png
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Wed, 30 Nov 2016 15:16:20 GMT
Content-Type: image/png
Content-Length: 3775
Connection: keep-alive
ETag: W/"3775-1411717259000"
Last-Modified: Fri, 26 Sep 2014 07:40:59 GMT
Expires: Thu, 01 Dec 2016 15:16:20 GMT
Cache-Control: max-age=86400
OpenCDN-Cache: HIT
Accept-Ranges: bytes

[root@purge cache]# curl --head 172.18.3.184/purge/resources/style/common/css/images/ItemsIco02.png
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Wed, 30 Nov 2016 15:16:40 GMT
Content-Type: text/html
Content-Length: 292
Connection: keep-alive
四:注意   404的时候注意配置文件
proxy_cache_key
proxy_cache_purge两个的一致性


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值