HTTP/3 是 Hypertext Transfer Protocol(HTTP) 的第三个主要版本。与依赖 TCP 的前辈不同,HTTP/3 基于 QUIC (Quick UDP Internet Connections) protocol。它带来了多项好处,减少了延迟并提高了性能:
APISIX 目前支持下游客户端和 APISIX 之间的 HTTP/3 连接。尚不支持与上游服务的 HTTP/3 连接。欢迎社区贡献。
:::caution
此功能尚未经过大规模测试,因此不建议用于生产使用。
:::
本文档将向您展示如何配置 APISIX 以在客户端和 APISIX 之间启用 HTTP/3 连接,并记录一些已知问题。
将以下配置添加到 APISIX 的配置文件。该配置将在端口 9443
(或其他端口)上启用 HTTP/3:
apisix: ssl: listen: - port: 9443 enable_http3: true ssl_protocols: TLSv1.3
:::info
如果您使用 Docker 部署 APISIX,请确保在 HTTP3 端口中允许 UDP,例如 -p 9443:9443/udp
。
:::
然后重新加载 APISIX 以使配置更改生效:
apisix reload
HTTP/3 需要 TLS。您可以利用购买的证书或自行生成证书。
如自行生成,首先生成证书颁发机构 (CA) 密钥和证书:
openssl genrsa -out ca.key 2048 && \ openssl req -new -sha256 -key ca.key -out ca.csr -subj "/CN=ROOTCA" && \ openssl x509 -req -days 36500 -sha256 -extensions v3_ca -signkey ca.key -in ca.csr -out ca.crt
接下来,生成具有 APISIX 通用名称的密钥和证书,并使用 CA 证书进行签名:
openssl genrsa -out server.key 2048 && \ openssl req -new -sha256 -key server.key -out server.csr -subj "/CN=test.com" && \ openssl x509 -req -days 36500 -sha256 -extensions v3_req \ -CA ca.crt -CAkey ca.key -CAserial ca.srl -CAcreateserial \ -in server.csr -out server.crt
可选择性地将存储在 server.crt
和 server.key
中的内容加载到环境变量中:
server_cert=$(cat server.crt) server_key=$(cat server.key)
创建一个保存服务器证书及其密钥的 SSL 对象:
curl -i "http://127.0.0.1:9180/apisix/admin/ssls" -X PUT -d ' { "id": "quickstart-tls-client-ssl", "sni": "test.com", "cert": "'"${server_cert}"'", "key": "'"${server_key}"'" }'
创建一个路由至 httpbin.org
:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d ' { "id":"httpbin-route", "uri":"/get", "upstream": { "type":"roundrobin", "nodes": { "httpbin.org:80": 1 } } }'
验证前需要安装支持 HTTP/3 的 curl,如 static-curl 或其他支持 HTTP/3 的 curl。
发送一个请求到路由:
curl -kv --http3-only \ -H "Host: test.com" \ --resolve "test.com:9443:127.0.0.1" "https://test.com:9443/get"
应收到 HTTP/3 200
相应如下:
* Added test.com:9443:127.0.0.1 to DNS cache * Hostname test.com was found in DNS cache * Trying 127.0.0.1:9443... * QUIC cipher selection: TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_CCM_SHA256 * Skipped certificate verification * Connected to test.com (127.0.0.1) port 9443 * using HTTP/3 * [HTTP/3] [0] OPENED stream for https://test.com:9443/get * [HTTP/3] [0] [:method: GET] * [HTTP/3] [0] [:scheme: https] * [HTTP/3] [0] [:authority: test.com] * [HTTP/3] [0] [:path: /get] * [HTTP/3] [0] [user-agent: curl/8.7.1] * [HTTP/3] [0] [accept: */*] > GET /get HTTP/3 > Host: test.com > User-Agent: curl/8.7.1 > Accept: */* > * Request completely sent off < HTTP/3 200 ... { "args": {}, "headers": { "Accept": "*/*", "Content-Length": "0", "Host": "test.com", "User-Agent": "curl/8.7.1", "X-Amzn-Trace-Id": "Root=1-6656013a-27da6b6a34d98e3e79baaf5b", "X-Forwarded-Host": "test.com" }, "origin": "172.19.0.1, 123.40.79.456", "url": "http://test.com/get" } * Connection #0 to host test.com left intact