一、问题现象和日志报错
之前在项目实现了excel导入导出:django导入导出excel实践,之前一直稳定运行,突然得知导出用户信息时出现nginx错误报告:
查看nginx日志,报错信息如下所示:
upstream timed out (: Connection timed out) while reading response header from upstream, client: 119.157.163.211,
server: localhost, request: "GET /xxxxx/export/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:7080", host: "xxxx.com:8100", referrer: "http://xxxxx.com:8100/xxxxxxx/"
查看uwsgi日志,报错信息如下所示:
Tue Jul :: - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /stark/crm/customer/export/ (ip 113.57.163.211) !!!
Tue Jul :: - uwsgi_response_write_body_do(): Broken pipe [core/writer.c line ] during GET /stark/crm/customer/export/ (113.57.163.211)
OSError: write error
二、解决方法
1、调整uwsgi配置
ignore-singpipe 使uWSGI不显示SIGPIPE错误;
ignore-write-errors 使它不显示诸如uwsgi_response_writev_headers_and_body_do的错误;
disable-write-exception
防止 OSError
写入时生成。
因此在uwsgi.ini中添加如下配置:
ignore-sigpipe=true
ignore-write-errors=true
disable-write-exception=true
配置添加后,uwsgi日志中不再显示错误信息。
2、调整nginx配置
一开始是调大了nginx的超时参数设置:
proxy_connect_timeout ;
proxy_read_timeout ;
proxy_send_timeout ;
没有产生作用nginx依然报错。
proxy_read_time针对的是反向代理转发的超时,不针对uwsgi超时,因此需要在nginx配置中添加如下参数:
uwsgi_send_timeout ; # 指定连接到后端uWSGI的超时时间。
uwsgi_connect_timeout ; # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
uwsgi_read_timeout ; # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
重启nginx后,excel导出正常。