您的位置:首页 > 运维架构 > Nginx

nginx缓存之proxy_cache遇到的坑

2014-06-06 11:24 411 查看
应用场景:font
计划配置nginx作为反向代理和负载均衡,同时利用其缓存功能,将静态页面在nginx缓存,以达到降低后端服务器连接数的目的。

关键配置点:
http{
.........
########################################################################
proxy_cache_path /usr/local/tenginx/nginx_temp/nginx_cache levels=1:2 keys_zone=cache_one:256m inactive=30m max_size=5G;
log_format access '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" $http_x_forwarded_for'
'"$upstream_cache_status"';
.........
}

server{
............
proxy_cache_key $host$uri$is_args$args;
............
}
但在我的应用场景中,nginx upstrem后端的ip不是具体服务器的地址,而是netscaler的VTP,且后端对应多台服务器,关键是为了persistence功能,配置了cookie insert,所以nginx去源站取回的所有页面,在header里面,都内插入了cookie信息。但nginx默认会不缓存带cookie的页面,所以才导致nginx缓存都是MISS。

解决办法:
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
根据具体需求将配置加入http、server、或者location段
详细见:http://nginx.org/cn/docs/http/ngx_http_proxy_module.html#proxy_hide_header
http://nginx.org/cn/docs/http/ngx_http_proxy_module.html#proxy_ignore_headers

附一:缓存状态判断:利用 "$upstream_cache_status",再日志里面输出,或者用浏览器端的工具,如:"live http headers"

附二:nginx缓存优先级(缓存问题者必看)
http://www.ttlsa.com/nginx/nginx-cache-priority/

附二:varnish缓存也会遇到相同的问题,解决如下:
## 静态资源需要去除cookie信息
if (req.request == "GET" && req.url ~ "\.(css|js|bmp|png|gif|jpg|jpeg|ico|gz
|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flvi|html|shtml)($|\?)") {
unset req.http.cookie;
return (lookup);
}
https://www.varnish-cache.org/docs/3.0/tutorial/cookies.html
http://eneplace.com/2011/01/varnish-cookies-querystrings.html

总结:缓存,cookie问题很重要

################# tengine 用非80端口的时候,ngx_http_sysguard_module 会调用80端口,不知道为什么 http://tengine.taobao.org/document_cn/http_sysguard_cn.html ###################

本文出自 “在路上” 博客,请务必保留此出处http://jacker.blog.51cto.com/1333001/1422921
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: