我有一个 Nginx 代理设置,我向服务器添加了几个与安全相关的标头,以便它们返回所有代理位置。在某些位置我需要添加额外的标头(例如Content-Security-Policyto /),而在其他特定位置我需要删除在服务器级别添加的标头之一(例如X-Frame-Optionsfrom /framepage.html)。
nginx.conf# ...
server {
# ...
include security-headers.conf;
location / {
proxy_pass http://web:5000/;
include security-headers.conf;
add_header Content-Security-Policy "my csp...";
}
location = /framepage.html {
proxy_pass http://web:5000/framepage.html;
# TODO: remove `X-Frame-Options` response header from this specific page
# Tried add_header X-Frame-Options "";
# Tried proxy_set_header X-Frame-Options "";
# Tried proxy_hide_header X-Frame-Options;
}
location /api/ {
proxy_pass http://api:5000/;
}
location /otherstuff/ {
proxy_pass http://otherstuff:5000/;
}
# ...
}
Run Code Online (Sandbox Code Playgroud)
security-headers.confadd_header Referrer-Policy same-origin;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,但似乎都没有X-Frame-Options从/framepage.html位置响应中删除标头:
add_header X-Frame-Options "";proxy_set_header X-Frame-Options "";proxy_hide_header X-Frame-Options;如何X-Frame-Options从/framepage.html位置响应中删除标头?
Wil*_*ilt 20
标头配置属性有点令人困惑,这就是它们的作用:
proxy_set_header是设置请求头
add_header是向响应添加头
proxy_hide_header是隐藏响应头
如果您想替换响应中已存在的标头,它是不够的,add_header因为它将堆叠值(来自服务器和您添加的值)。
您必须分两步执行此操作:
1)删除标题:
proxy_hide_header Access-Control-Allow-Origin;
2)添加您的自定义标头值:
add_header Access-Control-Allow-Origin "*" always;
您可能可以尝试使用第 3 方“Headers More”模块:
https://github.com/openresty/headers-more-nginx-module
以及类似的内容:
load_module modules/ngx_http_headers_more_filter_module.so;
http {
...
more_clear_headers 'X-Frame-Options';
...
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 headers_more 模块。例子:
location / {
proxy_pass http://upstream_server/;
more_clear_headers 'Access-Control-Allow-Origin';
}
Run Code Online (Sandbox Code Playgroud)
https://www.nginx.com/resources/wiki/modules/headers_more/
| 归档时间: |
|
| 查看次数: |
39429 次 |
| 最近记录: |