I have nginx 1.4.3 running on a Ubuntu 12.04 machine. I have nginx set up to cache pages (they are database-driven but stay fairly static). I'm using MySQL and PHP-FPM.
However, I found that I would intermittently get blank pages cached. There were no errors of any kind, and as soon as I delete the appropriate file from /var/cache/nginx
the page would come back.
After some investigation, I have found the issue is that if a HEAD request is received, nginx caches a blank response as the full response for that URL. So HEAD /example
stores a blank file in the cache file for the /example
page, and a subsequent GET /example
returns a blank page. (I seem to get HEAD requests regularly from various search engines and bots.)
Here is the relevant site config:
location ~ \.php$ {
try_files $uri =404;
fastcgi_cache one;
fastcgi_cache_key $scheme$host$request_uri;
fastcgi_cache_valid 200 5m;
fastcgi_cache_valid 301 302 304 12h;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/mysite/public$fastcgi_script_name;
fastcgi_param HTTPS off;
}
Is this a known bug in nginx? I haven't been able to find any information on this though various searches.
Is there a workaround? There is no way to prevent caching HEAD requests according to this.
I thought maybe there is some 'request method' variable that could be added to fastcgi_cache_key
, so that HEAD and GET requests are cached separately. But I cannot find anything.
Yes, the variable is
$request_method
and that's what you will want to add tofastcgi_cache_key
. This will cause GET and HEAD requests to be cached separately.I believe you'll want to add the http method to
fastcgi_cache_key
or possibly only includeGET
infastcgi_cache_methods
.