问题:
-
通过浏览器访问spring boot部署在默认tomcat中的文件下载接口,可以直接下载文件。
-
但是通过Nginx反向代理接口后,在通过浏览器访问出现ERR_EMPTY_RESPONSE的错误。
-
其他非下载接口却是正常的。
背景:
-
Nginx版本:
nginx version: nginx/1.20.2 -
spring boot实现文件下载功能,response header为:
Connection: keep-alive Content-Disposition: attachment;filename*=UTF-8''quickstart.sh Content-Type: application/octet-stream Date: Thu, 17 Mar 2022 02:01:29 GMT Keep-Alive: timeout=60 Transfer-Encoding: chunked Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers -
Nginx配置为:
location /test {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 100M;
proxy_buffers 4 100M;
proxy_busy_buffers_size 100M;
proxy_temp_file_write_size 100M;
client_max_body_size 200M;
client_body_buffer_size 100M;
proxy_pass http://test;
}
解决方案:
Nginx 的反向代理,默认用的就是 HTTP 1.0,那就导致了数据无法获取的问题,可以参考 Nginx 的官方文档说明:nginx.org/en/docs/htt… 原文中:
Syntax: proxy_http_version 1.0 | 1.1;
Default: proxy_http_version 1.0;
Context: http, server, location
This directive appeared in version 1.1.4.
所以,我们如果要解决这个问题,只需要设置一下 HTTP 版本为 1.1 就好了: 修改 nginx.conf 文件如下:
location /test {
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 100M;
proxy_buffers 4 100M;
proxy_busy_buffers_size 100M;
proxy_temp_file_write_size 100M;
client_max_body_size 200M;
client_body_buffer_size 100M;
proxy_pass http://test;
}
这里就增加了一行:
proxy_http_version 1.1;