废话不多说了, 直接上配置, 其实 fastcgi_cache 和 proxy_cache 的配置基本一样:

# !缓存文件存放目录
# 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 />';

最新文章

  1. C++ 中静态成员函数访问非静态成员变量的方法
  2. flume log4j配置
  3. Android -- ProgressBar(进度条的使用)
  4. 阿里云centos增加swap(虚拟内存)
  5. 自己动手写CPU之第七阶段(7)——乘累加指令的实现
  6. WustOJ 1575 Gingers and Mints(快速幂 + dfs )
  7. C/C++ 指针的非空判断
  8. Tomcat剖析(三):连接器(2)
  9. Ubuntu安装飞鸽传输
  10. iOS 之 Property List
  11. JavaEE开发之Spring中Bean的作用域、Init和Destroy方法以及Spring-EL表达式
  12. Java中单例模式的几种实现
  13. xc笔记
  14. Spring.Net 简单实例-02(属性注入)
  15. ROWNUM = 1 to replace count(*)
  16. nghttp2 和nginx的实践
  17. 图解Docker容器和镜像
  18. 使用Ajax的Time实现倒计时功能
  19. 一个完整的SAP RFC调用接口封装
  20. 大型运输行业实战_day02_1_数据库设计与powerDesigner使用

热门文章

  1. linux命令学习 查找文件和文件夹
  2. 使用tftp给ARM下载程序
  3. CentOS7 搭建LNMP
  4. 4、linux-grep awk sed and cuf sort uniq join
  5. 使用Cors后台设置WebAPI接口跨域访问
  6. 一个使用MVC3+NHibernate “增删改查” 的项目(修正版)
  7. jQuery插件AjaxFileUpload可以实现ajax文件上传
  8. .net动态代理-EMIT,AOP实现
  9. EIP权限工作流平台总结-2前端框架
  10. cinder服务状态up/down的源码梳理