在 Qualys SSL Labs SSL 測試中獲得 A+ 評級的秘技 2021 版
本系列文章將闡述主流應用交付控制器和主流 Web 伺服器如何運行 HTTP/2 和 TLSv1.3 協議,以及如何在 SSL Test 中獲得 A+ 評級。
請訪問原文鏈接://sysin.org/blog/get-a-plus-rating-on-ssl-test/,查看最新版。原創作品,轉載請保留出處。
作者:gc(at)sysin.org,主頁:www.sysin.org
2021 年 8 月發布的 Windows Server 2022 正式支援 QUIC 和 TLS 1.3 相關特性。至此,主流產品已經全部支援 TLSv1.3 協議。
0. 概述
Qualys SSL Labs 簡介
Qualys,Inc.(NASDAQ:QLYS)是雲安全和合規解決方案的先驅和領先提供商,在 100 多個國家擁有 6700 多個客戶,其中包括福布斯全球 100 強和財富 100 強中的大多數。QualysGuard 雲平台和集成解決方案套件通過按需提供關鍵的安全智慧並自動化 IT 系統和 web 應用程式的全方位審核、法規遵從性和保護,幫助組織簡化安全操作並降低合規成本。Qualys 成立於 1999 年,與英國電信、戴爾安全工程、富士通、IBM、NTT、Symantec、Verizon 和 Wipro 等領先的託管服務提供商和諮詢機構建立了戰略合作關係。該公司還是雲安全聯盟(CSA)的創始成員。
SSL Labs 推出的全球知名的 SSL 網站在線檢測工具,會對 HTTPS 網站的證書鏈、安全性、性能、協議細節進行全面檢測,檢測完畢後會進行打分,同時給出一份詳細的檢測報告和改進建議。
測試網站://www.ssllabs.com/ssltest/
測試規則概述
January 2020
主要是修改了 TLS 1.0 和 TLS 1.1 的評分標準,TLS 1.0 和 TLS 1.1 是分別於 1996 年和 2006 年發布的老版協議,使用的是弱加密演算法和系統。比如 SHA-1 和 MD5,這些演算法和系統十分脆弱,存在重大安全漏洞,容易受到降級攻擊的嚴重影響,而在 2008 年和 2017 年分別發布了協議的新版本,即 TLS 1.2 和 TLS 1.3,無疑更優於舊版本,使用起來也更安全。
2018 年,在春季 TLS 1.3 版本發布之後,蘋果、Google、Mozilla 和微軟四大瀏覽器製造商於 2018 年 10 月聯合宣布計劃在 2020 年初取消對 TLS 1.0 和 TLS 1.1 的支援。
主流瀏覽器客戶端都提供了禁用 TLS 1.0 和 TLS 1.1 協議的大致期限:
Browser Name | Date |
---|---|
Microsoft IE and Edge | First half of 2020 |
Mozilla Firefox | March 2020 |
Safari/Webkit | March 2020 |
Google Chrome | January 2020 |
備註:由於受 COVID-19 影響,瀏覽器廠商推遲了 TLS 1.0 和 1.1 版本協議的淘汰時間。
Existing Grades Sample
Server Configuration | Grade |
---|---|
TLS 1.2, TLS 1.1, TLS 1.0 + HSTS + No Warning + TLS_FALLBACK_SCSV | A+ |
TLS 1.2, TLS 1.1, TLS 1.0 + HSTS + No Warning + No support for TLS_FALLBACK_SCSV | A |
TLS 1.2, TLS 1.1, TLS 1.0 + HSTS + Warnings + No support for TLS_FALLBACK_SCSV | A- |
Future Grades Sample
Server Configuration | Grade |
---|---|
TLS 1.2, TLS 1.1, TLS 1.0 + HSTS + No Warning + TLS_FALLBACK_SCSV | B |
TLS 1.2, TLS 1.1, TLS 1.0 + HSTS + No Warning + No support for TLS_FALLBACK_SCSV | B |
TLS 1.2, TLS 1.1, TLS 1.0 + HSTS + Warnings + No support for TLS_FALLBACK_SCSV | B |
TLS 1.2 + HSTS + No Warning + TLS_FALLBACK_SCSV | A+ |
TLS 1.2 + HSTS + No Warning + No support for TLS_FALLBACK_SCSV | A |
TLS 1.2 + HSTS + Warnings + No support for TLS_FALLBACK_SCSV | A- |
References
- Modernizing TLS connections in Microsoft Edge and Internet Explorer 11 : //blogs.windows.com/msedgedev/2018/10/15/modernizing-tls-edge-ie11/
- Removing Old Versions of TLS : //blog.mozilla.org/security/2018/10/15/removing-old-versions-of-tls/
- Deprecation of Legacy TLS 1.0 and 1.1 Versions: //webkit.org/blog/8462/deprecation-of-legacy-tls-1-0-and-1-1-versions/
- Modernizing Transport Security: //security.googleblog.com/2018/10/modernizing-transport-security.html
- Recommendations for Secure Use of Transport Layer Security (TLS) and Datagram Transport Layer Security (DTLS): //tools.ietf.org/html/rfc7525
1. F5 BIG-IP
BIG-IP ® 系統是一組應用交付產品,它們協同工作以確保高可用性、改進的性能、應用安全和訪問控制。BIG-IP 系統的主要功能之一是將不同類型的協議和應用程式流量定向到適當的目標伺服器。系統通過其 Local Traffic Manager™ 模組實現這一點,該模組可以將流量直接轉發到負載平衡伺服器池,或將流量發送到下一跳路由器、路由器池或直接發送到網路上的選定節點。BIG-IP 系統上可用的其他模組提供關鍵功能,例如將安全策略應用於網路流量、加速 HTTP 連接以及優化廣域網中的連接。
F5 BIG-IP 默認 B 級別(本例基於當前最新的 BIG-IP 16.0.0)
TLS 1.2 + HSTS + No Warning + TLS_FALLBACK_SCSV = A+
(No Warning 即受信任 SSL 證書,TLS_FALLBACK_SCSV F5 默認支援)
故:A 級別 + 開啟 HSTS = A+,推薦啟用 TLSv1_3 和 HTTP/2
其他應用交付產品可以參照 F5 配置
1.1 Ciphers 配置:A 級別 (TLSv1.2)
根據 2020 年 1 月演算法變更,需要 TLSv1.2 及以上版本才能獲得 A,新的 A 級別如下:
ECDHE+AES-GCM:ECDHE+AES-GCM:ECDHE+AES:ECDHE+3DES:RSA+AES-GCM:RSA+AES:RSA+3DES:-MD5:-RC4:-SSLv3:-TLSv1:-TLSv1_1
或者:
ECDHE+AES-GCM:ECDHE+AES:ECDHE+3DES:RSA+AES-GCM:RSA+AES:RSA+3DES:-MD5:-RC4:-SSLv3:-TLSv1:-TLSv1_1
F5 Cipher TLS 版本寫法(與 nginx 和 apache 等使用 OpenSSL 的軟體略有不同):
TLSv1
TLSv1_1
TLSv1_2
TLSv1_3
執行步驟:
編輯 SSL Profile,修改 Ciphers,將默認 Default 替換上述內容。
1.2 啟用 HSTS
HSTS(HTTP Strict Transport Security,RFC6797),即 HTTP 嚴格安全傳輸,是國際互聯網工程組織 IETF 正在推行一種新的 Web 安全協議,網站採用 HSTS 後,用戶訪問時無需手動在地址欄中輸入 HTTPS,瀏覽器會自動採用 HTTPS 訪問網站地址,從而保證用戶始終訪問到網站的加密鏈接,保護數據傳輸安全。HSTS 的作用是強制客戶端(如瀏覽器)使用 HTTPS 與伺服器創建連接。伺服器開啟 HSTS 的方法是,當客戶端通過 HTTPS 發出請求時,在伺服器返回的超文本傳輸協議響應頭中包含 Strict-Transport-Security 欄位。
Preload List:讓防禦更加徹底
HSTS 存在一個比較薄弱的環節,那就是瀏覽器沒有當前網站的 HSTS 資訊的時候,或者第一次訪問網站的時候,依然需要一次明文的 HTTP 請求和重定向才能切換到 HTTPS,以及刷新 HSTS 資訊。而就是這麼一瞬間卻給攻擊者留下了可乘之機,使得他們可以把這一次的 HTTP 請求劫持下來,繼續中間人攻擊。針對這種攻擊,HSTS 也有應對辦法,那就是在瀏覽器里內置一個列表 Preload List,只要是在這個列表裡的域名,無論何時、何種情況,瀏覽器都只使用 HTTPS 發起連接。這個列表由 Google Chromium 維護,FireFox、Safari、IE 等主流瀏覽器均在使用。
可以通過官網(//hstspreload.org),查詢網站是否在 Preload List,可以申請將網站加入到 Preload List。
執行步驟:
v12 及以上版本直接在 TMUI 中 Enabling HSTS in the HTTP profile 或者使用 iRuels
v11 及以下可以只能通過 iRules 實現
### iRule for HSTS HTTP Virtuals ###
when HTTP_REQUEST {
HTTP::respond 301 Location "//[HTTP::host][HTTP::uri]"
}
### iRule for HSTS HTTPS Virtuals ###
## 31536000 sec = 1 Year
when HTTP_RESPONSE {
HTTP::header insert Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
}
OR
when HTTP_RESPONSE {
HTTP::header insert Strict-Transport-Security "max-age=31536000; includeSubDomains"
}
其中:
- max-age 是必選參數,是一個以秒為單位的數值,它代表著 HSTS Header 的過期時間,通常設置為 1 年,即 31536000 秒。
- includeSubDomains 是可選參數,如果包含它,則意味著當前域名及其子域名均開啟 HSTS 保護。
- preload 是可選參數,只有當你申請將自己的域名加入到瀏覽器內置列表的時候才需要使用到它。
1.3 啟用 TLSv1_3
F5 BIG-IP 當前 LTS 版本 14.1.0、15.1.0、16.1.0 及以上版本都可以完整支援 TLSv1_3。
BIG-IP v14 開始支援 TLSv1_3(In BIG-IP 14.0.0, the BIG-IP system adds limited support for Transport Layer Security (TLS) 1.3. Starting in BIG-IP 14.1.0.1 and later, this support was updated to provide production level support for TLS 1.3.)
默認沒有啟用:By default, TLS 1.3 is disabled. To enable TLS 1.3, you must remove the No TLSv1.3 option from the Enabled Options list in the Configuration utility for the Client SSL and Server SSL profiles
You can view a list of TLS 1.3 supported ciphers and groups using the following TMOS Shell (tmsh) commands:
-
To view the supported client-side ciphers, use the following command:
tmsh run util clientssl-ciphers TLSv1_3
-
To view the supported server-side ciphers, use the following command:
tmsh run util serverssl-ciphers TLSv1_3
配置啟用 TLSv1_3:
編輯 ClientSSL Profle:
Ciphers:選擇 Cipher Group,下拉選擇 f5-secure
Options:Options List…
Enabled Options,
Disable No TLSv1.3
添加,No TLSv1 和 TLSv1.1,保留默認的 「Don’t insert empty fragments」
1.4 配置 HTTP/2
不在贅述,詳見 官方文檔。
配置 HTTP/2 就是在啟用 HTTP profile 的 VS 上關聯一個 HTTP/2 profile,核心參數配置任然在原有的 HTTP profile 上。
特殊配置是 SSL profile 要取消勾選 Renegotiation,其他都可以使用默認配置。
1.5 TLS-FALLBACK-SCSV
TLS-FALLBACK-SCSV(TLS Fallback Signaling Cipher Suite Value)是 OpenSSL 的一種阻止協議降級攻擊的特性和機制。F5 BIG-IP,Nginx 和 Apache httpd 的 HTTPS 皆基於 OpenSSL 實現,符合要求的 OpenSSL 版本即可支援。
Poodle and TLS-FALLBACK-SCSV
SSLv3 allows exploiting of the POODLE bug. This is one more major reason to disable this.
Google have proposed an extension to SSL/TLS named TLS FALLBACK SCSV that seeks to prevent forced SSL downgrades. This is automatically enabled if you upgrade OpenSSL to the following versions:
- OpenSSL 1.0.1 has TLS FALLBACK SCSV in 1.0.1j and higher.
- OpenSSL 1.0.0 has TLS FALLBACK SCSV in 1.0.0o and higher.
- OpenSSL 0.9.8 has TLS FALLBACK SCSV in 0.9.8zc and higher.
TLS Fallback Signaling Cipher Suite Value (SCSV) for Preventing Protocol Downgrade Attacks。
2. Microsoft IIS
Windows® Server 的 Internet 資訊服務 (IIS) 是一種靈活、安全且可管理的 Web 伺服器,用於託管 Web 上的任何內容。從媒體流到 Web 應用程式,IIS 的可擴展和開放架構已準備好處理最苛刻的任務。
根據規則:TLS 1.2 + HSTS + No Warning + TLS_FALLBACK_SCSV = A+
理論上 Windows & IIS 不支援 TLS_FALLBACK_SCSV,所以無法 A+,但是 開啟 HSTS,並僅啟用 TLS1.2 可以獲得 A+ 得分,這樣就不存在協議降級風險。
Microsoft 的 SSL 基於 Schannel 實現,與 OpenSSL 無關(或稱 Microsoft TLS)。
Schannel is a Security Support Provider (SSP) that implements the Secure Sockets Layer (SSL) and Transport Layer Security (TLS) Internet standard authentication protocols.
The Security Support Provider Interface (SSPI) is an API used by Windows systems to perform security-related functions including authentication. The SSPI functions as a common interface to several SSPs, including the Schannel SSP.
2.1 IIS 獲得 A 級別
測試環境:IIS 10 on Windows Server 2019
執行 ps 腳本 或者使用 IIS Crypto,將獲得 A,這裡以 IIS Crypto 為例:
IIS Crypto:點擊 「Best Pratices」,Server Protocols 只勾選 TLS 1.2,Apply 並重啟生效。
僅啟用 TLS 1.2 將獲得 A,遠程桌面也可以正常連接,啟用 HSTS 將獲得 A+。
在早期的 Windows 版本中,僅啟用 TLS 1.2 遠程桌面將無法連接。
2.2 HSTS
分位兩種情況,舊版需要安裝 IIS 模組,新版(Windows Server 2019+)自帶 HSTS 選項。
-
In older versions of IIS (IIS 7.0 to 10.0 R1703) this requirement can only archived the simple way with an installation of HTTP Strict Transport Security IIS Module.
-
Microsoft added native HTTP Strict Transport Security (HSTS) Support to IIS 10.0 Version 1709
IIS 管理器 — 選擇站點 — 高級設置 — HSTS
2.3 TLSv1.3
2021 年 8 月發布的 Windows Server 2022 正式支援 HTTP/3、QUIC 和 TLS 1.3 相關特性。
在 Windows Server 2022 中的 IIS 新建一個站點將默認啟用 TLS 1.3 以及 QUIC,除非手動勾選禁用。
備註:IIS 10 & Windows Server 2019 以及之前版本無法支援,參看。
2.4 HTTP/2
版本要求:
HTTP2 requires Windows 2016 with IIS 10 or later.
配置方法:
首先配置好 SSL 證書並創建 HTTPS 站點,IIS 10 默認開啟 HTTP/2 協議,所以我們都不要額外去設置(可以禁用,新建站點時或者 “編輯網站” > “綁定…” 勾選 “禁用 HTTP/2″)。
在 Windows Server 2022 中的 IIS 新建一個站點將默認啟用 TLS 1.3 以及 QUIC(HTTP/3),除非手動勾選禁用。
3. Nginx
nginx [engine x] 是一個 HTTP 和反向代理伺服器,郵件代理伺服器,和一個通用的 TCP/UDP 代理伺服器,最初由 Igor Sysoev 編寫。在許多負載很重的俄羅斯網站上運行了很久,包括 Yandex,Mail.Ru,VK,和 Rambler。根據 Netcraft 的說法,nginx 服務或代理 22.36% 最繁忙的網站 (2021 年 11 月)。
以下是一些成功案例:Dropbox,Netflix,WordPress.com,FastMail.FM。
3.1 Nginx SSL 證書配置方法
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
...
}
證書文件使用 PEM 格式。
證書文件使用相對路徑時,證書文件要放在主配置文件相同目錄下(默認 /etc/nginx)。
更多配置,這篇文章可以參考:Strong SSL Security on nginx
3.2 TLSv1.3
版本支援如下:
- The
TLSv1.1
andTLSv1.2
parameters (1.1.13, 1.0.12) work only when OpenSSL 1.0.1 or higher is used. - The
TLSv1.3
parameter (1.13.0) works only when OpenSSL 1.1.1 built with TLSv1.3 support is used.
More info on the NGINX documentation
3.3 HTTP/2
版本要求:
openssl 的版本必須在 1.0.2e 及以上,執行以下命令驗證:
openssl version
nginx 的版本必須在 1.9.5 以上,需要添加 –with-http_v2_module 模組,執行以下命令驗證:
nginx -V
3.4 參考配置
以下配置:啟用 HTTP/2、TLSv1.3、推薦的 Ciphers、HSTS,可以獲得 A+ 得分
server {
#listen 443 ssl;
listen 443 ssl http2; # HTTP/2 Enable
listen [::]:443 ssl http2; # IPv6
server_name www.sysin.org;
ssl_certificate www.sysin.org.crt;
ssl_certificate_key www.sysin.org.key;
ssl_protocols TLSv1.2 TLSv1.3; # Requires nginx >= 1.13.0 else use TLSv1.2
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload" always;
...
}
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
#ssl_ciphers HIGH:!aNULL:!MD5; #B
#ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA; #A
#ssl_ciphers ALL:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4; #B
#ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384; #A (Mozilla Intermediate)
4. Kubernetes ingress-nginx
4.1 概述
默認 TLS 版本和 Ciphers
nginx-ingress 默認僅使用 TLS 1.2 和 1.3,with a secure set of TLS ciphers.
默認 cipher 列表: 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
.
Legacy TLS 支援
如果需要兼容一些老舊的瀏覽器和作業系統,需要使用 ConfigMap 修改默認配置,例如:
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
data:
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:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA"
ssl-protocols: "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3"
HSTS 默認啟用
可以在 ConfigMap 中配置參數 hsts: "false"
禁用默認行為
HTTP redirect
對於 TLS 類型的 ingress,控制器默認將 HTTP 請求定向到 HTTPS(308 Permanent Redirect response),
可以在 NGINX config map,中使用 ssl-redirect: "false"
參數全局禁用,或者針對單個 ingress 規則使用 annotation nginx.ingress.kubernetes.io/ssl-redirect: "false"
來禁用。
本例部署的 ingress-nginx 版本為 0.30.0,經過測試只要使用受信任證書,即可獲得 A+ 得分。
4.2 示例
以下發布 Dashboard 為例,配置受信任 SSL 證書。
查看 Dashboard 已經正常部署(部署 Dashboard 參看其他文檔):
kubectl get po,svc -n kubernetes-dashboard -o wide
部署受信任的 SSL 證書:
# 創建 secret,在 ingress 不能直接使用證書需要轉換為 secret 才能使用
# key 和 cert 都為 PEM 格式,cert 包含證書文件和證書鏈部分
kubectl create secret tls dashboard-ingress-tls --key dashboard-ingress.key --cert dashboard-ingress.crt -n kubernetes-dashboard
# 查看 secret 內容
kubectl get secret dashboard-ingress-tls -n kubernetes-dashboard -o yaml
# 刪除命令
kubectl delete secret dashboard-ingress-tls -n kubernetes-dashboard
配置 ingress 轉發文件
# vi dashboard-ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
name: kubernetes-dashboard-ingress
namespace: kubernetes-dashboard #注意修改
spec:
tls:
- secretName: dashboard-ingress-tls #上述創建的 secret
rules:
- host: k8s.sysin.cn #域名
http:
paths:
- path: /
backend:
serviceName: kubernetes-dashboard
servicePort: 443
host: 對應的域名
path: url 上下文
backend: 後向轉發到對應的 serviceName: 和 servicePort:
注意,dashboard 默認使用 https 提供服務,ingress 默認 backend-protocol 使用 http,這裡發布成功的關鍵是要添加 annotations 參數
annotations:
nginx.ingress.kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
部署:
kubectl apply -f dashboard-ingress.yaml
部署成功後可以通過域名訪問://k8s.sysin.cn
5. Apache httpd
Apache HTTP Server 項目致力於開發和維護一個用於現代作業系統的開源 HTTP 伺服器,包括 UNIX 和 Windows。該項目的目標是提供一個安全、高效和提供與當前 HTTP 同步的 HTTP 服務的可擴展伺服器標準。
Apache HTTP 伺服器(「httpd」)於 1995 年推出,自 1995 年以來一直是 Internet 上最受歡迎的 Web 伺服器。1996 年 4 月。它在 2020 年 2 月慶祝了它作為一個項目的 25 歲生日。
5.1 基本配置
參看 官網文檔
LoadModule ssl_module modules/mod_ssl.so
Listen 443
<VirtualHost *:443>
ServerName www.example.com
SSLEngine on
SSLCertificateFile "/path/to/www.example.com.cert"
SSLCertificateKeyFile "/path/to/www.example.com.key"
SSLCipherSuite HIGH:!aNULL:!MD5
#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:!aNULL:!MD5
</VirtualHost>
5.2 HTTP/2
Apache Module mod_http2 Available in version 2.4.17 and later
兩種配置:
HTTP/2 in a VirtualHost context (TLS only)
Protocols h2 http/1.1
Allows HTTP/2 negotiation (h2) via TLS ALPN in a secure <VirtualHost>
. HTTP/2 preamble checking (Direct mode, see H2Direct
) is disabled by default for h2
.
HTTP/2 in a Server context (TLS and cleartext)
Protocols h2 h2c http/1.1
Allows HTTP/2 negotiation (h2) via TLS ALPN for secure <VirtualHost>
. Allows HTTP/2 cleartext negotiation (h2c) upgrading from an initial HTTP/1.1 connection or via HTTP/2 preamble checking (Direct mode, see H2Direct
).
5.3 HSTS
# Load modules (or use the IfModule)
LoadModule headers_module modules/mod_headers.so
LoadModule rewrite_module modules/mod_rewrite.so
Rewrite HTTP connections and redirect them to HTTPS:
# Redirect HTTP connections to HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) //%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Now configure the virtual host:
<VirtualHost *:443>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</VirtualHost>
5.4 TLSv1.3
版本要求:
Apache version 2.4.36
or greater. (網上文章傳言 2.4.38 是錯誤的!)
OpenSSL version 1.1.1
or greater.
CentOS 8 和 Ubuntu 20.04 自帶軟體包滿足要求,低版本需要編譯安裝。
[root@c8 ~]# openssl version
OpenSSL 1.1.1c FIPS 28 May 2019
[root@c8 ~]# dnf list httpd
Installed Packages
httpd.x86_64 2.4.37-21.module_el8.2.0+382+15b0afa8 @AppStream
root@u20:~# openssl version
OpenSSL 1.1.1f 31 Mar 2020
root@u20:~# apt list apache2
Listing... Done
apache2/focal 2.4.41-4ubuntu3 amd64
僅啟用 TLS 1.2:
SSLProtocol -all +TLSv1.2
配置項如下所示:
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol -all +TLSv1.2
SSLCertificateFile "/path/to/www.example.com.cert"
SSLCertificateKeyFile "/path/to/www.example.com.key"
</VirtualHost>
啟用 TLS 1.3 和 1.2:
The Apache version 2.4.36 or higher versions support TLS v1.3. You must upgrade Apache packages before enabled TLS 1.3 in SSL settings.
SSLProtocol -all +TLSv1.2 +TLSv1.3
配置項如下所示:
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCertificateFile "/path/to/www.example.com.cert"
SSLCertificateKeyFile "/path/to/www.example.com.key"
</VirtualHost>
記得重啟 Apache 服務才能生效。
5.5 參考配置
# For CentOS
yum install mod_ssl
mv /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.bak
配置文件自動增加 “/etc/httpd/conf.modules.d/00-ssl.conf”
echo '
Listen 443
<VirtualHost *:443>
Protocols h2 http/1.1
ServerName sysin.org
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCertificateFile "/etc/httpd/ssl/sysin.org.pem"
SSLCertificateKeyFile "/etc/httpd/ssl/sysin.org.key"
SSLCipherSuite HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</VirtualHost>
' > /etc/httpd/conf.d/sysin.org.conf
其他 Cipher 參考配置
SSLCipherSuite HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA #A
SSLCipherSuite ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384 #A (Mozilla Intermediate)
6. Apache Tomcat
Apache Tomcat® 軟體是 Jakarta Servlet、Jakarta Server Pages、Jakarta Expression Language、Jakarta WebSocket、Jakarta Annotations 和 Jakarta Authentication 規範的開源實現。這些規範是 Jakarta EE 平台的一部分。Jakarta EE 平台是 Java EE 平台的演變。Tomcat 10 及更高版本實現了作為 Jakarta EE 一部分開發的規範。Tomcat 9 及更早版本實現了作為 Java EE 一部分開發的規範。
Tomcat 不使用 OpenSSL,所以不支援 TLS-FALLBACK-SCSV 特性,在 Tomcat 9 最新版默認配置即可獲得 A 得分,開啟 HSTS 也是 A 得分。
6.1 基本配置
打開 conf/server.xml 文件可以看到默認的 SSL/TLS HTTP/1.1 和 HTTP/2 配置方法如下:
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
-->
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
-->
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
6.2 HTTP/2
版本要求:Tomcat 8.5.0,2016-03-24,開始支援 HTTP/2
配置 HTTP/2:即增加 <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
根據官方文檔使用 PEM 格式證書測試失敗,這裡使用 PFX 格式。
Tomcat 9 強制要求證書別名設置為 tomcat。您需要使用以下 keytool 命令(這裡的證書原來別名是 alias,阿里雲申請的免費證書默認別名)轉換證書別名為 tomcat:
keytool -changealias -keystore my-cert.pfx -alias alias -destalias tomcat
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 443 with HTTP/2
-->
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig protocols="TLSv1.2+TLSv1.3"
ciphers="HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA" >
<Certificate certificateKeystoreFile="conf/k8s.sysin.cn.pfx"
certificateKeystoreType="PKCS12"
certificateKeystorePassword="your-pfx-password" />
</SSLHostConfig>
</Connector>
<!-- 默認參數如下:
hostName= default:`_default_`
ciphers= default:`HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA`
protocols= default:`all`
-->
ciphers 參看:Ciphers,HowTo SSLCiphers
protocols 寫法:
The names of the protocols to support when communicating with clients. This should be a list of any combination of the following:
SSLv2Hello
SSLv3
TLSv1
TLSv1.1
TLSv1.2
TLSv1.3
all
列表中的每個標記都可以以加號(「+」)或減號(「-」)為前綴。加號添加協議,減號將其從當前列表中刪除。該列表是從一個空列表開始構建的。
字元 all 是 SSLv2Hello,TLSv1,TLSv1.1,TLSv1.2,TLSv1.3 的別名。
請注意,只有在使用實現 TLSv1.3 的 JVM 時,JSSE 才支援 TLSv1.3。
請注意,對於基於 OpenSSL 的安全連接器,將忽略 SSLv2Hello。如果為基於 OpenSSL 的安全連接器指定了多個協議,它將始終支援 SSLv2Hello。如果指定了單個協議,它將不支援 SSLv2Hello。
請注意,SSLv2 和 SSLv3 本質上是不安全的。
如果未指定,則將使用 all 的默認值。
6.3 HSTS
Response Header 配置
Enabling HSTS (to include maxAgeSeconds = 31536000, includeSubDomains, and preload) requires two modifications of the Tomcat』s conf/web.xml
file:
1). 啟用 HSTS 支援,查找以下部分(通過搜索 「httpHeaderSecurity」 關鍵詞):
<!--
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<async-supported>true</async-supported>
</filter>
-->
替換為(或者新增):
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<init-param>
<param-name>hstsEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>hstsMaxAgeSeconds</param-name>
<param-value>31536000</param-value>
</init-param>
<init-param>
<param-name>hstsIncludeSubDomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>hstsPreload</param-name>
<param-value>true</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>
2). 繼續搜索 「httpHeaderSecurity」 關鍵詞,查找如下內容,在 「Built In Filter Mappings」 這一段:
<!--
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
-->
移除注釋,如下:
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
6.4 HTTP redirection
編輯 server.xml
,將 HTTP 重定向到 HTTPS,這裡分別使用 80 和 443 埠,搜索 「Connector」 關鍵詞,查找到如下部分:
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
修改為:
<Connector executor="tomcatThreadPool"
port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
或者修改這裡:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改為:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
7. HAProxy
HAProxy 是一種免費、快速且可靠的反向代理,可為基於 TCP 和 HTTP 的應用程式提供高可用性、負載平衡和代理。它特別適用於流量非常高的網站,並為世界上大部分訪問量最大的網站提供支援。多年來,它已成為事實上的標準開源負載均衡器,現在隨大多數主流 Linux 發行版一起提供,並且通常默認部署在雲平台中。由於它不做廣告,我們只知道它在管理員報告時被使用:-)
HAProxy 核心團隊並行維護多個版本。從 1.8 版開始,每年都會發布兩個主要版本。第一個數字通常表示重大更改(配置格式等),但實際上很少更改。第二個數字表示新功能。兩者構成一個分支。這些數字後面會出現一個額外的數字,以指示錯誤修復版本。
偶數的分支稱為 「LTS」(用於 「長期支援」),並且在發布後維護 5 年的區域。在此期間,他們將收到針對發布後發現的錯誤的修復程式。這些分支針對的是尋求極端穩定性並且不想過於頻繁地驗證新版本但仍希望收到修復程式的一般用戶。
奇數分支僅被稱為 「穩定」,它們針對那些喜歡經常升級以從現代功能中受益的高技能用戶,並且在出現問題時也能夠回滾。這些版本的維護期為 12 到 18 個月。
7.1 SSL cipher
Global 參數中關於 SSL 的配置
以下三個參數是定義 front:
ssl-default-bind-ciphers <ciphers> #適用於 TLSv1.2 及以下版本
ssl-default-bind-ciphersuites <ciphersuites> #OpenSSL 1.1.1 or later,TLSv1.3
同時需要同時支援 TLSv1.2 及以下版本和 TLSv1.3 兩個參數需要同時設置。
示例:
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
ssl-default-server-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
以下對應定義 backend server:
適用於 TLSv1.2 及以下版本
適用於 OpenSSL 1.1.1 or later,TLSv1.3
參看官方文檔:Configuring TLS Settings
參考配置:
global
......
#ssl-default-bind-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 #for TLSv1.2, Mozilla Intermediate
ssl-default-bind-ciphers HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!kRSA #for TLSv1.2
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256 #for TLSv1.3
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
#ssl-default-bind-options prefer-client-ciphers no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
.....
7.2 HTTP/2
HAProxy 1.8 及以上版本支援 HTTP/2
From the 1.8 announcement:
HAProxy 1.8 now supports HTTP/2 on the client side (in the frontend sections) and can act as a gateway between HTTP/2 clients and your HTTP/1.1 and HTTP/1.0 applications.
HTTP/2 協議已經被迅速採用,HAProxy 1.8 現在在客戶端支援 HTTP/2(在前端部分),並且可以充當 HTTP/2 客戶端與 HTTP/1.1 和 HTTP/1.0 應用程式之間的網關。
要啟用對 HTTP/2 的支援,前端部分的綁定行必須配置為 SSL 端點,alpn 必須宣布 h2,如下:
frontend myapp
bind :443 ssl crt /path/to/cert.crt alpn h2,http/1.1
mode http
備註:cert.crt 證書採用 PEM 格式,包含私鑰、證書,證書鏈在一個文件中。
7.3 TLSv1.3
要求:HAProxy 1.8.1 及以上,OpenSSL 1.1.1 及以上。
參數:ssl-default-bind-ciphersuites,參看上述 SSL cipher 部分的描述。
7.4 HSTS
方法如下:
frontend public
......
http-response add-header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
......
Rewriting HTTP responses 方法示例
- add-header
語法:
http-response add-header <name> <fmt> [<condition>]
示例:
http-response add-header X-Via %[env(HOSTNAME)]
- set-header
相當於覆蓋或者替換原有 header
語法:
http-response set-header <name> <fmt> [<condition>]
示例:
http-response set-header Server webserver #hide server header
- del-header
語法:
http-response del-header <name> [<condition>]
示例:
http-response del-header X-Varnish
- replace-value
高級替換
語法:
http-response replace-value <name> <match-regex> <replace-fmt> [<condition>]
7.5 Redirecting HTTP Requests
重定向 HTTP 請求到 HTTPS,可以參考以下官方示例:
Examples of traffic redirection:
Append a www. prefix in front of all URLs that do not have it:
acl has_www hdr_beg(host) -i www
http-request redirect code 301 location //www.%[hdr(host)]%[req.uri] unless has_www
Redirect all HTTP traffic to HTTPS when SSL is handled by haproxy:
acl http ssl_fc,not
http-request redirect scheme https if http
Send redirects for requests for articles without a ‘/’:
acl missing_slash path_reg ^/article/[^/]*$
http-request redirect code 301 prefix / drop-query append-slash if missing_slash
Move the login URL only to HTTPS:
acl http ssl_fc,not
acl https ssl_fc
acl u_login path_beg /login
acl u_logout path_beg /logout
acl up_userid urlp_len(userid) gt 0
acl cookie_set hdr_sub(cookie) SEEN=1
http-request redirect scheme https if http u_login
http-request redirect prefix //%[req.hdr(Host)] set-cookie SEEN=1 if !cookie_set
http-request redirect prefix //%[req.hdr(Host)] drop-query if u_login !up_userid
http-request redirect scheme http if https !u_login
http-request redirect location / clear-cookie USERID= if u_logout
7.6 參考配置
Config files and scripts for HAProxy 1.8 with HTTP/2 and dynamic reconfiguration
An ‘haproxy.cfg’ with:
- The ability to switch backends dynamically
- HTTP/2 support in all browsers
- Logging to systemd
- The various www vs non-www, HTTP vs HTTPS combinations redirected to a single HTTPS site.
- A branded ‘sorry’ page
- A separate server that handles blogs and marketing content
- Support for HTML5 Server Sent Events
- An A+ on the SSL Labs test
See the full docs at fast EV HTTPS verification provider CertSimple.
##//github.com/certsimple/haproxy-http2-load-balancing-config/blob/master/haproxy.cfg
global
# Log to systemd's /dev/log compatibility socket
log /dev/log local0 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket - see //cbonte.github.io/haproxy-dconv/1.8/configuration.html#3.1-stats%20socket
stats socket /var/lib/haproxy/stats mode 600 level admin
stats timeout 2m
# generated 2020-09-12, Mozilla Guideline v5.6, HAProxy 2.1, OpenSSL 1.1.1d, intermediate configuration
# //ssl-config.mozilla.org/#server=haproxy&version=2.1&config=intermediate&openssl=1.1.1d&guideline=5.6
# intermediate configuration
ssl-default-bind-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-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-bind-options prefer-client-ciphers no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
#ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
ssl-default-server-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-default-server-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-server-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
# curl //ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparam
ssl-dh-param-file /path/to/dhparam
defaults
mode http
# Needed for GeoIP
# Enable insertion of the X-Forwarded-For header to requests sent to servers
# May be used in sections :defaults frontend listen backend
# //cbonte.github.io/haproxy-dconv/2.2/configuration.html#4.2-option%20forwardfor
option forwardfor
# See //cbonte.github.io/haproxy-dconv/1.8/configuration.html#option%20http-server-close
option http-server-close
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 3000
# From //stackoverflow.com/questions/21419859/configuring-haproxy-to-work-with-server-sent-events
# Set the max time to wait for a connection attempt to a server to succeed
timeout connect 30s
# handle a client suddenly disappearing from the net
timeout client-fin 30s
option http-server-close
# The 'stats' site, where we see what's up and what's down - uncomment and set a password if you want it!
# stats enable
# stats uri /haproxy?stats
# stats realm Strictly\ Private
# stats auth someusername:i-am-an-awful-password-and-you-should-change-me
# Show a custom page during site maintenance (ie, when 'blue' and 'green' are both down)
errorfile 503 /etc/haproxy/errors/503-mycustom.http
frontend public
# HTTP/2 - see //www.haproxy.com/blog/whats-new-haproxy-1-8/
# h2 is HTTP2 with TLS - see //http2.github.io/faq/
# Order matters, so h2 before http1.1
bind :443 ssl crt /etc/https/cert-and-private-key-and-intermediate-and-dhparam.pem alpn h2,http/1.1
# Redirect http -> https
bind :80
redirect scheme https code 301 if ! {ssl_fc}
# Redirect www -> example.com
redirect prefix //example.com code 301 if {hdr(host) -i www.example.com }
# HSTS (15768000 seconds = 6 months)
# HSTS (31536000 seconds = 1 years)
http-response set-header Strict-Transport-Security max-age=31536000
# Use the marketing site for marketing URLs
acl marketing path_beg -i /help /sitemap.xml /BingSiteAuth.xml /about /blog /videos/blog /images/blog /fonts/blog /css/blog /js/blog
use_backend marketing if marketing
# Everything else goes to the app servers
default_backend app
option httpclose
option forwardfor
backend marketing
balance roundrobin
server static 127.0.0.1:8000 check
backend app
# From //blog.haproxy.com/2014/01/17/emulating-activepassing-application-clustering-with-haproxy/
# See also //cbonte.github.io/haproxy-dconv/configuration-1.5.html#stick-table
stick-table type ip size 1m
stick on dst
# See //cloud.digitalocean.com/droplets for these IPs
server green 10.1.1.1:8000 check
server blue 10.1.1.2:8000 check backup
# //stackoverflow.com/questions/21419859/configuring-haproxy-to-work-with-server-sent-events
timeout tunnel 10h
8. Varnish with Hitch
筆者寫了一篇文章描述了整個配置過程,訪問這裡查看:Varnish with Hitch HTTP/2 implement on CentOS 8.0
Varnish Cache 是一種 web 應用程式加速器,同時以被用於快取的 HTTP 反向代理而聞名。Varnish HTTP/2 前端通過 Hitch 代理實現。Hitch 是 Varnish Software 開發的基於 libev 的高性能 SSL/TLS 開源代理軟體。
8.1 啟動 Varnish 支援 HTTP/2
默認情況下,Varnish 中的 HTTP/2 支援是禁用的,因此必須添加一個特性標誌才能啟用它。即通過傳遞 「-p feature=+http2」 作為 Varnish 的啟動參數來實現。
您可以通過運行 varnishadm param.show feature
命令來檢查是否已啟用參數。
# varnishadm param.show feature
feature
Value is: none (default)
Enable/Disable various minor features.
none Disable all features.
Use +/- prefix to enable/disable individual feature:
short_panic Short panic message.
wait_silo Wait for persistent silo.
no_coredump No coredumps.
esi_ignore_https Treat HTTPS as HTTP in
ESI:includes
esi_disable_xml_check Don't check of body looks like
XML
esi_ignore_other_elements Ignore non-esi XML-elements
esi_remove_bom Remove UTF-8 BOM
https_scheme Also split https URIs
http2 Support HTTP/2 protocol
http_date_postel Relax parsing of timestamps in
HTTP headers
啟動 Varnish
本例中,Varnish 使用默認配置,事先運行了 Nginx,將 Nginx 默認埠修改為 8080 即可(具體過程略)。
varnishd -a :80 -a localhost:6086,PROXY -p feature=+http2 -f /etc/varnish/default.vcl
#或者
varnishd -a localhost:6086,PROXY -p feature=+http2 -f /etc/varnish/default.vcl
驗證 Varnish 已經開啟 HTTP/2 支援
varnishadm param.show feature
feature
Value is: +http2
Default is: none
......
8.2 hitch 參考配置:Ciphers,HTTP/2 和 TLSv1.3
版本要求:
- Cache 5.0 開始實驗性的支援 HTTP/2
- Varnish 6.0 完整支援 HTTP/2
- hitch 1.5 版本開始支援 TLS 1.3
示例配置(更多參數參看官方文檔):
mv /etc/hitch/hitch.conf /etc/hitch/hitch.conf.bak
echo '
# Run 'man hitch.conf' for a description of all options.
frontend = {
host = "*"
port = "443"
}
backend = "[127.0.0.1]:6086" # 6086 is the default Varnish PROXY port.
workers = 4 # number of CPU cores
daemon = on
# We strongly recommend you create a separate non-privileged hitch
# user and group
user = "hitch"
group = "hitch"
# Enable to let clients negotiate HTTP/2 with ALPN. (default off)
# Varnish 啟動參數必須增加 `-p feature=+http2`,開啟 HTTP/2 特性(默認關閉)
alpn-protos = "h2, http/1.1"
# run Varnish as backend over PROXY; varnishd -a :80 -a localhost:6086,PROXY ..
write-proxy-v2 = on # Write PROXY header
syslog = on
log-level = 1
# Add pem files to this directory
#pem-dir = "/etc/pki/tls/private"
## PEM 文件包含 key、cert 和 chain 的組合,可以支援多個 PEM 文件
## cat example.com.key example.com.crt my-ca-bundle.crt > example.com.pem
pem-file = "/etc/hitch/varnish.pem"
# 定義第二個 PEM 文件
#pem-file = "/etc/hitch/mydomain.pem"
## 官方推薦默認 cipher
ciphers = "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"
## Hitch supports TLS (1.0, 1.1, 1.2, 1.3) and SSL 3. By default
tls-protos = TLSv1.2 TLSv1.3
## TCP Fast Open saves up to one full round-trip time (RTT) over the standard three-way connection handshake during a TCP session.
tcp-fastopen = on
' > /etc/hitch/hitch.conf
OCSP staple 相關配置參看 官方文檔
以上配置將獲得 A 評級,加上下面的 HSTS 即可獲得 A+ 評級。
8.3 HSTS
編輯 varnish vcl,如下欄位添加:
sub vcl_deliver {
set resp.http.Strict-Transport-Security = "max-age=31536000; includeSubDomains; preload";
}