Socket.io Cluster With Nginx as Reverse Proxy
Node.js by default runs on a single process and at max utilizes one CPU. To take the full advantage of a multi core system, multiple node processes can be run with a frontend proxy interfacing with the client.
This scenario can be supported in node.js using the cluster module. However with websocket additionally it is required to provide a mapping between the client session-id and the server handling the client requests. This is achieved by routing the clients based on their originating address.
Nginx versions since 1.3, supports websocket protocol and are well suited to provide this functionality.
Relevant configuration in nginx.conf
for websocket clustering
If your server is on Amazon EC2 then have to enable the ports for inbound traffic in your security groups. Found that when changing the setting on a running server, we have to edit the existing security groups and add new ports.
With the changes, the client is able to connect to proxy server and is routed to the node processes.
It is also useful to view the node process logs by prefixing the startup command as DEBUG=* node server.js
References:
- http://nginx.com/blog/websocket-nginx/
- http://socket.io/docs/using-multiple-nodes/#