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

rebaca nginx-forum at forum.nginx.org
Thu Jun 22 07:51:21 UTC 2017


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


DETAILS:
Below shows the connection between entities:
 [ client ] <--->  [ serverX ] <---> [ serverY ]

serverX and serverY both are virtual nginx servers on same machine. 

serverX - proxy handler is enabled by using the directive (proxy_cache)
serverY - custom handler is used which simply outputs a text which serverX
forwards to the client to a particular port say 8111

Below is the sample nginx.conf fragments for each of the server
###############################################################

#serverX (1st server)
server
{
     location /cache1/ 
     {
         ...
         proxy_pass http://localhost:8111/custom/;
         ...
     }

}

#serverY (2nd server)
server
{
     location /custom/ 
     {
         #this is to invoke my custom handler for 2nd server
         my_custom_module_directive;
     }
}
###############################################################

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).

Please let me know if you need further clarifications.

PS - 
cache has been enabled and verified (used the directives - proxy_cache_path,
proxy_cache, proxy_cache_valid)
content-type and other response headers from serverY is properly assigned.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275046,275046#msg-275046



More information about the nginx mailing list