ETag in nginx - Simple Resource Caching

CaffeineFueled

2025/10/20

An ETag (Entity Tag) is an HTTP response header that identifies a version of a resource. This allow efficient caching by providing the client a method to check for a new version before downloading.

In general, the ETag is based on the file’s last modification time and size and usually handled by the reverse-proxy. This is called the weak ETag/ weak validation.

Alternative, there is a strong or content-based Etag that hashes the resources and provides a new version with every change. In most cases, the application has to manage the Etag.

Weak ETag headers can have the prefix W/ - this is optional and often only displayed with the initial 200 HTTP response and not the 304 HTTP response.

/images/blog/etag-browser-weak.png

More information in the RFC9110 8.8.1 - Weak versus Strong.


Enabling ETag in nginx

Enabling (weak) ETags in nginx is simple - add the following config to the http, server or location block to enable ETag headers.

[...]
  etag on;
  add_header Cache-Control "no-cache" always;
[...]

The config can be overwritten in a more specific block, in case you want to change the caching for images or other resources.

Check syntax with sudo nginx -t and reload the nginx config.

That is it.

Testing it with curl

In this article we are going to do some testing with curl. The ETag header can be seen in the browser dev tools as well.

curl -I https://ittavern.com

HTTP/2 200 
server: nginx
date: Mon, 20 Oct 2025 15:42:14 GMT
content-type: text/html
content-length: 20665
last-modified: Sat, 18 Oct 2025 20:55:34 GMT
vary: Accept-Encoding
etag: "68f3fec6-50b9"
[...]

This ETag won’t change as long as the size (content-length) or time of last modification (last-modified) of the resources changes.

We now can check the resource again with the ETag to check for changes.

curl -H 'If-None-Match: "68f3fec6-50b9"' -I https://ittavern.com

HTTP/2 304 
server: nginx
date: Mon, 20 Oct 2025 16:24:47 GMT
last-modified: Sat, 18 Oct 2025 20:55:34 GMT
etag: "68f3fec6-50b9"
[...]

The 304 HTTP code stands for Not Modified.


This is how it looks in the browser

/images/blog/etag-browser.png



Share:
E-Mail hellofoo@ittafoovern.comcom

Most recent Articles:
  • ETag in nginx - Simple Resource Caching
  • Sending nginx Logs to Loki with Grafana Alloy
  • How to: Cisco ISE backup to SFTP repository with public key authentication
  • Dummy IP & MAC Addresses for Documentation & Sanitization
  • Deploying ISSO Commenting System for Static Content using Docker