0

I've been trying to figure out why Im failing to achieve fastcgi caching for a rewrite rule applied php page.

While all other request wonderfully cache, the rewrite ^(.+)/special/?$ /inc/special.php?req=$1 last;always sent an $upstream_cache_status MISS.

I tried copying the same fastcgi_cache parameters for the */special/* page as well. But not working.

Anyone please suggest where this goes wrong.

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;      
}

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

}
TheMonkeyKing
  • 113
  • 1
  • 1
  • 5

1 Answers1

1

Your location = /inc/special.php passes requests directly to fastcgi server, therefore there is no caching.

Furthermore, you need to check the HTTP caching headers set by special.php. If the HTTP headers do not allow caching, fastcgi_cache does not cache the request.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63