nginx缓存 ngx_cache_purge清除模块 清除失败404异常找不到文件

nginx缓存 ngx_cache_purge清除模块 清除失败404错误找不到文件

一. 在测试nginx缓存,清除缓存文件时出现“404  NOT FOUND” 

二.我当时的文件配置(nginx.conf)如下:

proxy_cache_path /cache/proxycache levels=1:2 keys_zone=web_cache:100m inactive=1d max_size=30g;
proxy_cache_path /cache/proxycache_image levels=1:2 keys_zone=web_cache_image:100m inactive=1d max_size=30g;
upstream realserver {
server 192.168.2.166:80;
}

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}

 

#图片缓存区域

location ~* ^.*\.(js|ico|gif|jpg|jpeg|png)$ {
log_not_found off;
error_log /var/log/image_error.log debug;

expires 7d;
proxy_pass http://realserver;
proxy_cache web_cache_image;
proxy_cache_valid 404 1h;
proxy_cache_valid any 10m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
# proxy_cache_key $host$uri$is_args$args;
proxy_cache_key $host$uri$is_args$args;

add_header Nginx-Cache “$upstream_cache_status”;
}

#伪静态html(aspx伪静态)缓存区域
location ~ .*\.(html|htm)$ {
proxy_cache web_cache;
proxy_ignore_headers cache-control;
proxy_cache_valid 200 304 1d;
proxy_pass http://realserver;
proxy_cache_key $uri$is_args$args;
expires 1d;
add_header Nginx-Cache “$upstream_cache_status”;
}

#清除缓存区域
location ~ /purge(/.*) {
allow all;
allow 127.0.0.1;
# deny all;
proxy_cache_purge web_cache_image $host$1$is_args$args;

}

三.问题排查:

1.当时第一次认为是版本的问题,于是将ngx_cache_purge插件更新的最新版本(wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz),更新网插件之后,问题依然没有解决;

2.配置文件的问题,通过一下午的测试发现了问题所在

四. 解决问题及分析;

1.正确的配置如下;

proxy_cache_path /cache/proxycache levels=1:2 keys_zone=web_cache:100m inactive=1d max_size=30g;
proxy_cache_path /cache/proxycache_image levels=1:2 keys_zone=web_cache_image:100m inactive=1d max_size=30g;
upstream realserver {
server 192.168.2.166:80;
}

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#清除缓存区域(purge区域一定要在静态缓存区域的上面,否则会报错404错误)
location ~ /purge(/.*) {
allow all;
allow 127.0.0.1;
# deny all;
proxy_cache_purge     web_cache_image    $host$1$is_args$args;      #$host$1$is_args$args 这个参数不要去更改他,否则会报错,相对应的在 静态缓存区域必须有对应的可以参数 proxy_cache_key    $host$uri$is_args$args;

}

#图片缓存区域
location ~* ^.*\.(js|ico|gif|jpg|jpeg|png)$ {
log_not_found off;
error_log /var/log/image_error.log debug;

expires 7d;
proxy_pass http://realserver;
proxy_cache web_cache_image;
proxy_cache_valid 404 1h;
proxy_cache_valid any 10m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache_key    $host$uri$is_args$args; #这个参数必须有,否则在清除缓存的时候会报错

add_header Nginx-Cache “$upstream_cache_status”;
}

#伪静态缓存区域
location ~ .*\.(html|htm)$ {
proxy_cache web_cache;
proxy_ignore_headers cache-control;
proxy_cache_valid 200 304 1d;
proxy_pass http://realserver;
proxy_cache_key    $host$uri$is_args$args; 
expires 1d;
add_header Nginx-Cache “$upstream_cache_status”;       #添加响应头“$upstream_cache_status命中缓存信息, nginx-cache(可以缓存其他名字)
}

五.继续测试,在重启nginx服务之后,就可以清除缓存了,如图;

nginx缓存 ngx_cache_purge清除模块 清除失败404异常找不到文件

 

注意:

       upstream指令用于设置一组可以在proxy_pass和fastcgi_pass指令中使用的代理服务器,默认的负载均衡方式为轮询.upstream模块中的server指令用于指定后端服务器的名称和参数,服务器的名称可以是一个域名,一个ip地址,端口号或者UNIX Socket.
       而在server{..}虚拟主机内,可以通过proxy_pass和fastcgi_pass指令设置进行反向代理的upstream服务器集群
       proxy_set_header指令用于在向反向代理的后端WEB服务器发起请求时添加指定的header头信息
       当后端WEB服务器上有多个基于域名的虚拟主机时,要通过添加header头信息Host,用于指定请求的域名,这样后端服务器才能识别该反向代理访问请求是由那一个虚拟主机来处理  
       使用反向代理之后,后端web服务器就不能直接$_SERVER['REMOTE_ADDR']变量来获取用户的真实ip了,通过$_SERVER['REMOTE_ADDR']获得的将是负载均衡器的ip.这时,就要通过Nginx反向代理时添加Header头信息X-Forwarded-For,让后端web服务器能够通过$_SERVER['HTTP_X_FORWARDED_FOR']获取到用户的真实ip