nginx as a proxy for NodeJS+socket.io: everything

2019-02-17 21:56发布

As explained on nginx's website I've used these settings for my nginx to proxy websockets to a NodeJS server:

location /socket.io/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

Everything works fine and socket.emit() / socket.on() send messages to each other; until I send a rather big text message (26 kB of html).

  • this big message is not received by NodeJS (so I guess that the issue is on nginx side)
  • there are no error on nginx log
  • once this big message has been send by the client, NodeJS will stop receiving socket.io's heartbeats from this client.

What am I doing wrong? Is there a nginx setting that I am not aware of?

2条回答
我想做一个坏孩纸
2楼-- · 2019-02-17 22:26

Try adding these with your configuration:

proxy_buffers 8 2m;
proxy_buffer_size 10m;
proxy_busy_buffers_size 10m;

Reason : proxy_buffer default size is 4K or 8K. So it could be dropping those connections after the big message causes buffer overflow. Check the default settings here so that it meets you requirements.

查看更多
我想做一个坏孩纸
3楼-- · 2019-02-17 22:32

The "solution" found is to use haproxy to split the tcp stream between nginx and NodeJS.

It is not optimal because it adds yet-another-program in our stack, but it does the job.

It seems to me that nginx websocket support is still far from being production-ready.

查看更多
登录 后发表回答