nginx fastcgi_cache

本文介绍 Nginx 中 FastCGI 缓存的配置方法,包括缓存路径设置、缓存键定义、缓存绕过条件及缓存有效期配置等内容。此外还提供了清除缓存的方法。

参考 

http://www.cnxct.com/several-reminder-in-nginx-fastcgi_cache-and-php-session_cache_limiter

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache


fastcgi_cache_path /data/files/game/nginxCache/content levels=1:2 keys_zone=Hpp:250m inactive=1d max_size=1G;
fastcgi_temp_path /data/files/game/nginxCache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri$is_args$args";
fastcgi_cache_use_stale error timeout invalid_header http_500;
#nocache
fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie";

server{
		set $skip_cache 0;
                #post访问不缓存
                if ($request_method = POST) {
                                set $skip_cache 1;
                }
                #动态查询不缓存
                if ($query_string ~* "nonocache") {
                        set $skip_cache 1;
                }
                #后台等特定页面不缓存(其他需求请自行添加即可)
                if ($request_uri ~* "users") {
                    set $skip_cache 1;
                }
                if ($request_uri ~* "reply.php") {
                    set $skip_cache 1;
                }
                #对登录后的结果缓存(hehe就是一个判断登录与否的Cookei名称)
                if ($http_cookie ~* "ECM_ID") {
                                set $skip_cache 1;
                }

		fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                add_header KCache "$scheme$request_method$host$request_uri$is_args$args";
                fastcgi_cache Hpp;
                fastcgi_cache_valid 200 301 302 1d;
}

fastcgi_cache的清除需要使用ngx_cache_purge这个模块.

ngx_cache_purge 是 nginx 的第三方那个模块,用于清除 FastCGI, proxy, SCGI and uWSGI 缓存

参考:

点击打开链接https://www.cnhzz.com/nginx_cache_purge/

点击打开链接http://54im.com/nginx/ngx_cache_purge.html

或者

自己写方法删除缓存目录下的文件

举例:

fastcgi_cache_path /www/php_cache  levels=1:2  keys_zone=cache_php:30m inactive=1d max_size=10g;

1:2会生成16*256个字目录

root@vbstevena nginxCache # find . -type f 
./content/8/2d/4573270729c3db4e26e68198e5a1c2d8
./content/4/81/75a8c077cb22d087cf61c233a2184814
./content/1/6e/a4a589e68d5352e716f4033d01c026e1

<?php
function purgeCache()
{
    $url = $this->post('url');
 
    if (empty($url) || !Cola_Com_Validate::url($url)) {
        exit('请输入正确的URL。');
    }
 
    $md5 = md5($url);
    $cacheFile = $this->_cacheRoot . '/' . substr($md5, -1) . '/' . substr($md5, -3, 2) . '/' . $md5;
 
    if (!file_exists($cacheFile)) {
        exit('缓存不存在。');
    }
 
    if (@unlink($cacheFile)) {
        echo '清除缓存成功。';
    } else {
        echo '清除缓存失败。';
    }
}

参考: 点击打开链接http://www.lvtao.net/server/224.html#comment-1904







