取消掉Transfer-Encoding:chunked

本文详细解释了HTTPResponse中使用Chunked编码的情况及原理,包括如何在消息头部设置Transfer-Encoding并进行Chunked编码传输内容的过程,以及PHP版本的Chunked解码代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候,Web服务器生成HTTP Response是无法在Header就确定消息大小的,这时一般来说服务器将不会提供Content-Length的头信息,而采用Chunked编码动态的提供body内容的长度。

进行Chunked编码传输的HTTP Response会在消息头部设置:

Transfer-Encoding: chunked

表示Content Body将用Chunked编码传输内容。

Chunked编码使用若干个Chunk串连而成,由一个标明长度为0的chunk标示结束。每个Chunk分为头部和正文两部分,头部内容指定下一段正文的字符总数(十六进制的数字)和数量单位(一般不写),正文部分就是指定长度的实际内容,两部分之间用回车换行(CRLF)隔开。在最后一个长度为0的Chunk中的内容是称为footer的内容,是一些附加的Header信息(通常可以直接忽略)。具体的Chunk编码格式如下:

  Chunked-Body = *chunk
         "0" CRLF
         footer
         CRLF
  chunk = chunk-size [ chunk-ext ] CRLF
      chunk-da<wbr>ta CRLF</wbr>

  hex-no-zero = <HEX excluding "0">

  chunk-size = hex-no-zero *HEX
  chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-value ] )
  chunk-ext-name = token
  chunk-ext-val = token | quoted-string
  chunk-da<wbr>ta = chunk-size(OCTET)</wbr>

  footer = *entity-header

RFC文档中的Chunked解码过程如下:
  length := 0
  read chunk-size, chunk-ext (if any) and CRLF
  while (chunk-size > 0) {
  read chunk-da<wbr>ta and CRLF<br>  append chunk-da<wbr>ta to entity-body<br>  length := length + chunk-size<br>  read chunk-size and CRLF<br>  }<br>  read entity-header<br>  while (entity-header not empty) {<br>  append entity-header to existing header fields<br>  read entity-header<br>  }<br>  Content-Length := length<br>  Remove "chunked" from Transfer-Encoding</wbr></wbr>

最后提供一段PHP版本的chunked解码代码:

$chunk_size=(integer)hexdec(fgets($socket_fd,4096) );
while(!feof($socket_fd)&&$chunk_size>0) {
$bodyContent.=fread($socket_fd,$chunk_size);
fread($socket_fd,2);//skip \r\n
$chunk_size=(integer)hexdec(fgets($socket_fd,4096
) );
}


要解决服务器不返回Transfer-Encoding:chunked,在客户端请求的时候可以使用http 1.0的协议。

### 解决 Nginx 502 Bad Gateway 错误并处理 Transfer-Encoding: chunked #### 调整 PHP-CGI 进程配置 当遇到 `Nginx` 报告 `502 Bad Gateway` 错误时,可能是由于 `php-cgi` 进程数不足、PHP 执行时间过长或 `php-cgi` 进程崩溃引起的[^1]。为了防止这些问题发生,可以调整 `php-fpm` 的池设置: ```ini [www] pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 7 request_terminate_timeout = 30s ``` 通过增加最大子进程数量 (`pm.max_children`) 和适当延长请求终止超时时间 (`request_terminate_timeout`) 可以有效减少因资源耗尽而导致的服务中断。 #### 增大头部缓冲区大小 另一个常见原因是 HTTP 请求/响应头过大超过了默认限制,这可以通过修改 `large_client_header_buffers` 参数来解决。对于较大的响应头,建议如下配置[^3]: ```nginx http { ... server { listen 80; server_name *.example.com; # 设置更大的客户端头部缓存空间 large_client_header_buffers 4 16k; location / { proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; 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_http_version 1.1; proxy_set_header Connection ""; } } } ``` 这里特别注意设置了 `proxy_http_version 1.1` 并清除了连接头字段(`Connection ""`),这样可以让代理服务器支持持久连接以及正确解析带有 `Transfer-Encoding: chunked` 的响应体。 #### 升级 Nginx 版本注意事项 在升级过程中可能会引入新的兼容性问题,比如从较低版本(如 1.20.x)升至较高版本(如 1.23.x),应确保所有依赖项都已更新并且测试环境稳定后再部署到生产环境中[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值