Nginx

Nginx - fastcgi 記憶體不適用於啟用重寫的 php 頁面

  • October 1, 2022

我一直試圖弄清楚為什麼我無法為應用了重寫規則的 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;      
}

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

}

location = /inc/special.php將請求直接傳遞給fastcgi伺服器,因此沒有記憶體。

此外,您需要檢查由special.php. 如果 HTTP 標頭不允許記憶體,fastcgi_cache則不記憶體請求。

引用自:https://serverfault.com/questions/1111994