简体   繁体   中英

How to make more than 65k request on a port

I have a simple node js web socket server as follows:

var ws = require("nodejs-websocket")
var connectionCount = 0;
console.info("Node websocket started @ 8001");

var server = ws.createServer(function (conn) {;
    console.log("New connection", ++connectionCount);
    conn.on("close", function (code, reason) {
        console.log("Connection closed")
    });
}).listen(8001)

This server takes 65k connections (as one port takes at the max 65k connections). How can I scale the server so that it can take more than 100k connections?

I recently opened three such servers with different ports and tried to do load balancing using nginx but to no avail, as the nginx server also could take 65k connections only. The nginx config is here

upstream localhost {
    server localhost:8001;
    server localhost:8002;
    server localhost:8003;
}


server {
    proxy_read_timeout 3600s;
    listen 8000;
    location / {
            proxy_pass http://localhost;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }
}

The port 8000 could take only 65k connections.

How are these kind of situations handled in the real time. I am an amateur here and need some good pointer to scale the server. The solution need not have to be using nginx. It can be anything. I just want to know, how to handle these kind of problems. Detail answers are appreciated. Thanks.

The limitation of 65535 ports only holds for outgoing connections, not for incoming connections.

The operating system uniquely identifies a connection/socket by these four properties:

  • source IP address
  • source TCP port destination
  • server IP address
  • server TCP port

So for a single IP address you Node.js or Nginx server can maintain at most 65535 connections. Across different client IPs, the only limitations are the CPU and RAM resources of your server.

Maybe you tested this behavior from a single client? In that case, you ran into the limitation of source TCP ports . Try testing from different clients and your server should work just fine with more than 65535 connections.

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