是否可以在没有 ssl 的情况下在 NGINX 端口 443 上 运行 HTTP/2?

Is it possible to run HTTP/2 on NGINX port 443 without ssl?

我有 Envoy 代理处理 SSL 终止。 Nginx(docker 容器中的 1.17.0,已编译 --with-http_v2_module)是几个上游服务之一。结果,Nginx 在 443 端口接收流量,但没有使用 ssl 模块:

server {
    listen 443;
    server_name example.com www.example.com;
    root /var/www/html;

...

这工作正常,但如果我尝试将 http2 添加到监听行的末尾,我会收到:

curl: (1) Received HTTP/0.9 when not allowed

...不仅针对 example.com 有问题,而且 所有 服务器。

出于明显的性能原因,我希望 Envoy 通过 HTTP/2 与 Nginx 对话。

是否有一些技巧可以让 nginx 在端口 443 上使用 http2 而无需 SSL 终止?


编辑:

核心nginx.conf:

user  nginx;
worker_processes  2;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    client_max_body_size 64M;

    sendfile        on;

    keepalive_timeout  65;

    fastcgi_cache_path /etc/nginx-cache levels=1:2 keys_zone=multipress:100m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

请注意,我可以使用显式 curl --http2 命令成功卷曲当前的 Envoy HTTP2 服务器。问题是 Envoy 和 Nginx 之间的 HTTP2 连接。

Nginx只支持h2c(也就是HTTP/2没有HTTPS的叫法),所以不能用HTTP/1.1连接再升级。

事实上,如果您尝试使用 HTTP/1.1 进行连接,那么 nginx 将出现 it doesn’t support HTTP/1.1 and HTTP/2 on the same port unless you are using HTTPS.

错误

所以对于 curl 你必须使用这个语法直接跳到 HTTP/2:

curl --http2-prior-knowledge localhost:80

不确定 Envoy 中是否有类似的配置,请不要使用它,但如果以上内容与 curl 一起使用并且在 Envoy 中仍然出现相同的错误,那么至少你已经找到了 Nginx 问题的答案。

但是我不确定是否有必要为您的后端服务器启用 h2c,甚至是否明智。对于如上所述禁用 HTTP/1.1 的开始,任何其他应用程序、服务甚至使用 HTTP/1.1 的 curl 测试命令都会中断。

此外 ,主要好处是客户端到服务器的连接,而不是服务器到服务器的后端连接。

Include "http2" right after the listen port in /etc/nginx/nginx.conf如下和restart nginx pid(服务nginx重启)

server {
    listen 80 http2;
    server_name  myhost.mydomain.com;

location ^~ /api/detection/ {
    include uwsgi_params;
    proxy_pass    http://models;
}