You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx从1.18升级至1.24后出现重复Transfer-Encoding头错误求助

问题描述

将Nginx从1.18版本升级至1.24.0版本后,出现如下错误日志:

upstream sent duplicate header line: "Transfer-Encoding: chunked", previous value: "Transfer-Encoding: chunked" while reading response header from upstream, client: 54.xx.xx.xx, server: backend.example.com, request: "POST /test/file HTTP/1.1", upstream: "http://10.0.xx.xx:6067/test/file", host: "backend.example.com", referrer: "https://app.example.com/"

请求已在后端Spring Boot服务中成功处理,但Nginx报错导致前端返回502错误。

若移除server块中的include /etc/nginx/http_proxy.conf;配置,则出现另一个错误:

upstream sent invalid chunked response while reading upstream, client: 54.xx.xx.xx,server: backend.example.com, request: "POST /test/file HTTP/1.1", upstream: "http://10.0.xx.xx:6067/test/file", host: "backend.example.com", referrer: "https://app.example.com/"

环境信息

  • Nginx版本:nginx/1.24.0
  • 操作系统版本:Ubuntu 22.04.3 LTS

配置文件

backend.conf

server {
  access_log /var/log/nginx/access.log;
  index index.html index.htm index.nginx-debian.html;
  server_name backend.example.com;
  include /etc/nginx/http_proxy.conf;
  location /test/file {
    proxy_pass http://10.0.xx.xx:6067/test/file;
    proxy_set_header  X-Real-IP       $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $host;
    proxy_set_header  X-Forwarded-Port 443;
    proxy_set_header  X-Forwarded-Proto https;
    proxy_set_header  X-Forwarded-Host $host:443;
  }
  listen 443 ssl; 
  ssl_certificate /etc/backend.example.com/fullchain.pem; 
  ssl_certificate_key /etc/backend.example.com/privkey.pem; 
  include /etc/options-ssl-nginx.conf; 
  ssl_dhparam /etc/ssl-dhparams.pem;
}

http_proxy.conf

proxy_buffers           32 4k;
proxy_http_version      1.1;
proxy_send_timeout      60;
proxy_read_timeout      60;
proxy_connect_timeout   60;
proxy_set_header    Connection    '';
解决方向
  1. 检查后端Spring Boot服务的响应头
    后端大概率重复发送了Transfer-Encoding: chunked头,这是触发Nginx报错的直接原因。可以用curl直接请求后端服务,确认响应头是否存在重复:

    curl -I -X POST http://10.0.xx.xx:6067/test/file -H "Host: backend.example.com"
    

    如果发现重复头,排查Spring Boot代码或配置:比如是否有过滤器/拦截器手动添加了该头,或是第三方依赖库导致的重复。

  2. 调整Nginx配置处理重复头
    如果后端暂时无法修复,可在Nginx的/test/file location块中添加配置,忽略或重写重复的响应头:

    location /test/file {
      # 原有配置保留
      proxy_hide_header Transfer-Encoding;
      # 或者使用以下配置忽略后端的该头,由Nginx自行处理编码
      # proxy_ignore_headers Transfer-Encoding;
    }
    

    注意:使用proxy_ignore_headers时,需确保Nginx能正确识别响应的编码格式。

  3. 测试HTTP版本兼容性
    当前配置使用HTTP/1.1持久连接,若后端对HTTP/1.1的chunked编码处理存在兼容性问题,可临时调整为HTTP/1.0测试:

    # 在http_proxy.conf或location块中修改
    proxy_http_version 1.0;
    proxy_set_header Connection close;
    

    此方案会关闭持久连接,可能影响性能,仅作为临时排查手段。

  4. 核对Nginx版本变更的校验逻辑
    Nginx 1.24.0对响应头的校验比1.18更严格,旧版本可能容忍重复头但新版本直接报错。可查看官方变更日志,确认是否有相关头部校验规则的调整。

内容的提问来源于stack exchange,提问作者Sneha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 09:53:23