HTTPS 概述
HTTPS(HyperText Transfer Protocol Secure)是 HTTP 的安全版本,透過 SSL/TLS 協定對傳輸的資料進行加密,確保使用者與伺服器之間的通訊安全。在現代網路環境中,HTTPS 已成為網站的標準配置,不僅能保護使用者隱私,還能提升 SEO 排名和使用者信任度。
HTTPS 的主要優勢
- 資料加密:防止中間人攻擊,保護敏感資訊
- 身份驗證:確認網站的真實身份
- 資料完整性:確保傳輸過程中資料未被篡改
- SEO 加分:Google 將 HTTPS 列為排名因素之一
- 瀏覽器信任:避免「不安全」警告標示
取得 SSL 憑證(Let’s Encrypt)
Let’s Encrypt 提供免費、自動化的 SSL 憑證,是目前最受歡迎的憑證頒發機構之一。我們使用 Certbot 工具來申請和管理憑證。
安裝 Certbot
1
2
3
4
5
| # 更新套件清單
sudo apt update
# 安裝 Certbot 和 Nginx 插件
sudo apt install certbot python3-certbot-nginx -y
|
申請 SSL 憑證
1
2
3
4
5
| # 使用 Certbot 為 Nginx 申請憑證
sudo certbot --nginx -d example.com -d www.example.com
# 僅申請憑證,不自動修改 Nginx 設定
sudo certbot certonly --nginx -d example.com -d www.example.com
|
設定自動更新
Let’s Encrypt 憑證有效期為 90 天,需要定期更新:
1
2
3
4
5
| # 測試自動更新
sudo certbot renew --dry-run
# 查看 Certbot 的 systemd timer
sudo systemctl status certbot.timer
|
Nginx SSL 基本設定
取得憑證後,需要在 Nginx 中進行 SSL 設定。
基本 SSL 設定範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
# SSL 憑證設定
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# SSL 協定版本
ssl_protocols TLSv1.2 TLSv1.3;
# 網站根目錄
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
|
強制 HTTPS 重導向
為確保所有流量都使用 HTTPS,需要將 HTTP 請求重導向至 HTTPS。
HTTP 到 HTTPS 重導向設定
1
2
3
4
5
6
7
8
9
| # HTTP 重導向到 HTTPS
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# 301 永久重導向
return 301 https://$server_name$request_uri;
}
|
完整的雙伺服器區塊設定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # HTTP 重導向
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
# HTTPS 主伺服器
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
|
SSL 安全強化設定
基本的 SSL 設定可能存在安全漏洞,需要進行額外的強化。
禁用不安全的協定
1
2
3
4
5
| # 只允許 TLS 1.2 和 TLS 1.3
ssl_protocols TLSv1.2 TLSv1.3;
# 禁用 SSL 會話票據(Session Tickets)以提升前向保密性
ssl_session_tickets off;
|
DH 參數設定
產生更強的 Diffie-Hellman 參數:
1
2
| # 產生 4096 位元的 DH 參數(需要較長時間)
sudo openssl dhparam -out /etc/nginx/dhparam.pem 4096
|
在 Nginx 中設定:
1
| ssl_dhparam /etc/nginx/dhparam.pem;
|
現代加密套件設定
選擇安全的加密套件是 SSL 設定的重要環節。
推薦的加密套件設定
1
2
3
4
5
6
7
8
| # 現代加密套件設定
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
# 伺服器優先選擇加密套件
ssl_prefer_server_ciphers off;
# ECDH 曲線設定
ssl_ecdh_curve X25519:secp384r1;
|
SSL 會話快取設定
1
2
3
| # SSL 會話快取
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
|
HSTS 設定
HTTP Strict Transport Security(HSTS)強制瀏覽器只使用 HTTPS 連線。
基本 HSTS 設定
1
2
| # HSTS 設定(max-age 為一年)
add_header Strict-Transport-Security "max-age=31536000" always;
|
包含子網域的 HSTS 設定
1
2
| # 包含子網域和預載入
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
|
注意:啟用 HSTS preload 前,請確保網站已準備好長期使用 HTTPS,因為這是不可逆的設定。
OCSP Stapling 設定
OCSP Stapling 可以加速 SSL 握手過程,提升網站效能。
OCSP Stapling 設定
1
2
3
4
5
6
7
8
9
10
| # 啟用 OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
# 設定信任的 CA 憑證
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
# DNS 解析器
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
|
SSL Labs 評級優化
透過 SSL Labs 測試可以檢查 SSL 設定的安全性。以下是獲得 A+ 評級的完整設定。
完整的 A+ 評級設定範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| # /etc/nginx/sites-available/example.com
# HTTP 重導向
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
# HTTPS 主伺服器
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
# SSL 憑證
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.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:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_ecdh_curve X25519:secp384r1;
# DH 參數
ssl_dhparam /etc/nginx/dhparam.pem;
# SSL 會話設定
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# 安全標頭
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options DENY always;
add_header X-Content-Type-Options nosniff always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# 網站根目錄
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 隱藏 Nginx 版本
server_tokens off;
}
|
驗證設定並重新載入 Nginx
1
2
3
4
5
6
7
8
| # 測試 Nginx 設定語法
sudo nginx -t
# 重新載入 Nginx
sudo systemctl reload nginx
# 查看 Nginx 狀態
sudo systemctl status nginx
|
參考資料