【问题标题】:How to forward request to Node.js from nginx with tcp_proxy_module?如何使用 tcp_proxy_module 从 nginx 向 Node.js 转发请求?
【发布时间】:2011-08-12 06:47:13
【问题描述】:

现在,我用nginx_tcp_proxy_module 修补了nginx,它在8080 端口上运行正常。

如何将客户端连接到 nignx 的 80 端口,而不是 Node.js 的 8080 端口, 让 nginx 将请求转发给 Node.js?

【问题讨论】:

标签: node.js nginx websocket socket.io


【解决方案1】:

只需将8080改为80即可。但是TCP和HTTP在同一个端口is not possible

替代解决方案:

  • 在端口 80 上使用 HAProxy
  • 设置 nginx 监听 81 端口
  • 在端口 8080 上运行您的 node.js 应用
  • HAProxy 配置为
    • 转发Host: your.nodejs.socketio.com到127.0.0.1:8080
    • 将其他所有内容转发到 127.0.0.1:81

如果您沿着这条路线走,您可能希望保留客户端 IP:

  • 配置HAproxy
  • 在 nginx 中使用RealIP module
  • 在 socket.io 中使用 X-Forwarded-For

    socketio.handshakeData = function(data) {
        var d = socketio.Manager.prototype.handshakeData(data);
        d.ip = data.request.headers['x-forwarded-for'] || data.request.connection.remoteAddress;
        return d;
    };
    

【讨论】:

  • 谢谢,现在 haproxy 已经成功将 http 请求转发到 nginx 和 websocket 请求到 nodejs(仅在 chrome 中),但无法转发 xhr-polling 和 jsonp-polling 请求(即和 firefox)
  • 删除acl is_websocket hdr(Upgrade) -i WebSocket
  • 感谢您的回答。节省了我很多时间:-)
猜你喜欢
  • 2019-08-29
  • 1970-01-01
  • 2018-09-06
  • 2019-04-08
  • 2019-06-25
  • 2014-04-16
  • 2013-02-13
  • 1970-01-01
  • 2020-02-02
相关资源
最近更新 更多