# upstream sent invalid Transfer-Encoding: chunked with Content-Length while reading response header from upstream

- **ID:** `nginx/upstream-sent-invalid-transfer-encoding-chunked-with-content-length`
- **Domain:** nginx
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

Upstream server sends both Content-Length and Transfer-Encoding: chunked headers in the same response, which is invalid per HTTP/1.1.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.24.0 | active | — | — |
| nginx 1.25.3 | active | — | — |
| nginx 1.26.0 | active | — | — |

## Workarounds

1. **Use proxy_ignore_headers to ignore one of the conflicting headers. Example to ignore Content-Length:
  location / {
      proxy_pass http://upstream;
      proxy_ignore_headers Content-Length;
  }** (85% success)
   ```
   Use proxy_ignore_headers to ignore one of the conflicting headers. Example to ignore Content-Length:
  location / {
      proxy_pass http://upstream;
      proxy_ignore_headers Content-Length;
  }
   ```
2. **Fix the upstream application to send only one of the headers. For example, in a Node.js Express app:
  app.get('/data', (req, res) => {
      res.removeHeader('Content-Length');
      res.setHeader('Transfer-Encoding', 'chunked');
      res.send('hello');
  });** (95% success)
   ```
   Fix the upstream application to send only one of the headers. For example, in a Node.js Express app:
  app.get('/data', (req, res) => {
      res.removeHeader('Content-Length');
      res.setHeader('Transfer-Encoding', 'chunked');
      res.send('hello');
  });
   ```

## Dead Ends

- **Remove Content-Length header from nginx config using proxy_set_header** — proxy_set_header affects request headers to upstream, not response headers from upstream. (95% fail)
- **Enable chunked_transfer_encoding off; in nginx** — This directive controls nginx's own encoding, not upstream response validation. (80% fail)
- **Increase proxy_buffer_size** — Header size is not the issue; header validity is. (100% fail)
