Nginx – Why does NGinx send content in a HTTP 304 response when If-None-Match / If-Modified-Since is a match

cachehttp-headersnginx

My default NGinx installation is returning a 304 Not Modified response to a request which has a matching If-None-Match header and a non-expired If-Modified-Since header which is correct behaviour but NGinx also adds the content of the resource to the 304 response. One would think that NGinx wouldn't add the content, but simply return the 304 with an empty body indicating to the browser that using their cached resource is sufficient.

Is there a reason for this behaviour and how do I disable it?

Best Answer

Are you sure NGINX is returning the content?

Depending on how you're viewing the response, this might only appear to be the case.

For example, when Chrome dev tools receives a 304 from NGINX, it displays the content, which looks like it was sent from NGINX, but in reality is just being retrieved from cache by Chrome.

You can try this out by right clicking the file -> "Copy -> Copy as cURL" in the Chrome dev tools.

Add -vv to the beginning of what you just copied (ex curl -vv <your-url> -H 'HeaderKey: HeaderValue' <etc>), then execute in terminal/shell.

You'll probably see that it doesn't return body content (even though it is a 304.) You can then try the same request with just the URL (curl -vv <your-url>), and see a body without the 304.