支援 QUIC 和 HTTP/3

從原始碼建置
設定
範例設定
疑難排解

自 1.25.0 版本起,開始支援 QUICHTTP/3 通訊協定。此外,自 1.25.0 版本起,Linux 二進位套件也提供 QUIC 和 HTTP/3 的支援。

QUIC 和 HTTP/3 的支援屬於實驗性質,使用前請謹慎考慮。

從原始碼建置

建置是使用 configure 命令進行設定。請參考 從原始碼建置 nginx 以了解詳細資訊。

在設定 nginx 時,可以使用 --with-http_v3_module 設定參數來啟用 QUIC 和 HTTP/3。

建議使用提供 QUIC 支援的 SSL 程式庫來建置 nginx,例如 BoringSSLLibreSSLQuicTLS。否則,將使用不支援早期資料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';
        }
    }
}

疑難排解

可能有助於識別問題的提示