文档是这样说的:



我的问题是我有几个 location 块要缓存,如下所示:

add_header X-Frame-Options SAMEORIGIN;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

location ~ ^/img/(.*)\.(png|jpg|jpeg|gif|bmp)$ {
    expires 1w;
    add_header Cache-Control public;
}

但这将使我丢失在块外声明的所有 header 。所以显然唯一的方法是在每个 location 块上复制这些标题,例如:
add_header X-Frame-Options SAMEORIGIN;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

location ~ ^/img/(.*)\.(png|jpg|jpeg|gif|bmp)$ {
    expires 1w;
    add_header Cache-Control public;
    add_header X-Frame-Options SAMEORIGIN;
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
}

好像不太对有任何想法吗?

最佳答案

在包含共享头文件的每个位置使用 include (这必须重复,但只需要在包含的配置中更新,而不是单独更新每个块)。

关于nginx - 如何避免在 nginx 中重复 add_header 指令?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28665117/

10-13 09:20
查看更多