基于nginx的FastCGI的缓存配置
2013-06-09 15:03
495 查看
废话不多说了, 直接上配置, 其实 fastcgi_cache 和 proxy_cache 的配置基本一样:
几个问题:
1. 为什么使用 set 变量控制 $xwsoul_nocache 来控制 fastcgi_cache_bypass(0.8x之后官方推荐的方法) 和 fastcgi_no_cache 参数确定是否生成缓存?
很简单, 因为不能是所有的页面都需要 cache.当然有更好的实现方法请尽量给建议.
2. 如果我在 /cgi_cache/ 下的脚本不需要缓存可以实现么, 怎么办?
这个也很简单, 通过 CGI 的返回的 Head 即可控制:
和
均可实现.
3. 为什么缓存的 Cache Name 里需要 $scheme ?
因为 http/https 是不同的安全级别, 可能需要的缓存机制是不一样的!
最后附上对应访问的PHP测试脚本
# !缓存文件存放目录 # levels 缓存层次 # keys_zone 缓存空间名和共享内存大小(热点内容放在内存) # inactive 失效时间, 1d = 1天 # max_size 最大缓存空间(硬盘占用) fastcgi_cache_path /home/xwsoul/code/web/demo/cgi_cache/cache_dir/ levels=2:2 keys_zone=cgi_one:10m inactive=2h max_size=2g; server { listen 80; server_name demo.xwsoul.com; root /home/xwsoul/code/web/demo; index index.html index.php; # 清理 fastcgi_cache location ~ /pcgi(/*) { allow 127.0.0.1; allow 192.168.2.0/24; deny all; fastcgi_cache_purge cgi_one "$scheme$request_method$host$1"; } try_files $uri @missing; location @missing { rewrite ^/cgi_cache/(.*) /cgi_cache/index.php?code=$1 last; } location ~ .*\.php$ { # 默认情况下关闭cache set $xwsoul_nocache yes; # 请求的 URL 在 /cgi_cache/ 的允许缓存 if ($request_uri ~ ^/cgi_cache/) { set $xwsoul_nocache ""; } # CGI指向地址 fastcgi_pass 127.0.0.1:9000; # 调用 cache 的命名空间 fastcgi_cache cgi_one; # 缓存触发的方法 HEADER, GET, POST 中的一个或多个 fastcgi_cache_methods GET; # 生成的缓存名称的 key 的名字 # $scheme https/http # $request_method 请求方法,基于上面的设置,这里为GET # $host 主机 # $request_uri 请求地址 fastcgi_cache_key "$scheme$request_method$host$request_uri"; # 生效值: 即代理目标的状态码以及缓存时间 fastcgi_cache_valid 200 302 1h; # 确定缓存成为过期数据的情况 fastcgi_cache_use_stale error timeout invalid_header http_500; # 请求过多少次相同的URL后, 缓存将开始 fastcgi_cache_min_uses 1; # 关闭缓存 fastcgi_cache_bypass $xwsoul_nocache; fastcgi_no_cache $xwsoul_nocache; # 发送头信息到客户端 - 一般是浏览器 add_header X-Cache "$upstream_cache_status"; # fastcgi 的其他参数配置 include fastcgi.conf; } }
几个问题:
1. 为什么使用 set 变量控制 $xwsoul_nocache 来控制 fastcgi_cache_bypass(0.8x之后官方推荐的方法) 和 fastcgi_no_cache 参数确定是否生成缓存?
很简单, 因为不能是所有的页面都需要 cache.当然有更好的实现方法请尽量给建议.
2. 如果我在 /cgi_cache/ 下的脚本不需要缓存可以实现么, 怎么办?
这个也很简单, 通过 CGI 的返回的 Head 即可控制:
header('Cache-Control:no-cache, must-revalidate, max-age=0;');
和
header('Expires:0;');
均可实现.
3. 为什么缓存的 Cache Name 里需要 $scheme ?
因为 http/https 是不同的安全级别, 可能需要的缓存机制是不一样的!
最后附上对应访问的PHP测试脚本
<?php $code = isset($_GET['code'])?$_GET['code']:''; echo '<pre>'; var_dump($code); echo '</pre>'; $code = md5($code); $rand = mt_rand(1, 10); echo '<p>'; echo 'Loop <b>', $rand, '</b> :<br />'; for ($i = 0;$i < $rand;$i++) { echo $code, '<br />'; } echo '</p>'; echo date('Ymd H:i:s'), '<br />';
相关文章推荐
- 基于nginx的FastCGI的缓存配置
- 基于nginx的FastCGI的缓存配置
- Nginx中FastCGI缓存配置 详解(主要针对PHP优化)
- nginx配置基于memcached缓存web服务器
- 利用nginx proxy_cache进行网站缓存托底配置 nginx fastcgi cache nginx cache 配置 nginx file cach
- Linux笔记(68)——nginx缓存配置及其他配置
- Cacti监控服务器配置教程(基于CentOS+Nginx+MySQL+PHP环境搭建)
- nginx图片缓存服务器配置实战
- Nginx缓存区内存配置大小对性能测试的影响
- Nginx与PHP(FastCGI)的安装、配置与优化
- 基于nginx实现缓存功能
- nginx及apache不缓存配置;
- linux CentOS7 下 Nginx 1.13.7 配置expires缓存提升网站速度与负载
- Nginx与PHP(FastCGI)的安装、配置、优化
- linux后台服务器开发环境部署配置和验证(nginx+apache+php-fpm+FASTCGI(C/C++))
- Nginx配置缓存静态资源
- 高性能Web服务器Nginx的配置与部署研究(12)应用模块之Memcached做文件缓存时压缩引起的问题
- nginx 缓存路径配置优化
- Nginx配置文件详细说明,包括基本配置,反向代理配置,expire缓存过期,适合接入CDN的配置
- nginx+fastcgi单机配置