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

文档说明了这一点:

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

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

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

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

似乎不对.有任何想法吗?
最佳答案
你是在ngx_headers_more模块之后:
https://www.nginx.com/resources/wiki/modules/headers_more/

是的,add_header的行为真的很烦人:)

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。