在Debian系统上配置Nginx以添加安全头信息,可以提高网站的安全性。以下是一些常见的安全头信息及其配置方法:
-
Content-Security-Policy (CSP): CSP可以防止跨站脚本攻击(XSS)和其他代码注入攻击。
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://trustedscripts.example.com; object-src 'none';"; -
X-Content-Type-Options: 防止浏览器MIME类型嗅探。
add_header X-Content-Type-Options "nosniff"; -
X-Frame-Options: 防止点击劫持攻击。
add_header X-Frame-Options "SAMEORIGIN"; -
Strict-Transport-Security (HSTS): 强制浏览器使用HTTPS连接。
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; -
X-XSS-Protection: 启用浏览器的XSS过滤器(现代浏览器已经不再支持这个头,但仍然可以添加以提高兼容性)。
add_header X-XSS-Protection "1; mode=block"; -
Referrer-Policy: 控制Referer头的发送。
add_header Referrer-Policy "no-referrer-when-downgrade"; -
Feature-Policy: 控制浏览器功能的启用(类似于CSP,但更细粒度)。
add_header Feature-Policy "accelerometer 'none'; camera 'none'; geolocation 'none';"; -
Permissions-Policy: 控制浏览器功能的启用(类似于Feature-Policy,但更现代)。
add_header Permissions-Policy "accelerometer=(), camera=(), geolocation=()";
配置示例
以下是一个完整的Nginx配置示例,包含了上述安全头信息:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://trustedscripts.example.com; object-src 'none';";
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "SAMEORIGIN";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy "no-referrer-when-downgrade";
add_header Feature-Policy "accelerometer 'none'; camera 'none'; geolocation 'none';";
add_header Permissions-Policy "accelerometer=(), camera=(), geolocation=()";
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
注意事项
- CSP: CSP头信息非常强大,配置不当可能会导致网站功能失效。建议先在测试环境中验证配置。
- HSTS: 一旦启用HSTS,浏览器会记住这个设置一段时间(由
max-age指定),在此期间无法通过HTTP访问该网站。 - Feature-Policy 和 Permissions-Policy: 这些头信息相对较新,可能不被所有浏览器支持。建议查阅最新的浏览器兼容性表。
通过以上配置,可以显著提高Nginx服务器的安全性。
以上就是关于“Debian Nginx SSL如何配置安全头信息”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm