gpt4 book ai didi

nginx - 如何避免 nginx 中重复的 add_header 指令?

转载 作者:行者123 更新时间:2023-12-02 05:09:22 28 4
gpt4 key购买 nike

文档是这样说的:

These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.

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

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;
}

但这将使我丢失在 block 之外声明的所有 header 。因此显然唯一的方法是在每个 location block 上复制这些 header ,例如:

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在包含共享 header 的每个位置中(必须重复,但只需要在包含的配置中更新,而不是单独更新每个 block )。

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com