【问题标题】:Chrome net::ERR_HTTP2_PROTOCOL_ERROR 200 after a reconnectChrome net::ERR_HTTP2_PROTOCOL_ERROR 200 重新连接后
【发布时间】:2020-07-13 09:36:57
【问题描述】:

我正在使用 Node 服务器和一个处理服务器发送事件流的快速应用程序。这是通过启用了 http2 的 NginX 代理的。 SSE 事件通过 React 应用程序中的 EventSource 使用。我每 10 秒发送一次心跳消息以保持连接有效。

这一切都很好,直到出现某种形式的网络中断,例如让我的笔记本电脑进入睡眠状态然后重新唤醒它。

然后从那一点开始,流将每隔 40 秒左右出错一次,出现 net::ERR_HTTP2_PROTOCOL_ERROR 200 错误,然后重新连接,而不是只用稳定的流重新连接一次。

Firefox 可以正常工作。它不会出错,只会重新连接一次。

如果我将 Node 配置为直接提供 http2 而不是通过 NGinx 作为测试(通过 spdy 库),那么一切都会按预期工作,所以我认为这不是 Node 问题,我的 Nginx 配置和铬合金。

Nginx 配置如下(位置 /stream 是 SSE 代理)

server {
    listen 28443 ssl http2;
    listen [::]:28443 ssl http2;

    server_name example.com;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    http2_max_field_size 16k;
    http2_max_header_size 128k;

    root /var/www/example.com;
    index index.html index.htm;

    location / {
        client_max_body_size 100M;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://localhost:28080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'Upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /stream {
        proxy_http_version 1.1;
        # proxy_request_buffering off;
        # proxy_buffering off;
        # proxy_cache off;
        # chunked_transfer_encoding off;
        proxy_set_header    Connection          '';
        proxy_set_header  Host            $host;
        proxy_set_header  X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:28080/stream;
    }
}

我尝试了关闭 proxy_buffers 和 keepalive 设置等的各种组合。这似乎只影响错误之间的时间,即 5 分钟而不是 40 秒。

【问题讨论】:

    标签: node.js google-chrome nginx http2 eventsource


    【解决方案1】:

    不知道你是否明白这一点。我最近通过增加大小遇到了同样的问题:

    http2_max_field_size 64k;
    http2_max_header_size 512k;
    

    chrome 的net::ERR_HTTP2_PROTOCOL_ERROR 不见了。

    另外,不确定它是否适用于您。如果我使用 Firefox,我实际上可以正确访问我的网站。

    【讨论】:

    • 这是搜索了 3 天后的最佳答案,非常感谢。
    【解决方案2】:

    http2_max_field_sizehttp2_max_header_size 指令自版本 1.19.7 起已过时

    相反,可以使用以下内容:

    large_client_header_buffers 4 64k;
    

    【讨论】:

      【解决方案3】:

      现在令牌被命名

      在我的环境中刚刚设置为:

      large_client_header_buffers 10 512k;
      

      错误消失了

      来源:

      http://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-24
        • 2020-07-02
        • 2021-05-07
        • 1970-01-01
        • 2021-08-17
        • 1970-01-01
        • 2022-11-17
        • 2013-01-25
        相关资源
        最近更新 更多