Nginx 配置 HTTPS 與 WSS 完整指南
本教程將手把手教你如何為網站配置 HTTPS 加密訪問,并通過反向代理實現安全的 WebSocket(WSS)通信。以 https://www.zhegepai.cn 域名為例,完整流程約需 30 分鐘完成。
一、前置準備
1.1 域名注冊(示例)
? 已注冊域名:www.zhegepai.cn
? 確認域名已解析到服務器 IP
? 推薦 DNS 檢查工具:dig www.zhegepai.cn
或 DNS Checker
1.2 SSL 證書獲取
? 免費證書:推薦使用 Let’s Encrypt 通過 Certbot 工具自動簽發
? 商業證書:阿里云/騰訊云等平臺購買(示例使用)
? 最終需要兩個文件:
? 證書鏈文件:www.zhegepai.cn.pem
? 私鑰文件:www.zhegepai.cn.key
二、Nginx 核心配置
2.1 文件結構準備
# 創建證書存放目錄
sudo mkdir -p /etc/nginx/ssl/
# 上傳證書文件到指定位置
sudo cp www.zhegepai.cn.pem /etc/nginx/ssl/www.zhegepai.cn.pem
sudo cp www.zhegepai.cn.key /etc/nginx/ssl/www.zhegepai.cn.key
2.2 主配置文件 /etc/nginx/nginx.conf
# 用戶權限配置
user nginx;
worker_processes auto;
pid /run/nginx.pid;# 動態模塊加載
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {# 基礎日志格式log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;# 性能優化參數sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;# HTTPS 服務器配置server {listen 443 ssl http2;server_name www.zhegepai.cn;root /var/www/EasyTools;# SSL 證書配置ssl_certificate /etc/nginx/ssl/www.zhegepai.cn.pem;ssl_certificate_key /etc/nginx/ssl/www.zhegepai.cn.key;# 安全協議配置ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305;ssl_prefer_server_ciphers on;ssl_session_cache shared:SSL:10m;ssl_session_timeout 10m;# WebSocket 代理配置location /ws {proxy_pass http://127.0.0.1:8765;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_set_header Host $host;proxy_read_timeout 86400s;}# 安全響應頭add_header Strict-Transport-Security "max-age=31536000" always;add_header X-Content-Type-Options "nosniff" always;add_header X-Frame-Options "SAMEORIGIN" always;}# HTTP 重定向配置server {listen 80;server_name www.zhegepai.cn;return 301 https://$host$request_uri;}
}
三、關鍵配置解析
3.1 HTTPS 強化配置
參數 | 作用說明 | 推薦值 |
---|---|---|
ssl_protocols | 允許的 TLS 協議版本 | TLSv1.2 TLSv1.3 |
ssl_ciphers | 加密套件白名單 | ECDHE 系列優先 |
ssl_session_cache | 會話緩存提升性能 | shared:SSL:10m |
add_header | 添加 HSTS 等安全響應頭 | 根據業務需求調整 |
3.2 WebSocket 反向代理
location /ws {proxy_pass http://localhost:8765; # 本地 WS 服務端口proxy_http_version 1.1; # 必須使用 HTTP/1.1proxy_set_header Upgrade $http_upgrade; # 協議升級proxy_set_header Connection "upgrade"; # 保持長連接proxy_read_timeout 86400s; # 24小時超時防止斷開
}
四、部署與測試
4.1 服務端操作
# 語法檢查
sudo nginx -t# 重載配置
sudo systemctl reload nginx# 查看運行狀態
sudo systemctl status nginx
4.2 客戶端測試
Https測試,瀏覽器打開:https://www.zhegepai.cn
WSS測試腳本,創建 test-wss.html
:
<!DOCTYPE html>
<html>
<head><title>WSS 連接測試</title>
</head>
<body><h1 id="status">測試中...</h1><script>const socket = new WebSocket('wss://www.zhegepai.cn/ws');socket.onopen = () => {document.getElementById('status').textContent = '[成功] 連接已建立: ' + socket.url;};socket.onerror = (error) => {document.getElementById('status').textContent = '[失敗] 連接錯誤: ' + error.type;};</script>
</body>
</html>
本地雙擊瀏覽器直接打開測試:
—
五、常見問題排查
5.1 證書相關錯誤
# 檢查證書路徑權限
ls -l /etc/nginx/ssl/
# 應顯示 -rw-r--r-- 權限# 驗證證書鏈完整性
openssl verify -CAfile www.zhegepai.cn.pem www.zhegepai.cn.pem
5.2 WebSocket 連接失敗
# 在 location /ws 中添加調試日志
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log /var/log/nginx/ws-access.log main;
5.3 性能優化建議
- 啟用 OCSP Stapling 減少 SSL 握手時間
- 配置 SSL 會話票據(tickets)提升復用率
- 使用
nginx -V
確認編譯時啟用了 HTTP/2 模塊
通過本文的配置,您的網站將獲得:
? 全站 HTTPS 加密傳輸
? 安全的 WebSocket 通信
? A+ 等級的 SSL Labs 測試評分
? 防御常見 Web 攻擊的能力
實際部署時請根據業務需求調整超時時間、CSP 策略等參數。建議每 3 個月檢查 SSL 證書有效期,保持服務安全穩定。