大网数据广告活动
服务器运维问答

nginx: combine Expires and Cache-Control: immutable headers

· 52CE 我爱测

nginx expires directive sets 2 headers, Expires and Cache-Control : Config: expires 1d; Headers: Expires: Tue, 24 Nov 2020 12:51:31 GMT Cache-Control: max-age=86400 I would like to keep Expires header but also set Cache-Control t…

问题原文

原帖链接 · Selivanov Pavel · CC BY-SA 4.0

nginx expires directive sets 2 headers, Expires and Cache-Control:

Config:

expires 1d;

Headers:

Expires: Tue, 24 Nov 2020 12:51:31 GMT
Cache-Control: max-age=86400

I would like to keep Expires header but also set Cache-Control to public, max-age=86400, immutable. But that produces double Cache-Control headers:

Config:

expires 1d;
add_header Cache-Control "public, max-age=86400, immutable"

Headers:

Expires: Tue, 24 Nov 2020 12:57:53 GMT
Cache-Control: max-age=86400
Cache-Control: public, max-age=86400, immutable

I can not just use add_header Expires ..., because it requires exact time in future, not just number of seconds.

I tried using more_set_headers from ngx_headers_more module, but Cache-Control header set by expires directive is still there.

Is there any way to combine correct Expires header with Cache-Control set to immutable?

社区高分解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

The Cache-Control: header can appear more than once, provided that the two headers do not try to use the same directives. If they do, that directive is ignored. (RFC 7234 § 4.2.1)

Thus you can let nginx send its header with the max-age directive, and send your own with only the other directives.

The result would be:

Expires: Tue, 24 Nov 2020 12:57:53 GMT
Cache-Control: max-age=86400
Cache-Control: public, immutable

(But if max-age=86400 appeared in both headers it would be ignored.)

It's normal and expected that certain header fields may appear more than once. RFC 7230 § 3.2.1 specifies that recipients may combine them into a single header, but this is not required.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-11-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。