简体   繁体   English

缓存控制头重复; 有效与否? (Nginx)

[英]Cache-Control headers repeated; valid or not? (Nginx)

I've got a resource in my Nginx that is configured like this:我的 Nginx 中有一个资源配置如下:

location ~ foo\.js$ {
    add_header Cache-Control public;
    expires 1d;
}

If I open this with Firebug and look at the headers it shows this:如果我用 Firebug 打开它并查看标题,它会显示:

Cache-Control   max-age=86400, public

The site is using HTTPS so I want to make sure I get it right because apparently browsers don't cache it unless it's max-age>0 AND public .该网站正在使用 HTTPS,所以我想确保我做对了,因为显然浏览器不会缓存它,除非它是 max-age>0 AND public See this 看到这个

But what happens with my Nginx when I use curl -Ik https://... is that it says:但是当我使用curl -Ik https://...时我的 Nginx 会发生什么,它说:

...
Expires: Sat, 22 Jan 2011 18:23:36 GMT
Cache-Control: max-age=86400
Cache-Control: public
...

It repeats the Cache-Control header!它重复Cache-Control标头! Clearly Firebug doesn't mind.显然 Firebug 并不介意。 But is it right?但这是对的吗?

Is there a perhaps a better way to set Expires and Cache-Control (with public ) in one just two lines?是否有更好的方法来在两行中设置ExpiresCache-Control (使用public )?

Yes, it's valid and equivalent to use multiple Cache-Control headers.是的,使用多个 Cache-Control 标头是有效且等效的。

From the HTTP 1.1 spec :HTTP 1.1 规范

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [ie, #(values)].当且仅当该头字段的整个字段值定义为逗号分隔列表[即,#(values)] 时,消息中可以存在多个具有相同字段名称的消息头字段。 It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma.通过将每个后续字段值附加到第一个字段值,每个字段值用逗号分隔,必须可以将多个标题字段组合成一个“字段名称:字段值”对,而不改变消息的语义。

It's easy to verify that this provision applies to the Cache-Control header because of how it's defined :很容易验证此规定是否适用于 Cache-Control 标头,因为 如何定义的

Cache-Control = "Cache-Control" ":" 1#cache-directive Cache-Control = "Cache-Control" ":" 1#cache-directive

To understand how to interpret the line above, see the spec's notational conventions .要了解如何解释上面的行,请参阅规范的符号约定 The 1# means "a comma-separated list of one or more". 1#表示“一个或多个逗号分隔的列表”。

I was having the same problem on different configuration.我在不同的配置上遇到了同样的问题。 This is what currently works for me.这是目前对我有用的方法。

Example taken from Module ngx_http_headers_module来自模块 ngx_http_headers_module 的示例


map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    application/octet-stream   max;
    ~image/                    max;
}

server {
        expires $expires;
        ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM