Nginx 安全防护与 HTTPS 部署实战文档

Nginx 安全防护与 HTTPS 部署实战文档


一、环境准备

  • 服务器:Linux(CentOS 7+/Ubuntu 18+)
  • Nginx:1.18+
  • 域名已备案并解析到服务器
  • 开放端口:80、443

二、HTTPS 证书获取

1. 安装 Certbot

bash

运行

复制代码
# CentOS
yum install -y certbot certbot-nginx

# Ubuntu
apt install -y certbot python3-certbot-nginx

2. 一键申请证书

bash

运行

复制代码
certbot --nginx -d yourdomain.com -d www.yourdomain.com

按提示操作,自动生成 HTTPS 配置。

证书路径(默认):

plaintext

复制代码
/etc/letsencrypt/live/yourdomain.com/
├── fullchain.pem
└── privkey.pem

3. 证书自动续期

bash

运行

复制代码
echo "0 3 * * * certbot renew --quiet" >> /etc/crontab

三、HTTPS 最优配置(直接写入 Nginx 站点)

nginx

复制代码
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    # 强制跳转 HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name yourdomain.com www.yourdomain.com;

    # SSL 证书
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    # SSL 安全协议
    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;
    ssl_prefer_server_ciphers on;

    # 会话优化
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # 安全头部
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options SAMEORIGIN;

    root /var/www/html;
    index index.html;
}

四、Nginx 安全防护实战

1. 隐藏 Nginx 版本号

nginx

复制代码
http {
    server_tokens off;
}

2. 防 SQL 注入、XSS、目录遍历

nginx

复制代码
location / {
    if ($query_string ~* "union|select|insert|delete|update|drop|script|<|>") {
        return 403;
    }
    if ($request_uri ~* "\.\.\/") {
        return 403;
    }
}

3. 限制请求频率(防 CC 攻击)

nginx

复制代码
http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
}

server {
    location / {
        limit_req zone=one burst=20 nodelay;
    }
}

4. 禁止恶意 UA / 爬虫

nginx

复制代码
if ($http_user_agent ~* "scrapy|curl|wget|python|bot|spider") {
    return 403;
}

5. 禁止访问敏感文件

nginx

复制代码
location ~ /\.ht { deny all; }
location ~ /\.git { deny all; }
location ~ /\.env { deny all; }
location ~ /config\.php { deny all; }

6. 限制上传大小

nginx

复制代码
client_max_body_size 10m;
client_body_timeout 60;

五、配置检查与重启

bash

运行

复制代码
nginx -t
systemctl restart nginx

六、安全检测

可通过以下工具验证:

bash

运行

复制代码
curl -I https://yourdomain.com

七、生产安全总结

  1. 必须开启 HTTPS + HSTS
  2. 隐藏版本、限制请求、过滤恶意参数
  3. 禁止敏感路径、恶意 UA、目录穿越
  4. 证书自动续期,避免过期故障
  5. 开启 HTTP/2 提升性能
相关推荐
shuxiaohua2 小时前
一次现网问题定位-nginx报错upstream sent invalid chunked response while reading upstream
运维·nginx
lplum_2 小时前
2026 中国高校智能机器人创意大赛 软件系统安全赛 初赛wp
安全·web安全·网络安全·系统安全·密码学·网络攻击模型·安全威胁分析
Jasminee2 小时前
SSH 服务攻防实战
笔记·安全
lsrsyx3 小时前
iCoin:构建更高效、安全的数字资产交易新体验
安全·区块链
云安全联盟大中华区3 小时前
[特殊字符] | OpenClaw威胁模型:MAESTRO框架分析
大数据·人工智能·深度学习·安全·ai
m0_738120723 小时前
应急响应——知攻善防挖矿事件应急溯源详细过程
网络·数据库·安全·web安全
秃头摸鱼侠4 小时前
OpenClaw 团队级落地手册:规范、权限、安全、CI/CD 一体化实践
数据库·安全·ci/cd·ai
咚为4 小时前
深入浅出 Rust FFI:从内存安全到二进制兼容
开发语言·安全·rust
2501_915918414 小时前
通过IPA 结构调整和资源指纹变化来处理 iOS 应用相似度问题
android·ios·小程序·https·uni-app·iphone·webview