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/

相关推荐
Lsetea5 小时前
OpenSSL指定-cipher仍协商TLS1.3:证书排查中的套件与协议边界
运维·https·ssl证书·openssl·tls
程序员夏洛10 小时前
TCP 是用来解决什么问题?
java·网络协议·http·https
老李IT笔记11 小时前
设备显示在线但指令没回执:三类静默失效的排查顺序
ios·iphone
xixingzhe21 天前
Nginx 看懂请求耗时瓶颈
服务器·前端·nginx
北风toto1 天前
Windows 下用 OpenSSL 生成 SSL 密钥并配置 Nginx 教程
windows·nginx·ssl
卜锦元1 天前
Docker Compose 部署 Nginx 1.30.6 完整指南
nginx·docker·容器
小小龙学IT1 天前
Nginx 开源 Web 服务器与反向代理深度解
前端·nginx·开源
青瓦梦滋1 天前
【在线五子棋对战】游戏匹配模块
网络·c++·网络协议·http·https
Coisinier2 天前
docker命令管理镜像及容器
linux·运维·nginx·docker·容器·操作系统
2301_32241428042 天前
基于web的校园失物招领管理系统59695-计算机毕设 原创(免费领源码+部署教程+开发环境)
mysql·nginx·php·apache·thinkphp·phpstudy·wampserver