Nginx 反向代理重新打包分块响应

Nginx 反向代理重新打包分块响应

我正在运行 nginx/1.1.19:

# nginx -V
nginx version: nginx/1.1.19
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-dav-ext-module

我正在使用它来反向代理一个可以将数据作为分块响应进行流式传输的服务。来自后端服务的每个块都是有效的 JSON 文档,但当通过 nginx 代理访问时,这些块会被重新打包成“整数”大小,如 0x1000、0x2000、0x4000 等。这使得这些块不再是有效的 JSON blob,因为每个块的开头都有一个文档的结尾,结尾处又有一个文档的开头。

我的location块配置如下:

    location / {
            proxy_pass http://backend;
            proxy_read_timeout 600;
            proxy_buffering off;
    }

我认为该proxy_buffering off;部件应避免我所看到的行为。我是否需要更改其他设置?

答案1

您永远不应将业务数据绑定到块。块大小可以由代理更改,甚至由不同的 Web 服务器更改。甚至一些基于启发式算法的代理也可能会聚合块。即使每个块都包含 JSON 或 1.5 JSON 或多个 JSON 的一部分,您的块解析器(如果有客户端 SDK 等)也应该能够解析您的数据。

相关内容