NGINX Error: upstream sent invalid chunked response while reading upstream
Asked Answered
M

3

20

We have tomcat with Jersey serving APIs behind NGINX. A new streaming API we have developed worked great when we call Tomcat directly, but started getting no response when calling it through NGINX.

Looking at NGINX logs we got:

upstream sent invalid chunked response while reading upstream

Myers answered 23/3, 2018 at 14:20 Comment(0)
M
30

We have solved the issue by adding the following to NGINX:

proxy_http_version 1.1

I guess NGINX proxies traffic by default with http version 1.0, but chunked transfer encoding is a http 1.1 feature.

https://forum.nginx.org/read.php?2,247883,247906#msg-247906

Myers answered 23/3, 2018 at 14:20 Comment(2)
This answer already saved my life twice. I always forget what is the cause of this issue and end up here to remember.Bister
Ha ha... I added it here due to selfish reasons, I knew I will need it in the future and will search for an answer in StackOverflow :-)Myers
B
6

In my case, only setting proxy_http_version 1.1 did not work. I had to set these -

proxy_http_version 1.1;
proxy_set_header Connection "";
Both answered 31/5, 2021 at 13:47 Comment(1)
Why Connection header should be blank, excuse me?Gaultiero
F
0

You should add proxy_http_version 1.1,

This directive appeared in version 1.1.4.
Sets the HTTP protocol version for proxying. By default, version 1.0 is used. Version 1.1 is recommended for use with keepalive connections and NTLM authentication.

Best way, Create a http_proxy.conf file and include it in your server config:

## 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    '';
#proxy_set_header   HEADER_NAME   $VALUE;

## server.conf
server {
  listen 80;
  server_name example.domain.com
  include /etc/nginx/http_proxy.conf;    
  #.....
}

Flaherty answered 23/2, 2023 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.