Nginx not caching the content when the response is just a plain text string

Maxim Dounin mdounin at
Thu Jun 22 13:36:53 UTC 2017


On Thu, Jun 22, 2017 at 03:51:21AM -0400, rebaca wrote:


> I am trying to access the link "localhost/cache1/sample.txt" which hits
> serverX. serverX then finds that the file is not present, takes it as MISS,
> and then forwards the request probably as
> 'localhost:8111/custom/sample.txt'. But here since "/custom/" is used as
> filter in location, my custom module handler gets invoked which simply puts
> a text string in the response body, which is then forwarded to the client. I
> am able to see the response in the html.
> The issue is every time I am accessing the file, this sample.txt is still
> taken, as per cache log, as a MISS (which should have been a HIT instead
> since previously 1st server serverX should have saved the text string as
> sample.txt and would have served directly)
> Kindly let me know why this behaviour of serverX not caching such a response
> from serverY (which is a text in the response body).

There may be multiple reasons to not cache a response even with 
cache properly configured, including:

- no Cache-Control / Expires headers, and no proxy_cache_valid 
  configured for a particular response code;

- various headers which prevent caching, including Cache-Control 
  which disables caching, Expires in the past, Set-Cookie, "Vary: *";

- incomplete or incorrect response (for example, you announce 
  some Content-Length, but the actual body is smaller).

Some additional details can be found in the documentation, see

If you don't see why a particular response is not cached, a 
debugging log might help, see

Maxim Dounin

More information about the nginx mailing list