Nginx配置文件缓存需要用到ngx_cache_purge模块

ngx_cache_purge模块安装

#wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

#tar zxvf ngx_cache_purge-2.3.tar.gz

#./configure --prefix=/opt/nginx \
                      --user=www \
                      --group=www \
                     --with-http_stub_status_module \
                     --with-http_gzip_static_module \
                     --with-http_realip_module \
                     --with-http_ssl_module \
                     --add-module=/opt/soft/ngx_cache_purge-2.3   #添加ngx_cache_purge模块
#make
#make install

ngx_cache_purge 模块配置

#mkdir /opt/nginx/proxy_cache_dir
#mkdir /opt/nginx/cache_tmp

#nginx.conf 配置

proxy_temp_path /opt/nginx/cache_tmp/nginx_cache;      #临时目录

proxy_cache_path /opt/nginx/proxy_cache_dir/ levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=2g;  #缓存目录,缓存级数,共享内存大小,非活动时间,最大容量


#后端服务器

upstream dl {

  server 192.168.1.100;

}


#vhost配置

location ~ /purge(/.*) {
    allow 127.0.0.1;         #只允许本机清除缓存
    deny all;
    allow all;
    proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(gif|jpg|jpeg|png|js|css|html)$ {          #设置要缓存的文件
    proxy_pass http://dl;
    proxy_http_version 1.1;
    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_cache cache_one;
    proxy_cache_valid 200 304 1d;
    proxy_cache_valid 301 302 1m;
    proxy_cache_valid any 1m;
    proxy_cache_key $host$uri$is_args$args;
    add_header pub "$upstream_cache_status from $server_name";
    proxy_ignore_headers "Cache-Control" "Expires";
}


清除缓存

http://www.test.com/purge/uri