How to avoid duplication of add_header directives in nginx?

The documentation says the following:

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

My problem is that I have several blocks locationthat I want to cache, for example:

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

But this will make me lose all headers declared outside the block. Thus, apparently, the only way is to duplicate these headers in each block location, for example:

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

Doesn't seem right. Any ideas?

+4
source share
2 answers

include , ( , ).

+4

nginx add_header. 🤕

nginx ngx_headers_more. , . , nginx ( Debian/Ubuntu nginx-extras), .

0

All Articles