Opened 9 months ago

Last modified 4 months ago

#1188 new enhancement

Send "immutable" keyword in Cache-Control when "expires max"

Reported by: fazalmajid@… Owned by:
Priority: minor Milestone:
Component: nginx-module Version: 1.11.x
Keywords: Cc:
Sensitive: no
uname -a: SunOS mordac 5.11 joyent_20160218T022556Z i86pc i386 i86pc
nginx -V: nginx version: nginx/1.11.8 built by gcc 6.3.0 (GCC) built with OpenSSL 1.1.0c 10 Nov 2016 TLS SNI support enabled configure arguments: --prefix=/usr/local --conf-path=/usr/local/etc/nginx --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-select_module --with-poll_module --with-http_stub_status_module --with-http_gzip_static_module --without-http_scgi_module --without-http_uwsgi_module --with-cc-opt='-I/usr/local/include -I/usr/local/ssl/include -DNGINX_DTRACE=1' --with-ld-opt='-L /usr/local/lib -L /usr/local/ssl/lib' --error-log-path=/tmp/nginx_error.log --with-http_v2_module --with-http_auth_request_module --with-stream


Per the documentation for ngx_http_headers_module:

The max parameter sets “Expires” to the value “Thu, 31 Dec 2037 23:55:55 GMT”,
and “Cache-Control” to 10 years.

At Facebook's urging, Firefox implemented an additional keyword to Cache-Control for immutable assets:

Adding this "immutable" keyword to the Cache-Control header for "expires max" requests would improve cache effectiveness for Firefox users.

The change itself is trivial:

--- src/http/modules/ngx_http_headers_filter_module.c.dist      Thu Jan 26 11:14:29 2017
+++ src/http/modules/ngx_http_headers_filter_module.c   Thu Jan 26 11:15:23 2017
@@ -301,7 +301,7 @@
     if (expires == NGX_HTTP_EXPIRES_MAX) {
         e-> = (u_char *) "Thu, 31 Dec 2037 23:55:55 GMT";
         /* 10 years */
-        ngx_str_set(&cc->value, "max-age=315360000");
+        ngx_str_set(&cc->value, "max-age=315360000, immutable");
         return NGX_OK;

Change History (2)

comment:1 follow-up: Changed 6 months ago by ru

What's wrong with add_header Cache-Control immutable;?

comment:2 in reply to: ↑ 1 Changed 4 months ago by carrete@…

Replying to ru:

What's wrong with add_header Cache-Control immutable;?

Primarily because of:

Inheritance Rules for add_header Directives

NGINX configuration blocks inherit add_header directives from their enclosing blocks, so you just need to place the add_header directive in the top-level server block. There’s one important exception: if a block includes an add_header directive itself, it does not inherit headers from enclosing blocks, and you need to redeclare all add_header directives:


I replaced "expires max" with two add_header directives for Expires and Cache-Control which disabled add_header directives that were set elsewhere, notably for Strict-Transport-Security and X-Frame-Options.

Note: See TracTickets for help on using tickets.