nginx清除缓存ngx_cache_purge

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两个的一致性

nginx清除缓存ngx_cache_purge_第1张图片nginx清除缓存ngx_cache_purge_第2张图片

你可能感兴趣的:(linux,nginx)