nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx修复漏洞

Nginx中修复安全配置漏洞的实战指南

作者:ζั͡山 ั͡有扶苏 ั͡✾

这篇文章主要为大家详细介绍了Nginx中修复安全配置漏洞的实战指南,文中介绍了如何识别常见的漏洞并进行修复,大家可以根据需要进行选择

1. 漏洞识别与风险分析

1.1 关键漏洞清单

检测到以下高危安全缺陷:

SSL/TLS协议信息泄露漏洞(CVE-2016-2183)

关键安全响应头缺失

1.2 安全风险矩阵

漏洞类型潜在攻击风险
CVE-2016-2183弱加密套件导致中间人攻击,敏感数据可能被窃取
X-Content-Type-Options缺失浏览器自动MIME嗅探可能触发XSS跨站脚本攻击
HSTS头未设置HTTPS可能被降级为HTTP,遭遇SSL剥离攻击
X-Frame-Options缺失网站易遭受点击劫持(Clickjacking)
其他安全头缺失整体安全防护薄弱,不符合现代Web安全最佳实践

2. 修复方案与配置代码

2.1 TLS安全加固

# 禁用不安全协议,修复CVE-2016-2183
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK';
ssl_prefer_server_ciphers on;

2.2 安全响应头配置

# 强制添加安全响应头(需在每个location块中配置)
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header X-Download-Options "noopen" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; frame-src *;frame-ancestors 'self' http://<your host> https://<your host> http://<your host> https://<your host>; object-src 'none';" always;

Nginx安全响应头详解

1.Cache-Control

add_header Cache-Control no-cache;

2.X-Content-Type-Options

add_header X-Content-Type-Options "nosniff" always;

3.X-XSS-Protection

add_header X-XSS-Protection "1; mode=block" always;

4.Strict-Transport-Security (HSTS)

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

5.Referrer-Policy

add_header Referrer-Policy "strict-origin-when-cross-origin" always;

6.X-Download-Options

add_header X-Download-Options "noopen" always;

7.X-Permitted-Cross-Domain-Policies

add_header X-Permitted-Cross-Domain-Policies "none" always;

8.Content-Security-Policy (CSP)

add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; frame-src *;frame-ancestors 'self' http://<your host> https://<your host> http://<your host> https://<your host>; object-src 'none';" always;

内容安全策略各部分解析:

default-src 'self'

script-src 'self' 'unsafe-inline' 'unsafe-eval'

style-src 'self' 'unsafe-inline'

img-src 'self' data:

connect-src 'self'

限制AJAXWebSockets等连接只能与当前域名建立

frame-src *

frame-ancestors 'self' http://<your host> ...

object-src 'none'

3. 操作步骤详解

3.1 配置更新流程

1.紧急备份原配置

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak_$(date +%F)

2.修改nginx.conf

在 http 或 server 块中更新TLS配置

重点:在每个 location 块中添加安全响应头(Nginx指令不继承!)

3.配置示例片段

server {
    listen 443 ssl;
    # ...其他配置...
    
    # 添加至所有location块(含HTML文件的特殊处理)
    location / {
        # 此处插入2.2的安全头配置
    }
    
    location ~* \.(html|htm)$ {
        add_header Cache-Control "no-cache";
        # 此处同样插入2.2的安全头配置
    }
}

3.2 重启与测试

# 语法检查(必须执行!)
nginx -t

# 热重载配置
systemctl reload nginx  # 或 nginx -s reload

4. 效果验证方案

4.1 TLS协议验证

# 验证TLSv1.1应被阻断(应该失败,显示协议不可用)
openssl s_client -connect yourdomain.com:443 -tls1_1

# 测试TLSv1.2(应该成功)
openssl s_client -connect <your server host>:443 -tls1_2

成功标志:输出包含 no protocols available

CONNECTED(00000003)
80AB0333AA7F0000:error:0A0000BF:SSL routines:tls_setup_handshake:no protocols available:../ssl/statem/statem_lib.c:104:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 7 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)

4.2 验证安全响应头

# 快速检查安全头
curl -I https://yourdomain.com | grep -iE 'xss|frame|content-security'

成功标志:返回所有配置的安全头

HTTP/2 200
server: rump/c
date: Thu, 03 Jul 2025 03:34:17 GMT
content-type: text/html
content-length: 3998
last-modified: Wed, 02 Jul 2025 12:02:47 GMT
etag: "68651fe7-f9e"
cache-control: no-cache
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-frame-options: SAMEORIGIN
referrer-policy: strict-origin-when-cross-origin
x-download-options: noopen
x-permitted-cross-domain-policies: none
content-security-policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; object-src 'none'; media-src 'self'; frame-src 'self'; worker-src 'self'; frame-ancestors 'self'; form-action 'self'; base-uri 'self';
accept-ranges: bytes

4.3 使用在线安全测试工具(可选)

SSL Labs Server Test: https://www.ssllabs.com/ssltest/

Security Headers: https://securityheaders.com/

例图:

5. 注意事项与最佳实践

6. 修复效果总结

通过本方案可实现:

到此这篇关于Nginx中修复安全配置漏洞的实战指南的文章就介绍到这了,更多相关Nginx修复漏洞内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文