基于nginx的FastCGI的缓存配置

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# !缓存文件存放目录
# 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测试脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 />';