In modern versions of Nginx, the ETag is automatically generated for static file types, even if you don't specifically enable the etag on
in your location blocks or otherwise:
https://nginx.org/en/docs/http/ngx_http_core_module.html#etag
For example, your PNG or JPEG files served by Nginx will automatically include the HTTP headers for content-length
, last-modified
, and etag
on static files that are not gzipped even if you don't manually enable them...
However, what file types does Nginx do this for by default? When I tested this by including .html
file extensions in my static files location block, and manually added etag on
to all of my "static" file types (including .html
files, even though they are not typically considered static), Nginx stripped out the etag
HTTP header for my .html
files (even though it was hardcode enabled in the location block) but not other file types such as PNG or JPEG.
Update: it was Cloudflare stripping out the etag
header on my .html files and not Nginx. However, Nginx does strip the content-length
header when gzip is enabled on certain file types. To make matters more confusing, Cloudflare adds gzip on some content automatically.
I don't see any documentation about this... which files types will Nginx automatically enable ETags for, and which file types will Nginx ignore/strip ETags for by default?
Edit: These gzip related discussion might be related:
https://javorszky.co.uk/2019/03/28/etag-if-match-nginx-and-you/
https://forum.nginx.org/read.php?2,286645,286645#msg-286645
https://forum.nginx.org/read.php?2,240120,240120#msg-240120
TIL that Nginx strips weak ETags from upstream responses when gzipping them for "Accept-Encoding: gzip". Thanks, internet.
nginx -T
.nginx.conf
..html
files I was testing on my server. In fact, even when I didn't setetag on;
Nginx still decided to send an ETag header for my HTML files, so this does seem to be gzip related.