Nginx报504超时java.io.IOException: Broken pipe
java后端提示java.io.IOException: Broken pipe:
2024-11-04 09:15:09.074 ERROR 26725 --- [http-nio-9001-exec-23] cn.stylefeng.guns.modular.utils.PoiUtil : >>> 导出数据异常:java.io.IOException: Broken pipe
大致是因为, 在导出excel时,因为什么原因客户端被关闭了,后端还在向前端写入数据, 就报了"java.io.IOException: Broken pipe"
1.先检查
先检查前端, 发现设置超时时间为"1000* 10000" 总共10000秒.应该不是这里.
再检查后端,发现没有设置超时时间.
再检查中间件, 发现没有设置超时时间.
2.再尝试确认
使用apiPost访问后提示:
<strong>nginx error!</strong>
<h3>
The page you are looking for is temporarily unavailable. Please try again later.
</h3>
<p>
Something has triggered an error on your website. This is the default error page for <strong>nginx</strong> that is distributed with Fedora. It is located <tt>/usr/share/nginx/html/50x.html</tt>
</p>
<p>
You should customize this error page for your own site or edit the <tt>error_page</tt> directive in the <strong>nginx</strong> configuration file <tt>/etc/nginx/nginx.conf</tt>.
</p>这是nginx的报错信息, 看来是nginx超时了.
去nginx的error.log中检查日志,发现
2024/11/04 10:25:42 [error] 29990#29990: *1879503 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 1.192.176.71, server: hlb.helibaopay.net, request: "POST /api/rebateInfo/exportExcel HTTP/1.1", upstream: "http://127.0.0.1:9001/rebateInfo/exportExcel", host: "hlb.helibaopay.net"解决方式(proxy/fastcgi):
那么就需要在这个请求上设置nginx的超时时间了.
nginx默认请求时间是60s
先看下nginx使用的是什么协议(proxy/fastcgi):
(可以根据需求将以下配置加在http或server或location模块)
如果使用proxy_pass进行请求转发,那我们就配置proxy相关的参数:
proxy_connect_timeout 100; #nginx跟后端服务器连接超时时间(代理连接超时)默认60s
proxy_read_timeout 100; #后端服务器数据回传时间(代理发送超时)默认值60s
proxy_send_timeout 100; #连接成功后,后端服务器响应时间(代理接收超时)默认值60s
如果使用fastcgi_pass进行请求转发,那就配置fastcig相关的参数:
fastcgi_connect_timeout 1800s;#nginx跟后端服务器连接超时时间(代理连接超时)默认60s
fastcgi_send_timeout 1800s;#后端服务器数据回传时间(代理发送超时)默认值60s
fastcgi_read_timeout 1800s;#连接成功后,后端服务器响应时间(代理接收超时)默认值60s
修改完重启nginx生效
server {
listen 8888;
proxy_read_timeout 300s; # 增加读超时时间
proxy_connect_timeout 30s; # 增加连接超时时间
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
location / {
proxy_pass http://pdfs;
proxy_connect_timeout 18000;
proxy_send_timeout 18000;
proxy_read_timeout 18000;
}
}