Skip to content


ngx_cache_puage更新缓存404错误

在清除缓存时部分成功部分失败,有时通过浏览器可以但用程序就失败.

location ~ /purge(/.*)
{
	#设置只允许指定的IP或IP段才可以清除URL缓存。
	allow            127.0.0.1;
	allow            192.168.0.0/16;
	include manageip.conf;
	deny            all;
	proxy_cache_purge    cache_www   $host$1$is_args$args;
	error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
}    
if ( $request_method = "PURGE" ) {
	rewrite ^(.*)$ /purge$1 last;
}
模拟测试
curl -H "Host:blog.c1gstudio.com" -H "User-Agent: c1gtest"  -X PURGE http://blog.c1gstudio.com/static/image/common/qrcode.png
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>openresty</center>
</body>
</html>

去掉权限,并输出url,发现没有$1的输出
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
echo cache_bbs $host$1$is_args$args;
error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
}

curl -H “Host:blog.c1gstudio.com” -H “User-Agent: c1gtest” -X PURGE http://blog.c1gstudio.com/static/image/common/qrcode.png
cache_bbs: blog.c1gstudio.com

可能是$1被其它脚本占用,重新定义一个 $purgeurl变量
location ~ /purge(?<purgeurl>/.*)
{
	#设置只允许指定的IP或IP段才可以清除URL缓存。
	allow        127.0.0.1;
	allow        192.168.0.0/16;
	echo    cache_bbs   $host$purgeurl$is_args$args;
	error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
}    

测试可以输出
curl -H "Host:blog.c1gstudio.com" -H "User-Agent: c1gtest"  -X PURGE http://blog.c1gstudio.com/static/image/common/qrcode.png
cache_bbs: blog.c1gstudio.com/static/image/common/qrcode.png
最后修改并测试成功
location ~ /purge(?<purgeurl>/.*)
{
	#设置只允许指定的IP或IP段才可以清除URL缓存。
	allow        127.0.0.1;
	allow        192.168.0.0/16;
	include manageip.conf;
	deny            all;
	proxy_cache_purge    cache_bbs   $host$purgeurl$is_args$args;
	error_page 405 =200 /purge$purgeurl; #处理squidclient purge的时候出现的405错误
}    
if ( $request_method = "PURGE" ) {
	rewrite ^(.*)$ /purge$1 last;
}      

curl -H "Host:blog.c1gstudio.com" -H "User-Agent: c1gtest"  -X PURGE http://blog.c1gstudio.com/static/image/common/qrcode.png
<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>Key : blog.c1gstudio.com/static/image/common/qrcode.png
<br>Path: /dev/shm/nginx/proxy_cache_bbs/1/92/9a7ee4d7167bc0ead33f4ccdb4439921
</center>
<hr><center>openresty/1.19.9.1</center>
</body>
</html>

Posted in Nginx.

Tagged with .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.