0

Since enabling the fastcgi_cache on my nginx server, my php-enabled custom error page has suddenly stopped working and I'm getting the internal 404 message instead.

In nginx.conf:

fastcgi_cache_path /var/lib/nginx/fastcgicache levels=1:2 
keys_zone=MYCACHE:5m inactive=2h max_size=1g loader_files=1000
loader_threshold=2000;
map $http_cookie $no_cache { default 0; ~SESS 1; }
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-My-Cache $upstream_cache_status;

map $uri $no_cache_dirs {
   default 0;
   ~^/(?:phpMyAdmin|rather|poll|webmail|skewed|blogs|galleries|pixcache) 1;
}

the cache relevant stuff in my fastcgi.conf:

fastcgi_cache MYCACHE;
fastcgi_keep_conn on;
fastcgi_cache_bypass $no_cache $no_cache_dirs;
fastcgi_no_cache $no_cache $no_cache_dirs;
fastcgi_cache_valid 200 301 5m;
fastcgi_cache_valid 302 5m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
fastcgi_ignore_headers Cache-Control Expires;
expires epoch;
fastcgi_cache_lock on;

If I disable the fastcgi_cache, the php-enabled 404 page works as it has for years.

How would I disable the cache for the custom error page?

Ian
  • 251
  • 2
  • 10

2 Answers2

2

You should remove fastcgi_cache_valid 404 1m;. This forces nginx to cache the 404 errors, but according to spec, these are not supposed to be cached.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
0

Two suggestions from nginx's Igor:

The issue is in fastcgi_cache_key: fastcgi_cache_key "$scheme$request_method$host$request_uri";

It always uses client original $request_uri. Try: fastcgi_cache_key "$scheme$request_method$host$uri?$args";

making that change got me back to the custom 404, but a cached version. Adding the following location fixed that:

location = /dhe404.shtml {
fastcgi_pass 127.0.0.1:10004;
fastcgi_cache  off;
}
Ian
  • 251
  • 2
  • 10
  • You may also remove `fastcgi_cache_valid 404 1m;` as 404 errors shouldn't be cached anyway. If this is solved, please mark it as solved by ticking the check mark so that it turns green. – Michael Hampton Apr 19 '18 at 17:22