Opened 4 years ago

Last modified 6 days 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:
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

Description

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:
https://code.facebook.com/posts/557147474482256/this-browser-tweak-saved-60-of-requests-to-facebook/
https://hacks.mozilla.org/2017/01/using-immutable-caching-to-speed-up-the-web/

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->value.data = (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 (5)

comment:1 by Ruslan Ermilov, 4 years ago

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

in reply to:  1 comment:2 by carrete@…, 3 years ago

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:

From: https://www.nginx.com/blog/http-strict-transport-security-hsts-and-nginx/

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.

comment:3 by eoghanmurray@…, 19 months ago

Cache-Control: ...,immutable; support is also in Webkit: https://bugs.webkit.org/show_bug.cgi?id=167497

Edge appears to have it since v15: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

(Chromium is wontfix: https://bugs.chromium.org/p/chromium/issues/detail?id=611416 )

I think this would be great to add with expires max and I (naïvely) can't see any downside :D

comment:4 by dvershinin@…, 9 months ago

I've created ngx_immutable module so one can specify immutable on; in lieu of expires max;.

comment:5 by Maxim Dounin, 6 days ago

See also #2100.

Note: See TracTickets for help on using tickets.