Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Nginx 配置 HTTPS 全流程:SSL 模塊編譯、證書生成與虛擬主機配置

編輯:Linux 教學

前置條件

編譯 Nginx 時指定 --with-http_ssl_module,依賴 OpenSSL 庫及開發包(通常為 libssl-dev)。

生成自簽名證書(測試用)

cd /usr/local/nginx/conf

# 1. 生成私鑰(需輸入密碼)
openssl genrsa -des3 -out server.key 1024

# 2. 生成簽名請求 CSR
openssl req -new -key server.key -out server.csr

# 3. 去除私鑰密碼(Nginx 啟動時不需交互輸入)
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key

# 4. 自簽名證書,有效期 365 天
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Nginx SSL 虛擬主機配置

server {
    server_name YOUR_DOMAINNAME_HERE;
    listen 443 ssl;
    ssl_certificate /usr/local/nginx/conf/server.crt;
    ssl_certificate_key /usr/local/nginx/conf/server.key;
    # 建議添加的安全優化
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
}

HTTP 80 端口強制跳轉 HTTPS

server {
    listen 80;
    server_name www.yourdomain.com;
    rewrite ^(.*) https://$server_name$1 permanent;
}

生產環境建議

  • 使用 CA 簽發的正規證書(Let's Encrypt 免費、自動續期)。
  • 啟用 HSTS:add_header Strict-Transport-Security "max-age=31536000";
  • 配置 OCSP Stapling 提升握手性能。
  • 定期檢查 SSL Labs 評分,優化協議與加密套件。
Copyright © Windows教程網 All Rights Reserved