# 全局配置 worker_processes auto; # 自动检测CPU核心数 error_log D:/nginx/nginx-1.26.2/logs/error.log; # 错误日志路径 pid D:/nginx/nginx-1.26.2/logs/nginx.pid; # PID文件路径 # 事件模块 events { worker_connections 12000; # 每个工作进程的最大并发连接数 } # HTTP模块 http { # MIME类型映射 include mime.types; # 引入MIME类型配置文件 default_type application/octet-stream; # 默认的MIME类型 # 文件传输优化 sendfile on; # 启用sendfile系统调用 tcp_nopush on; # 减少数据包碎片 tcp_nodelay on; # 加快小数据包传输 # 长连接设置 keepalive_timeout 65; # 长连接超时时间 # 日志格式 log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'request_time=$request_time'; # 定义日志格式 # 默认访问日志 access_log D:/nginx/nginx-1.26.2/logs/access.log main; # 访问日志路径和格式 # FastCGI缓存配置 fastcgi_cache_path D:/nginx/nginx-1.26.2/fastcgi_cache levels=1:2 keys_zone=licache:10m inactive=5m; # 缓存路径及参数 fastcgi_cache_key "$request_method://$host$request_uri"; # 缓存键值生成规则 fastcgi_cache_valid 200 302 1h; # 缓存状态码为200和302的响应,有效期1小时 fastcgi_cache_valid 301 1d; # 缓存状态码为301的响应,有效期1天 fastcgi_cache_valid any 1m; # 缓存其他状态码的响应,有效期1分钟 fastcgi_cache_min_uses 1; # 至少需要被请求一次才会缓存 fastcgi_cache_use_stale error timeout invalid_header http_500; # 当后端出现错误时,允许使用旧缓存 # 客户端超时设置 client_header_timeout 60s; # 客户端请求头读取超时时间 client_body_timeout 60s; # 客户端请求体读取超时时间 send_timeout 60s; # 从后端接收响应的超时时间 # 客户端请求体大小 client_max_body_size 20m; # 允许的最大请求体大小 # 客户端头缓冲区 client_header_buffer_size 16k; # 请求头缓冲区大小 large_client_header_buffers 4 32k; # 大型请求头缓冲区大小 # 服务器名称哈希表 server_names_hash_max_size 512; # 最大服务器名哈希表大小 server_names_hash_bucket_size 64; # 哈希桶大小 # FastCGI缓冲区 fastcgi_buffer_size 128k; # FastCGI缓冲区大小 fastcgi_buffers 8 128k; # FastCGI缓冲区数量和大小 fastcgi_busy_buffers_size 128k; # 忙时使用的缓冲区大小 fastcgi_temp_file_write_size 128k; # 缓冲区写入临时文件的大小 # Upstream模块 upstream backend_servers { # 后端服务器组 least_conn; # 使用最少连接算法 server 10.169.97.8:80; # 后端服务器地址 } upstream backend { # 后端服务器组 least_conn; # 使用最少连接算法 server 10.169.97.8:1210; # 后端服务器地址 } upstream backe { # 后端服务器组 least_conn; # 使用最少连接算法 server 10.169.97.8:10885; # 后端服务器地址 } # 主机配置 server { listen 80; # 监听80端口 server_name 10.169.96.79; # 服务器域名 location / { proxy_pass http://backend_servers/wilink/aggregate; # 转发请求到后端服务器 proxy_set_header Host $host; # 传递原始Host头 proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 添加客户端IP到X-Forwarded-For头 proxy_set_header X-Forwarded-Proto $scheme; # 传递协议类型 fastcgi_cache licache; # 启用FastCGI缓存 fastcgi_cache_valid 200 302 1h; # 缓存状态码为200和302的响应,有效期1小时 fastcgi_cache_valid 301 1d; # 缓存状态码为301的响应,有效期1天 fastcgi_cache_valid any 1m; # 缓存其他状态码的响应,有效期1分钟 } } # 测试环境配置 server { listen 5180; # 监听5180端口 server_name 172.24.10.114; # 服务器域名 location / { return 404 "Not Found"; # 返回404页面 } # 114api location /api/ { proxy_pass http://backend_servers/wilink/aggregate/api/; } } server { listen 80; # 监听80端口 server_name 10.169.96.79; # 服务器域名 location / { proxy_pass http://backend/wilink/aggregate/api/info/development; # 转发请求到后端服务器 proxy_set_header Host $host; # 传递原始Host头 proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 添加客户端IP到X-Forwarded-For头 proxy_set_header X-Forwarded-Proto $scheme; # 传递协议类型 fastcgi_cache licache; # 启用FastCGI缓存 fastcgi_cache_valid 200 302 1h; # 缓存状态码为200和302的响应,有效期1小时 fastcgi_cache_valid 301 1d; # 缓存状态码为301的响应,有效期1天 fastcgi_cache_valid any 1m; # 缓存其他状态码的响应,有效期1分钟 } } server { listen 1210; # 监听1210端口 server_name 172.24.10.114; # 服务器域名 location / { return 404 "Not Found"; # 返回404页面 } location /api/info/department { # 特定路径匹配 proxy_pass http://backend/wilink/aggregate/api/info/department; # 转发请求到后端服务器 } } # Upstream模块 upstream back_servers { # 后端服务器组 least_conn; # 使用最少连接算法 server 10.169.96.250:80; # 后端服务器地址 } # 主机配置 server { listen 80; # 监听80端口 server_name 10.169.96.79; # 服务器域名 location /api/ { proxy_pass http://back_servers/wilink/aggregate; # 转发请求到后端服务器 proxy_set_header Host $host; # 传递原始Host头 proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 添加客户端IP到X-Forwarded-For头 proxy_set_header X-Forwarded-Proto $scheme; # 传递协议类型 fastcgi_cache licache; # 启用FastCGI缓存 fastcgi_cache_valid 200 302 1h; # 缓存状态码为200和302的响应,有效期1小时 fastcgi_cache_valid 301 1d; # 缓存状态码为301的响应,有效期1天 fastcgi_cache_valid any 1m; # 缓存其他状态码的响应,有效期1分钟 } } # 正式环境配置 server { listen 5181; # 监听5181端口 server_name 172.24.10.114; # 服务器域名 location /api/ { return 404 "Not Found"; # 返回404页面 } # 114api location / { proxy_pass http://back_servers/wilink/aggregate/api; proxy_set_header Host $host; # 传递原始Host头 proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 添加客户端IP到X-Forwarded-For头 proxy_set_header X-Forwarded-Proto $scheme; # 传递协议类型 fastcgi_cache licache; # 启用FastCGI缓存 fastcgi_cache_valid 200 302 1h; # 缓存状态码为200和302的响应,有效期1小时 fastcgi_cache_valid 301 1d; # 缓存状态码为301的响应,有效期1天 fastcgi_cache_valid any 1m; # 缓存其他状态码的响应,有效期1分钟 } } }
最新发布
06-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值