Skip to content

dynamic-etag: NGINX module for adding ETag to dynamic content

Installation

You can install this module in any RHEL-based distribution, including, but not limited to:

  • RedHat Enterprise Linux 7, 8, 9 and 10
  • CentOS 7, 8, 9
  • AlmaLinux 8, 9
  • Rocky Linux 8, 9
  • Amazon Linux 2 and Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-dynamic-etag
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-dynamic-etag

Enable the module by adding the following at the top of /etc/nginx/nginx.conf:

load_module modules/ngx_http_dynamic_etag_module.so;

This document describes nginx-module-dynamic-etag v0.3.0 released on Nov 14 2026.


ETag for the responses your application generates on the fly.

NGINX adds an ETag to static files, and nothing to what comes from PHP-FPM, a proxied application or any other upstream. Those pages are then downloaded in full on every visit, even when not a byte has changed. This module hashes the response body, sends the hash as the ETag, and answers a matching If-None-Match with 304 Not Modified and no body.

It works with every upstream module: proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, grpc_pass.

Synopsis

location ~ \.php$ {
    dynamic_etag on;
    fastcgi_pass unix:/run/php-fpm/www.sock;
    include      fastcgi_params;
}
$ curl -sI -X GET https://example.com/ | grep -i etag
ETag: "6f5902ac237024bdd0c176cb93063dc4"

$ curl -s -o /dev/null -w '%{http_code} %{size_download}\n' \
    -H 'If-None-Match: "6f5902ac237024bdd0c176cb93063dc4"' https://example.com/
304 0

How it works

The ETag has to be in the response header, and the header goes out before the body. So the module holds the response back: it collects the body as the upstream delivers it, hashes all of it (MD5), and only then sends the header, now with the ETag, followed by the body.

  • The hash always covers the whole body, however it arrives: in one piece or many, with proxy_buffering on or off, from memory or from a temporary file NGINX spooled it to.
  • A body larger than dynamic_etag_buffer_size gets no ETag and is passed on untouched. There is never a partial hash.
  • Conditional requests are answered by NGINX itself, against the computed ETag: If-None-Match gets a 304, a failed If-Match gets a 412.
  • The body is hashed before compression. gzip and other compression filters then weaken the ETag (W/"..."), as they do for static files.

Directives

dynamic_etag

  • syntax: dynamic_etag on | off | $variable
  • default: off
  • context: http, server, location

Enables the ETag for upstream responses. With a variable, the ETag is added when its value is on; see Conditional use.

dynamic_etag_types

  • syntax: dynamic_etag_types mime-type ...
  • default: text/html
  • context: http, server, location

Adds the ETag to responses with the listed MIME types in addition to text/html. The special value * matches any type.

dynamic_etag_strength

  • syntax: dynamic_etag_strength strong | weak | $variable
  • default: strong
  • context: http, server, location

Makes the generated ETag strong ("...") or weak (W/"..."). A weak ETag says the page is equivalent, not byte-for-byte identical. With a variable, the ETag is weak when its value is weak.

dynamic_etag_buffer_size

  • syntax: dynamic_etag_buffer_size size
  • default: 1m
  • context: http, server, location

The largest response body the module holds in memory to hash. A larger response is sent without an ETag, exactly as NGINX would send it with the module off.

Which responses get an ETag

All of these have to be true:

  • the response comes from an upstream (not a static file, return or an error page: NGINX handles static files itself);
  • its status is 200;
  • its type is listed in dynamic_etag_types;
  • the request is not HEAD (there is no body to hash);
  • the body is not empty and fits into dynamic_etag_buffer_size;
  • the upstream did not send an ETag of its own. That one is kept, and NGINX answers conditional requests against it.

Notes

The page has to be the same to get the same ETag. A page that embeds a timestamp, a random token or a per-request nonce changes on every request, and so does its hash. Check with:

diff <(curl -s https://example.com/) <(curl -s https://example.com/) && echo stable

The response is sent when it is complete. The client receives the first byte once the application has produced the last one. For an ordinary page that is a few milliseconds of difference; for a response that streams on purpose (progress output, server-sent events) leave dynamic_etag off.

A response that the upstream fails to finish is not sent at all. If the application dies mid-page, the client gets a closed connection, not half a page with a 200 and an ETag.

Testing with curl. curl -I sends a HEAD request, which gets no ETag. Use curl -sI -X GET instead.

Conditional use

The directives are not valid inside if. Use map:

map $request_uri $dyn_etag {
    default      on;
    ~^/checkout  off;
    ~^/api/      off;
}

map $http_user_agent $etag_strength {
    default      strong;
    ~*legacybot  weak;
}

server {
    location / {
        dynamic_etag          $dyn_etag;
        dynamic_etag_strength $etag_strength;
        proxy_pass            http://backend;
    }
}