支援 QUIC 和 HTTP/3
從原始碼建置 設定 範例設定 疑難排解 |
自 1.25.0 版本起,開始支援 QUIC 和 HTTP/3 通訊協定。此外,自 1.25.0 版本起,Linux 二進位套件也提供 QUIC 和 HTTP/3 的支援。
QUIC 和 HTTP/3 的支援屬於實驗性質,使用前請謹慎考慮。
從原始碼建置
建置是使用 configure
命令進行設定。請參考 從原始碼建置 nginx 以了解詳細資訊。
在設定 nginx 時,可以使用 --with-http_v3_module
設定參數來啟用 QUIC 和 HTTP/3。
建議使用提供 QUIC 支援的 SSL 程式庫來建置 nginx,例如 BoringSSL、LibreSSL 或 QuicTLS。否則,將使用不支援早期資料的 OpenSSL 相容層。
使用以下命令來設定使用 BoringSSL 的 nginx
./configure --with-debug --with-http_v3_module --with-cc-opt="-I../boringssl/include" --with-ld-opt="-L../boringssl/build/ssl -L../boringssl/build/crypto"
或者,可以使用 QuicTLS 設定 nginx
./configure --with-debug --with-http_v3_module --with-cc-opt="-I../quictls/build/include" --with-ld-opt="-L../quictls/build/lib"
或者,可以使用新版本的 LibreSSL 設定 nginx
./configure --with-debug --with-http_v3_module --with-cc-opt="-I../libressl/build/include" --with-ld-opt="-L../libressl/build/lib"
設定完成後,使用 make
編譯並安裝 nginx。
設定
在 ngx_http_core_module 模組中的 listen 指令新增了一個參數 quic,用於在指定的連接埠上啟用透過 QUIC 的 HTTP/3。
除了 quic
參數之外,也可以指定 reuseport 參數,使其能夠與多個工作程序正常運作。
有關指令的清單,請參閱 ngx_http_v3_module。
要啟用位址驗證
quic_retry on;
要啟用 0-RTT
ssl_early_data on;
要啟用 GSO(通用分段卸載)
quic_gso on;
要設定各種權杖的主機金鑰
quic_host_key <filename>;
QUIC 需要 TLSv1.3 通訊協定版本,該版本在 ssl_protocols 指令中預設為啟用。
預設情況下,特定於 Linux 的 GSO 優化已停用。如果對應的網路介面設定為支援 GSO,請啟用此選項。
範例設定
http { log_format quic '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$http3"'; access_log logs/access.log quic; server { # for better compatibility it's recommended # to use the same port for quic and https listen 8443 quic reuseport; listen 8443 ssl; ssl_certificate certs/example.com.crt; ssl_certificate_key certs/example.com.key; location / { # required for browsers to direct them to quic port add_header Alt-Svc 'h3=":8443"; ma=86400'; } } }
疑難排解
可能有助於識別問題的提示
- 確保 nginx 是使用適當的 SSL 程式庫建置的。
- 確保 nginx 在執行階段使用適當的 SSL 程式庫 (
nginx -V
會顯示目前使用的程式庫)。 - 確保用戶端實際上是透過 QUIC 發送請求。建議先從簡單的控制台用戶端(例如 ngtcp2)開始,以確保伺服器已正確設定,然後再嘗試使用對憑證可能相當挑剔的實際瀏覽器。
- 使用偵錯支援建置 nginx 並檢查偵錯記錄。其中應包含連線的所有詳細資訊以及失敗的原因。所有相關訊息都包含 “
quic
” 前綴,可以輕鬆篩選出來。 - 若要進行更深入的調查,可以使用以下巨集來啟用其他偵錯功能:
NGX_QUIC_DEBUG_PACKETS
、NGX_QUIC_DEBUG_FRAMES
、NGX_QUIC_DEBUG_ALLOC
、NGX_QUIC_DEBUG_CRYPTO
。./configure --with-http_v3_module --with-debug --with-cc-opt="-DNGX_QUIC_DEBUG_PACKETS -DNGX_QUIC_DEBUG_CRYPTO"