简体   繁体   中英

error: [Errno 32] Broken pipe when running flask with nginx at localhost

I have a Flask app using separate socket.io server for realtime application. I run them on separate port and use nginx to test them on the same port.

upstream httpapp {
    ip_hash;
    server 127.0.0.1:7777;
}
upstream socketioapp {
    ip_hash;
    server 127.0.0.1:5555;
}
server {
    listen       5000;
    server_name  localhost;
        location / {
                proxy_pass http://httpapp;
                proxy_http_version 1.1;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
        location /socket.io {
                proxy_pass http://socketioapp;
                proxy_http_version 1.1;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

This setting has been fine for the last half a year of development, until one day, I keep getting this error for no obvious reason. This is the error I get when I visit localhost:5000.

127.0.0.1 - - [13/Oct/2015 07:58:08] "GET /static/js/main.js HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 53388)
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 657, in __init__
    self.finish()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 716, in finish
    self.wfile.close()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 283, in close
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

The error only occurs on 2.4MB static javascript files , which is uncompressed size at development environment. I know the static file is huge but it's been working fine before.

The most important observation - visiting localhost:7777 is totally functional. However, it's not suitable for testing the application since it requires realtime feature. The idea was to put both flask application and realtime application on the same port and use default socket.io init. One thing I haven't tried is to change socket.io settings to localhost:5555.

I'm running flask using python manage.py runserver with flask_script style configuration as it servers all the purpose as development environment. (I haven't touched production environment at EC2 since it's running fine and I didn't want to mess with the business.) The only action that caused this which I can recall is having changed the port number from 5000 -> 80 and running nginx using sudo. It sounds irrelevant, but it really took me as a surprise.

Hours of research on internet only explained the definition of the error, not the solution. What are the actions that I take to get anywhere near close to solving this issue?

I have a Django app and ran into the same issue for about the last 2 months, but haven't been able to figure it out.

I just updated my nginx.conf file to have:

proxy_read_timeout 400s;

According to http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout , the default timeout is 60s. I updated mine to 400s. So far it is working great!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM