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

libraco
  • 760

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

Traceback (most recent call last):
  File "run.py", line 6, in <module>
    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
回复
阅读 5.8k
2 个回答
✓ 已被采纳

你用的是flask自带的内置的测试用的http server,不支持并发。你请求频率过高,可能就会出现你所说的错误。

app.run(threaded=True)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