如何避免在Nginx的add_header指令的重复?

文档说这个:

当且仅当在当前级别上没有定义add_header指令时,这些指令才从前一级inheritance。

我的问题是,我有几个location块,我想要caching,就像这样:

 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的行为真的很刺激:)