【发布时间】:2019-05-25 01:49:18
【问题描述】:
我们有一个使用 SocketIO 运行并在 heroku 上集群的 NodeJS 应用程序。为了让 SocketIO 工作,我们使用这里讨论的 redis-adapter:https://socket.io/docs/using-multiple-nodes/。
然后我们实现了粘性会话,如此处的粘性会话文档所示:https://github.com/elad/node-cluster-socket.io。
原来当我们部署到 Heroku 时,connection.remoteAddress 在:
// Create the outside facing server listening on our port.
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
// We received a connection and need to pass it to the appropriate
// worker. Get the worker for this connection's source IP and pass
// it the connection.
var index = worker_index(connection.remoteAddress, num_processes);
var worker = workers[index];
worker.send('sticky-session:connection', connection);
}).listen(port);
实际上是一些heroku 路由服务器的IP 地址,而不是客户端IP。我已经看到可以使用请求头“x-forwarded-for”来获取客户端IP,但是当我们以这种方式暂停连接时,我们甚至还没有请求头?
【问题讨论】:
标签: node.js heroku sticky-session