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

Cho*_*per 4 nginx http-headers

文档说明了这一点:

当且仅当在当前级别上没有定义add_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;
}
Run Code Online (Sandbox Code Playgroud)

但这将使我失去在块外宣布的所有标题.所以显然唯一的方法是在每个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;";
}
Run Code Online (Sandbox Code Playgroud)

似乎不对.有任何想法吗?

Aey*_*oun 5

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