大网数据广告活动
服务器运维问答

FastCGI cache "MISS" due to conflicting headers from PHP application?

· 52CE 我爱测

I've setup FastCGI cache like this: location ~ \.php(/.*)?$ { fastcgi_cache RWI; fastcgi_cache_valid 200 60m; set $nocache 0; if ($request_method = POST) { set $nocache 1; } if ($http_cookie ~ (rwi_userid*|rwi_password*)) { set $…

问题原文

原帖链接 · Joran Den Houting · CC BY-SA 4.0

I've setup FastCGI cache like this:

location ~ \.php(/.*)?$ {
    fastcgi_cache RWI;
    fastcgi_cache_valid 200 60m;

    set $nocache 0;
    if ($request_method = POST)
    {
        set $nocache 1;
    }

    if ($http_cookie ~ (rwi_userid*|rwi_password*)) {
        set $nocache 1;
    }

    if ($request_uri ~* "/(vb/admincp/)") {
        set $nocache 1;
    }

    fastcgi_no_cache $nocache;
    fastcgi_cache_bypass $nocache;

    fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_pass "unix:/var/www/vhosts/system/{domain}/php-fpm.sock";
    include /etc/nginx/fastcgi.conf;
}

And on top of the server{} block I have:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=RWI:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;

Problem is my PHP script (vBulletin) is always sending a Pragma & Cache-Control private. Is there any way to ignore those headers and force FastCGI to use cache when the above rules don't apply? I'm kind of lost in this caching possibility.

已采纳解答

原帖链接 · Xavier Lucas · CC BY-SA 3.0

Nginx interprets a bunch of headers when used as a reverse proxy to honor HTTP intermediate caches specifications. This means the following headers, if present in your app replies, will change caching behaviour as explained :

  • The “X-Accel-Expires” header field sets caching time of a response in seconds. The zero value disables caching for a response. If the value starts with the @ prefix, it sets an absolute time in seconds since Epoch, up to which the response may be cached.

  • If the header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or “Cache-Control”.

  • If the header includes the “Set-Cookie” field, such a response will not be cached.

  • If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes the “Vary” field with another value, such a response will be cached taking into account the corresponding request header fields (1.7.7).

However, nginx ships with the fastcgi_ignore_headers directive in case you want to turn this off. So what you are looking for is : fastcgi_ignore_headers Cache-Control Pragma;.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-05-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。