1

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.

2 Answers 2

3

Yes, the variable is $request_method and that's what you will want to add to fastcgi_cache_key. This will cause GET and HEAD requests to be cached separately.

2
  • Thanks, looks like this has solved the issue! Do you think this the best method? Seems like there would be a reason why $request_method is not there by default (i.e. it's not listed in any guides or official nginx docs). Commented Jul 15, 2014 at 16:55
  • Not including this by default makes little sense, so I suspect it's a bug or simple oversight. Commented Jul 15, 2014 at 16:59
2

I believe you'll want to add the http method to fastcgi_cache_key or possibly only include GET in fastcgi_cache_methods.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.