从 h2 到 h2c 的反向代理 HTTP/2

Reverse proxying HTTP/2 from h2 to h2c

我们有一个 java 网络服务器,可以通过 h2c 提供内容(HTTP/2 明文)

我们想将使用 h2(即通过 SSL 的标准 HTTP/2)建立的代理连接反向到 h2c 中的 java 服务器。

在 nginx 上启用 HTTP/2 非常简单,处理传入的 h2 连接工作正常。

我们如何告诉 nginx 使用 h2c 而不是 http/1.1 代理连接?

注意:可以接受非 nginx 解决方案

server {
    listen       443 ssl http2 default_server;
    server_name  localhost;

    ssl_certificate      /opt/nginx/certificates/???.pem;
    ssl_certificate_key  /opt/nginx/certificates/???.pk8.key.pem;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        proxy_pass http://localhost:8080/;  ## <---- h2c here rather than http/1.1
    }
}

结论(2016 年 6 月)

这可以通过 haproxy 使用如下所示的简单配置文件来完成。

查询清楚(HttpServletRequest)req.getProtocol()returnsHTTP/2.0

global
tune.ssl.default-dh-param 1024

defaults
timeout connect 10000ms
timeout client 60000ms
timeout server 60000ms

frontend fe_http
mode http
bind *:80
# Redirect to https
redirect scheme https code 301

frontend fe_https
mode tcp
bind *:443 ssl no-sslv3 crt mydomain.pem ciphers TLSv1.2 alpn h2,http/1.1
default_backend be_http

backend be_http
mode tcp
server domain 127.0.0.1:8080

HAProxy 确实支持。

HAProxy 可以卸载 TLS 并转发到支持 h2c.

的后端

有关如何设置此配置的详细信息,请参阅 this blog post