Nginx配置Http响应头安全策略_nginx content-security-policy

本文详细介绍了如何在Nginx中配置HTTP响应头的安全策略,包括Content-Security-Policy、X-Content-Type-Options、X-XSS-Protection、X-Frame-Options、Strict-Transport-Security、Referrer-Policy、X-Download-Options和X-Permitted-Cross-Domain-Policies等,以增强网站的安全性,防止恶意代码执行和攻击。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

server {
    listen 80;
    server_name example.com;

    location / {
        add_header Content-Security-Policy "script-src 'self' https://ajax.googleapis.com;";
        # 其他配置...
    }
}

3.12style-src指令的参数、说明和示例
参数 说明 示例
self 只允许从同源加载样式表。 style-src 'self'
unsafe-inline 允许内联样式,但可能会存在安全风险。 style-src 'unsafe-inline'
https://example.com/css/ 只允许从指定的URL加载样式表。 style-src 'https://example.com/css/'
strict-dynamic 如果样式表是通过动态方式插入到页面中的,则不允许执行该样式表。如果样式表是静态的,则允许执行。 style-src 'strict-dynamic'
nonce (仅适用于HTTPS) 要求样式表必须具有特定的随机值(nonce),以防止重复攻击。 style-src 'nonce https://example.com/css/'
hash (仅适用于HTTPS) 要求样式表必须具有特定的哈希值,以防止重复攻击。通常与nonce一起使用。 style-src 'hash https://example.com/css/'
report-sample (仅适用于HTTPS) 要求服务器在响应头中包含一个样本,以便CSP能够检测到潜在的XSS攻击。通常与report-uri指令一起使用。 style-src 'report-sample https://example.com/css/'
upgrade-insecure-requests (仅适用于HTTPS) 要求将所有不安全的请求升级为HTTPS请求,以提高安全性。通常与report-uri指令一起使用。 style-src 'upgrade-insecure-requests https://example.com/css/'
blockallmixedcontent (仅适用于HTTPS) 要求将所有混合内容的请求升级为HTTPS请求,以提高安全性。通常与report-uri指令一起使用。 style-src 'blockallmixedcontent https://example.com/css/'

以下是一个示例(允许从 Google 的 AJAX API加载样式):

server {
    listen 80;
    server_name example.com;

    location / {
        add_header Content-Security-Policy "style-src 'self' https://fonts.googleapis.com;";
        # 其他配置...
    }
}

3.13report-uri指令的参数、说明和示例
参数 说明 示例
none 不发送任何报告,是最严格的设置 report-uri none;
self 向同源的URL发送报告 report-uri self;
* 向任意URL发送报告 report-uri *;
http://example.com/report 向指定的URL发送报告 report-uri http://example.com/report;
https://csp.example.com/report?source=%s&hash=%s 向指定的安全站点发送报告,同时传递一些额外的信息,如来源和哈希值等 report-uri https://csp.example.com/report?source=%s&hash=%s;

以下是一个示例(添加了一个 report-uri 指令,用于指定报告 CSP 违规的 URL):

server {
    listen 80;
    server_name example.com;

    location / {
        add_header Content-Security-Policy "report-uri /csp-report.php;";
        # 其他配置...
    }
}

3.14upgrade-insecure-requests指令的参数、说明和示例
参数 说明 示例
none 不升级不安全的请求,是最严格的设置 upgrade-insecure-requests none;
1 将HTTP升级请求(如HTTP/1.0到HTTP/1.1)视为不安全,不允许升级 upgrade-insecure-requests 1;
2 将HTTPS降级请求(如HTTPS到HTTP)视为不安全,不允许降级 upgrade-insecure-requests 2;
3 将不安全的跨域请求(如使用document.domain)视为不安全,不允许跨域请求 upgrade-insecure-requests 3;

在 Nginx 中,可以通过以下配置来启用或禁用 upgrade-insecure-requests 指令:

  1. 启用 upgrade-insecure-requests 指令:
http {
    add_header Content-Security-Policy "upgrade-insecure-requests";
    ...
}

  1. 禁用 upgrade-insecure-requests 指令:
http {
    add_header Content-Security-Policy "default-src 'self'; upgrade-insecure-requests off";
    ...
}

注意:在生产环境中,建议启用 upgrade-insecure-requests 指令,以防止潜在的安全风险。

3.15sandbox指令的参数、说明和示例

sandbox 为特定的元素或脚本添加额外的限制和安全策略,防止恶意代码执行和攻击行为。
sandbox 指令可以包含以下关键字进行配置:

关键字 说明
allow-s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值