Nginx-fastcgi 缓存不适用于启用重写的 php 页面

Nginx-fastcgi 缓存不适用于启用重写的 php 页面

我一直在试图弄清楚为什么我无法实现应用重写规则的 php 页面的 fastcgi 缓存。

虽然所有其他请求都完美地缓存了,但 rewrite ^(.+)/special/?$ /inc/special.php?req=$1 last;总是发送一个 $upstream_cache_status MISS。

我也尝试复制相同的fastcgi_cache参数到该页面。但是没有用。*/special/*

谁能指出问题出在哪里。

fastcgi_cache_path /etc/nginx/cache/webcache levels=1:2 keys_zone=wbcache:100m inactive=60m;

server {

      server_name example.com www.example.com;
      root /var/www/html;
      index index.php index.html;
      set $skip_cache 0;

 rewrite ^(.+)/special/?$ /inc/special.php?req=$1 last;
 
 location = /inc/special.php { 
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
       }

 location / {
        try_files $uri $uri/ /index.php?q=$uri&$args; 
        }
        
 location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
       fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
       fastcgi_hide_header "Set-Cookie";

       fastcgi_cache_key "$scheme$request_method$host$request_uri";
       fastcgi_cache_use_stale updating error timeout invalid_header http_500;
       fastcgi_cache_lock on;
       fastcgi_cache_valid any 300s;
       fastcgi_cache_bypass $skip_cache;
       fastcgi_no_cache $skip_cache;
       fastcgi_cache wbcache;
       fastcgi_cache_background_update on;
       fastcgi_param  CONTENT_TYPE $content_type;

       add_header X-Cache $upstream_cache_status;      
}

......
............

}

答案1

您的location = /inc/special.php请求直接传递到fastcgi服务器,因此没有缓存。

此外,您需要检查 设置的 HTTP 缓存标头special.php。如果 HTTP 标头不允许缓存,fastcgi_cache则不会缓存该请求。

相关内容