Nginx 中的HTTP2
Nginx 环境检查
在nginx 中使用http2,首先需要确认是否安装了ngx_http_v2_module
模块。 默认不会加入。 需要通过配置--with-http_v2_module
参数来启用。
检查nginx是否已经支持http2
# 该ngx_http_v2_module模块(1.9.5)提供对 HTTP/2 的支持。
nginx -V
参数配置参考:https://nginx.org/en/docs/http/ngx_http_v2_module.html
Nginx配置
部署并配置Nginx,配置如下:
worker_processes 1;
events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 8080;http2 on;server_name localhost;location / {root /opt/homebrew/Cellar/nginx/1.27.4/html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}include servers/*;
}
HTTP2 验证
网站验证:https://tools.keycdn.com/http2-test
使用nghttp 工具进行验证:
分别使用一下命令进行验证:
# 表明客户端和服务器都明确知晓对方支持 HTTP/2 协议,并且直接使用 HTTP/2 进行通信,无需进行协议协商
curl -v --http2-prior-knowledge http://127.0.0.1:8080
# 对于https,TLS握手中协商使用HTTP/2, 对于HTTP则尝试使用upgrade,将请求升级到HTTP/2。
curl -v --http2 http://127.0.0.1:8080
# 专门用于测试和调试 HTTP/2 协议的工具,能详细展示 HTTP/2 通信过程。
nghttp -v http://127.0.0.1:8080
注意事项
Nginx 中可以通过配置ssl_alpn_protocol
参数,来指定在 SSL 握手期间由 ALPN 选择的协议,否则返回空字符串 (1.21.4),但是我没试过,这里记录下供你们参考,有结果请评论给我。 谢谢。
参考文档
AWS 负载均衡器控制器: https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.2/guide/service/annotations/