【摘自张宴的"实战:Nginx"】使用nginx的fastcgi_cache缓存php输出的内容

亲自测试发现,fastcgi_cache虽然可以缓存生成的php输出的文件,但是有个弊端,在缓存的失效时间之内,你继续访问这个地址,输出的内容没有发生变化,即使数据库新增了数据或者删除了数据,所以不适合来做即时的数据查询;


#user nobody;
worker_processes 1;

error_log logs/static_source.error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/static_source.access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;


#*************************************************************************
#注 fastcgi_temp_path, fastcgi_cache_path指定的路径必须在一个分区
fastcgi_temp_path /data/fastcgi_temp_path;

#设置web缓存区的名字为cache_one ,内存缓存空间大小为200M, 自动清除超过1天美意被访问的缓存数据,磁盘空间大小为30GB
fastcgi_cache_path /data/fastcgi_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

root /data/www/static;

server {
listen 80;
server_name www.test.com;

charset utf-8;

access_log logs/test.access.log main;

#location /
#{
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://my_server_pool;
#
#}

location ~ .*.(php)?$
{
fastcgi_cache cache_one;

#对不同状态码缓存设置不同的缓存时间
fastcgi_cache_valid 200 10m
fastcgi_cache_valid 301 302 1h;
fastcgi_cache_valid any 1m;

fastcgi_cache_key 127.0.0.1:9000$request_uri;

#fastcgi服务器
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

}

}
}

访问http://192.168.66.138/a.php会在目录/data/fastcgi_cache_path生成a.php输出的缓存的内容,php输出的内容为
testhelojack (10) tome (88)

生成的缓存的文件b7629eb874ec26fe28dd33e6183f9b5c(nginx根据配置的Key,md5生成的文件名)内容为:

1 ^C^@^@^@D^LHWÿÿÿÿ4þGWÿkó3^@^@<9b>^@Ø^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ ^@^@^@^@^@
2 KEY: 127.0.0.1:9000/a.php
3 ^A^F^@^A^@Q^G^@X-Powered-By: PHP/5.5.30^M
4 Content-type: text/html^M
5 ^M
6 testhelojack (10)
7 tome (88)

原文地址:https://www.cnblogs.com/maxomnis/p/5534968.html