启用 nginx 分块传输编码

ren*_*sch 20 nginx git chunked

看起来nginx 0.8.35 可能支持分块传输编码

nginx 0.8.35 2010 年 4 月 1 日的变化

*) Change: now the charset filter runs before the SSI filter.

*) Feature: the "chunked_transfer_encoding" directive.
Run Code Online (Sandbox Code Playgroud)

这很棒,因为我正在尝试通过 nginx 反向代理将 git 更改推送到 git-http-backend 进程。出于客户端效率原因, Git HTTP 利用分块传输编码。

但是,我无法让它工作。我在 Debian Lenny 上使用 nginx 0.8.44 并使用以下配置调用:

./configure \
--sbin-path=/usr/sbin \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--user=www-data \
--group=www-data \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module
Run Code Online (Sandbox Code Playgroud)

以及以下配置文件:

server {
    server_name example.com;
    location / {
        proxy_pass  http://192.168.0.10;
        include     /etc/nginx/proxy.conf;
        chunked_transfer_encoding on;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的proxy.conf样子是这样的:

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    100M;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
Run Code Online (Sandbox Code Playgroud)

(最初我将此问题发布到 Stack Overflow,被告知它更适合 Server Fault)

Joh*_*ton 28

这是一个老问题,我知道,但它是为了寻找问题而提出的(我花了一个下午的时间试图解决这个问题)。Martin F 的评论给了我足够的线索来让它工作!

诀窍是proxy_buffering off;在您的位置块中进行设置。假设您的上游服务器正在发回分块响应,这将导致 nginx 将各个块发送回客户端 - 如果您打开了 gzip 输出压缩,甚至可以动态地对它们进行 gzip。

请注意,关闭缓冲可能还有其他缺点,因此不要在不了解原因的情况下盲目地关闭缓冲。


Rog*_*nns 9

我建议编辑您的问题以澄清。分块请求和分块响应之间存在很大差异。约翰道尔顿的回答解决了后者。Git 两者兼而有之。

Nginx 目前不支持分块的 POST 请求,并且该帖子在该主题的搜索结果中显示的很高。分块POST请求用于事先不知道上传数据量的情况,手机经常使用。

我找到的唯一可行的解​​决方案是:

http://wiki.nginx.org/HttpChunkinModule

不幸的是,它需要重新编译 nginx,因为 nginx 不支持可加载模块。


小智 7

就我而言......我尝试了很多东西,最后只需要添加到配置中

proxy_http_version 1.1;
Run Code Online (Sandbox Code Playgroud)

它有效...