Node.js - 具有 WebSocket 代理的良好 Web 服务器SSL 支持吗?

发布于 2024-11-02 17:00:37 字数 272 浏览 1 评论 0原文

我真的很喜欢node.js,但是当你想运行多个websocket服务器并让它们都可以通过端口80访问时,它真的很复杂。

我目前正在运行nginx,但根据url将传入的websocket连接代理到不同的websocket服务器不可能,因为 nginx 不支持 http 1.1。

我尝试实现一个具有我自己的功能的网络服务器,但是当涉及到标头传递等时,它确实很复杂。另一件事是 SSL 支持。支持它并不容易。

那么,有谁知道做我提到的事情的好解决方案吗?

感谢您的帮助!

I really love node.js but it´s really complicating when you want to run multiple websocket servers and make them all accessible over port 80.

I'm currently running nginx, but proxying incoming websocket connections to the different websocket servers depending on the url is not possible because nginx does not support http 1.1.

I´ve tried to implement a webserver that has the functionality on my own, but it is really complicated when it comes to header passing etc. Another thing is SSL support. It´s not easy to support it.

So, does anyone know a good solution to do the things i mentioned?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-11-09 17:00:37

我使用nodejitsu的 node-http-proxy 取得了很好的结果。正如他们的自述文件中所述,他们似乎支持 WebSockets。

WebSocket 示例(取自 GitHub 自述文件):

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create an instance of node-http-proxy
//
var proxy = new httpProxy.HttpProxy();

var server = http.createServer(function (req, res) {
  //
  // Proxy normal HTTP requests
  //
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 8000
  })
});

server.on('upgrade', function(req, socket, head) {
  //
  // Proxy websocket requests too
  //
  proxy.proxyWebSocketRequest(req, socket, head, {
    host: 'localhost',
    port: 8000
  });
});

它的生产使用应该没有问题,因为它用于 nodejitsu.com。要将代理应用程序作为守护进程运行,请考虑使用 forever

I had good results using node-http-proxy by nodejitsu. As stated in their readme, they seem to support WebSockets.

Example for WebSockets (taken from their GitHub readme):

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create an instance of node-http-proxy
//
var proxy = new httpProxy.HttpProxy();

var server = http.createServer(function (req, res) {
  //
  // Proxy normal HTTP requests
  //
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 8000
  })
});

server.on('upgrade', function(req, socket, head) {
  //
  // Proxy websocket requests too
  //
  proxy.proxyWebSocketRequest(req, socket, head, {
    host: 'localhost',
    port: 8000
  });
});

It's production usage should be no problem since it is used for nodejitsu.com. To run the proxy app as a daemon, consider using forever.

老娘不死你永远是小三 2024-11-09 17:00:37

较新版本的 nginx 实际上将支持 http/1.1 的反向代理。您可能需要 1.1.7 或更高版本。

在你的配置中尝试这样的事情:

location / {
    chunked_transfer_encoding off;
    proxy_http_version 1.1;
    proxy_pass        http://localhost:9001;
    proxy_buffering   off;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host $host:9001;  #probaby need to change this
    proxy_set_header  Connection "Upgrade"; 
    proxy_set_header  Upgrade websocket;
}

这样做的好处是你可以在 nginx 上终止 SSL。

Newer versions of nginx actually will support reverse proxying for http/1.1. You probably want version 1.1.7 or greater.

Try something like this in your config:

location / {
    chunked_transfer_encoding off;
    proxy_http_version 1.1;
    proxy_pass        http://localhost:9001;
    proxy_buffering   off;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host $host:9001;  #probaby need to change this
    proxy_set_header  Connection "Upgrade"; 
    proxy_set_header  Upgrade websocket;
}

Nice thing about this is that you can terminate SSL at nginx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文