python - Nginx反向代理Flask发生Broken pipe错误

 WingKeii- 发布于 2022-10-29 18:00

用flask写了个后台,直接用ip访问没有问题,但是用nginx反向代理多刷新几次页面就挂了。

Traceback (most recent call last):
  File "run.py", line 6, in 
    app.run(host='0.0.0.0', port=app.config['PORT'])
  File "/usr/lib64/python2.7/site-packages/flask/app.py", line 843, in run
    run_simple(host, port, self, **options)
  File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 694, in run_simple
    inner()
  File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 659, in inner
    srv.serve_forever()
  File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 499, in serve_forever
    HTTPServer.serve_forever(self)
  File "/usr/lib64/python2.7/SocketServer.py", line 238, in serve_forever
    self._handle_request_noblock()
  File "/usr/lib64/python2.7/SocketServer.py", line 297, in _handle_request_noblock
    self.handle_error(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib64/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib64/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib64/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib64/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
socket.error: [Errno 32] Broken pipe

访问路由:

@app.route('/')
@app.route('/index')
@flask_login.login_required
def index():
    url = []
    ip = []
    total = {}    
    return flask.render_template('index.html', ip=ip, url=url, total=total)

--------------------- update -------------------

  1. 直接用命令行python run.py启动没有问题

  2. 启用supervisor重定向标准输出到日志文件,请求频繁就会爆Broken Pipe错误

  3. 不论是否使用supervisor,用nginx反向代理请求频繁都会报错

所以我个人觉得是写日志文件的原因,在supervisor和nginx里如何配置可以规避这个问题呢?

--------------------- update -------------------
broken pipe 详情

[dev@localhost ~]$ ll /proc/30861/fd    
total 0
lr-x------ 1 dev dev 64 Aug 30 17:51 0 -> pipe:[219413249]
l-wx------ 1 dev dev 64 Aug 30 17:51 1 -> pipe:[219413250]
lrwx------ 1 dev dev 64 Aug 30 17:51 10 -> socket:[219423379]
lr-x------ 1 dev dev 64 Aug 30 17:51 11 -> /dev/urandom
l-wx------ 1 dev dev 64 Aug 30 17:51 2 -> pipe:[219413250]
l-wx------ 1 dev dev 64 Aug 30 17:51 4 -> /home/dev/Spectre/log/spectre.log
lrwx------ 1 dev dev 64 Aug 30 17:51 6 -> socket:[219433077]
lrwx------ 1 dev dev 64 Aug 30 17:51 8 -> socket:[219423378]
lrwx------ 1 dev dev 64 Aug 30 17:51 9 -> socket:[219413251]
[dev@localhost ~]$ lsof | grep 219413249
python     30861                  dev    0r     FIFO                0,8       0t0 219413249 pipe
python     30861  30866           dev    0r     FIFO                0,8       0t0 219413249 pipe
python     30861  30867           dev    0r     FIFO                0,8       0t0 219413249 pipe
python     30861  30869           dev    0r     FIFO                0,8       0t0 219413249 pipe
python     30861  30871           dev    0r     FIFO                0,8       0t0 219413249 pipe
python     30861  30873           dev    0r     FIFO                0,8       0t0 219413249 pipe
superviso 122521                  dev   11w     FIFO                0,8       0t0 219413249 pipe
[dev@localhost ~]$ lsof | grep 219413250
python     30861                  dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861                  dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30866           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30866           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30867           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30867           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30869           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30869           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30871           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30871           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30873           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30873           dev    2w     FIFO                0,8       0t0 219413250 pipe
superviso 122521                  dev   12r     FIFO                0,8       0t0 219413250 pipe
[dev@localhost ~]$ lsof | grep 219413250
python     30861                  dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861                  dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30866           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30866           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30867           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30867           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30869           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30869           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30871           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30871           dev    2w     FIFO                0,8       0t0 219413250 pipe
python     30861  30873           dev    1w     FIFO                0,8       0t0 219413250 pipe
python     30861  30873           dev    2w     FIFO                0,8       0t0 219413250 pipe
superviso 122521                  dev   12r     FIFO                0,8       0t0 219413250 pipe
1 个回答
  • 你用的是flask自带的内置的测试用的http server,不支持并发。你请求频率过高,可能就会出现你所说的错误。

    2022-10-31 00:25 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有