Nginx 配置 HTTPS 与 WSS 完整指南(最新推荐)

Nginx 配置 HTTPS 与 WSS 完整指南

一、准备工作
  1. 获取 SSL 证书
  • 从可信机构(如 Let's Encrypt)申请证书
  • 获得以下文件:
    • 域名证书:domain.crt
    • 私钥文件:domain.key
    • 中间证书链:chain.crt
  1. 推荐合并证书链:
bash 复制代码
cat domain.crt chain.crt > fullchain.pem
二、HTTPS 基础配置
nginx 复制代码
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/domain.key;
    
    # 现代加密配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    # 其他配置...
}
三、WSS 配置(WebSocket Secure)
nginx 复制代码
location /websocket/ {
    proxy_pass http://backend_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    
    # 保持长连接
    proxy_read_timeout 86400s;
    proxy_send_timeout 86400s;
    
    # 传递必要头信息
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}
四、最佳实践
  1. 强制 HTTPS 跳转:
nginx 复制代码
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}
  1. OCSP 装订优化:
nginx 复制代码
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/chain.crt;
  1. 安全增强配置:
nginx 复制代码
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
五、完整配置示例
nginx 复制代码
http {
    # 共享 SSL 配置
    ssl_session_cache shared:le_nginx_SSL:10m;
    
    server {
        listen 443 ssl;
        server_name example.com;

        # 证书路径
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        # 安全协议
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        # WebSocket 配置
        location /wss/ {
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        # 静态文件服务
        location / {
            root /var/www/html;
            index index.html;
        }
    }

    # HTTP 强制跳转
    server {
        listen 80;
        server_name example.com;
        return 301 https://$server_name$request_uri;
    }
}
六、验证与测试
  1. 检查配置语法:
bash 复制代码
sudo nginx -t
  1. HTTPS 验证:
bash 复制代码
curl -I https://example.com
# 应返回 HTTP/2 200
  1. WSS 测试:
javascript 复制代码
// 浏览器端测试代码
const ws = new WebSocket("wss://example.com/wss");
ws.onopen = () => console.log("Connected");
七、常见问题排查
  1. 证书错误:
  • 确认证书路径正确
  • 检查文件权限(推荐 644)
  1. WebSocket 连接失败:
  • 验证 Upgrade 头是否正确传递
  • 检查后端服务是否正常运行
  1. 性能优化:
nginx 复制代码
# 调整缓冲区设置
proxy_buffers 8 32k;
proxy_buffer_size 64k;
  1. 防火墙设置:
bash 复制代码
sudo ufw allow 443/tcp

配置完成后建议使用 SSL Labs 测试:https://www.ssllabs.com/ssltest/

相关推荐
春天花会开1311 小时前
内网 HTTPS 部署实战:私有 CA + Nginx 反向代理全流程复盘
运维·nginx·https
还是大剑师兰特5 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing6 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
2501_916007476 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
Lsetea6 天前
证书没到期却报certificate has expired:OpenSSL定位中间证书与系统时间
运维·https·ssl证书·openssl·证书链
PC2005-cloud6 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
2501_916008896 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
SDWAN_Cheap6 天前
HTTPS到底加密了什么?
https·数据加密
PHP实战开发录6 天前
Nginx上传大文件为什么报413
nginx·php·开发
2501_916008896 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview