Nginx系列(十四. nginx缓存)
2015-05-24 20:52
267 查看
一.代理模块缓存
1.配置文件#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区 proxy_temp_path /web/tmp/nginx; #设置Web缓存区名称为cache_one,内存缓存空间大小为100MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为1GB。 proxy_cache_path /web/cache/nginx levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=1g;
location / { #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; #对不同的HTTP状态码设置不同的缓存时间 proxy_cache_valid 200 304 1h; #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 proxy_cache_key $host$uri$is_args$args; proxy_pass http://webserver; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
反向代理的缓存功能是一个重定向,不是根据url生成缓存静态文件的功能
二.fastcgi模块缓存
1.配置文件fastcgi_temp_path /web/tmp/fastcgi; #设置fastcgi缓存路径 levels代表目录层级,1:2会生成16*256,2:2会生成256*256 keys_zone代表缓冲区名称 inactive代表过期时间 max_size代表最多用多少磁盘空间 fastcgi_cache_path /web/cache/fastcgi levels=1:2 keys_zone=cache_two:100m inactive=1d max_size=1g;
location ~ [^/]\.php(/|$) { fastcgi_cache cache_two; fastcgi_cache_valid 200 10m; fastcgi_cache_methods GET HEAD; #忽视以下头信息 fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie"; fastcgi_cache_key "$scheme$request_method$host$request_uri"; #添加状态头信息 add_header X-Cache-CFC "$upstream_cache_status - $upstream_response_time"; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; }
注意:
1.如果fastcgi响应的头信息里有Expires Cache-Control Set-Cookie的时候,fastcgi_cache是不起作用的,所以需要把fastcgi_ignore_headers这个配置项加上。
2.添加头信息X-Cache-CFC 是为了测试缓存是否起作用,$upstream_cache_status包含以下几个状态:
MISS 未命中,请求被传送到后端
HIT 缓存命中
EXPIRED 缓存已经过期请求被传送到后端
UPDATING 正在更新缓存,将使用旧的应答
STALE 后端将得到过期的应答
BYPASS 缓存被绕过了
三.参考
http://www.qttc.net/201307355.htmlhttps://serversforhackers.com/nginx-caching/
http://www.ha97.com/5194.html
http://www.cnxct.com/several-reminder-in-nginx-fastcgi_cache-and-php-session_cache_limiter/
相关文章推荐
- nginx系列:nginx反向缓存代理详解 推荐
- Nginx系列(十五. nginx清除缓存)
- 服务器数据库系列 - Nginx 静态文件缓存时间设置
- 面试宝典系列-nginx的缓冲和缓存
- memcache系列--处理缓存的三种方案(三)
- Nginx系列教程之四:Nginx常用变量汇总及测试
- Nginx 模块自主开发十: 设计一个基于NGINX 的缓存服务
- 使用Nginx的proxy_cache缓存功能取代Squid
- 淘淘商城系列——商品详情页面展示添加缓存
- 缓存淘汰算法系列之1——LRU类
- 前端页面缓存系列之localStorage
- nginx 缓存简单配置
- [.NET领域驱动设计实战系列]专题八:DDD案例:网上书店分布式消息队列和分布式缓存的实现
- nginx图片缓存服务器配置实战
- 内存缓存__DiscuzX2.0缓存的用法与缓存机制解析[调用与更新缓存]系列二
- nginx缓存及错误页面配置
- 【Nginx】开启 gzip和缓存
- nginx对压缩,负载均衡,反向代理,缓存参数忧化
- FastDFS + Nginx 多服务器完整配置与反向代理缓存等
- android原理揭秘系列之VacantCell缓存