配置nginx过滤不良访问提高squid命中率
原文:http://sudone.com/nginx/nginx_strip_args.html
1、对静态内容加以问号的访问
例如http://www.sudone.com?abc,这样的请求会透过squid缓存,直达后端服务器,并且在squid中保存缓存,从而造成压力和内存浪费。
在nginx的server中加入对html文件和首页等的过滤规则以解决此问题,此规则判断首页和html、jpg、gif结尾的文件,如果结尾有?xxx,则抛出403错误,由error_page接收,并用302跳转到正确的地址。
location ~* (.html$)|(^/$)| (.jpg$)|(.gif$){
proxy_pass http://www.sudone.com;
if ($is_args)
{
return 403;
error_page 403 =200 $scheme://$host$uri;
}
}
第二种办法是用rewrite,用302跳转怎么说也多了一次请求,不是很好,而且效率也不高。
location ~* (.html$)|(^/$)| (.jpg$)|(.gif$){
proxy_pass http://www.sudone.com;
rewrite ^(.*)$ $1? break;
}
通过rewrite,将请求定向到该页本身,后面多加一个?,便可让args的内容被过滤掉。
现在squid对于同一个html文件就会有只有一个存档了,但值得注意的是,这个存档并不带有?,也就是说,通过squidclient进行purge 操作的时候,不需要加上?。例如访问http://www.sudone.com/?abc,通过rewrite后,nginx使用的是http: //www.sudone.com/访问squid,而不是http://www.sudone.com/?。因此这个办法最适合于过滤掉args。
相比于n
3、对静态内容发送POST请求
这种请求也会透过squid,但不会在squid缓存内容。
依葫芦画个瓢,以下配置可解决
location ~* (.html$)|(^/$)| (.jpg$)|(.gif$){
proxy_pass http://www.sudone.com;
if ($request_method = POST){
return 403;
error_page 403 =200 $scheme://$host$uri;
}
}
五月 3rd, 2011 at 04:51
Some New Sites I Like…
[...]below you’ll find the link to some websites that I think you should go and see http://www.panicattackstreatmentcure.net/links/ some are related to my site and others are unrelated but definitely worth visiting[...]…
五月 15th, 2011 at 02:17
Related Websites…
[...]here are some other links to sites that we find everyday so here are some popular sites we like today[...]…