通过 Apache 提供的页面访问 socket.io 服务器

发布于 2024-12-04 02:58:05 字数 566 浏览 0 评论 0 原文

我希望这不会成为一个非常愚蠢的问题,但我正在学习如何为我的网站实现一个 socket.io 服务器来生成实时应用程序,但我的问题是我不知道如何在 Apache 服务环境中实施所述应用程序。目前,当我运行 node server.js 来启动我的 socket.io 服务器时,我必须通过访问 http://localhost:XXXX 来访问它,其中 XXXX 自然是我将其附加到的任何端口。我不希望我的网站被迫在这样的备用端口上查看,但我显然无法将服务器连接到端口 80,因为 Apache 正在侦听该端口。

显然,一个自然的解决方案是停止 Apache 服务,然后在端口 80 上节点服务器以避免冲突,但我不想牺牲 Apache 提供的所有功能。基本上,我想继续通过端口 80 上的 Apache 为我的网站提供服务,并通过端口 3000 上的 socket.io 集成实时应用程序的某些方面。

有没有办法避免我不想要的事情?这些事情是 1) 让用户在 URL 中使用 :3000 访问我的网站,2) 禁用 Apache,3) 使用 iframe。

提前致谢。

I hope this doesn't come across as a terribly silly question, but I'm learning how to implement a socket.io server for my website to produce real-time applications, but my problem is that I can't figure out how to implement said applications in an Apache served environment. Currently, when I run node server.js to start my socket.io server, I have to access it by visiting http://localhost:XXXX where XXXX is whatever port I attach it to, naturally. I don't want my website to be forced to be viewed on an alternate port like this, but I obviously can't attach the server to port 80 since Apache is listening on that.

Obviously a natural solution would be to stop the Apache service and then node the server on port 80 that way to avoid a collision, but I don't want to sacrifice all of the functionality that Apache offers. Basically, I want to continue to serve my website via Apache on port 80, and integrate certain aspects of real-time applications via socket.io on port 3000, let's say.

Is there a way to do this that avoid the things I don't want? Those things being 1) having users access my site with :3000 in the URL, 2) disabling Apache, 3) using iframes.

Thanks in advance.

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

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

发布评论

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

评论(2

谁的年少不轻狂 2024-12-11 02:58:05

一般来说,您应该能够使用 mod_proxy 隐藏 Node.js。一些搜索发现了这个: https:// /github.com/sindresorhus/guides/blob/master/run-node-server-alongside-apache.md (旧链接已失效,这是一个新链接)

但是,Socket.io 可能有点挑剔(https://github.com/LearnBoost/socket.io/issues/25),所以你可能会遇到具体的问题。

由于那张票有点旧了,还是值得一试。如果您遇到问题,请不要感到惊讶。之后你的下一个赌注是将 Node.js 绑定到端口 80 并让它充当 Apache 的反向代理 https://github.com/nodejitsu/node-http-proxy(仍在相当多的开发中)。

最佳解决方案是在它自己的服务器上运行它,然后让您的套接字流量转到socket.example.com 或类似的地方。

Generally, you should be able to hide Node.js with mod_proxy. A bit of searching turned up this: https://github.com/sindresorhus/guides/blob/master/run-node-server-alongside-apache.md (old link died, this is a new one)

However, Socket.io can be a bit finicky (https://github.com/LearnBoost/socket.io/issues/25), so you may have problems with it specifically.

As that ticket is a bit old, it's worth a shot. Just don't be surprised if you have problems. You're next bet after that is bind Node.js toport 80 and have it act as a reverse proxy for Apache with https://github.com/nodejitsu/node-http-proxy (still under a fair bit of development).

The optimal solution would be run it on it's own server and just have you're socket traffic go to socket.example.com or something like that.

别靠近我心 2024-12-11 02:58:05

Socket.io 有多种传输机制。如果您将 Apache 作为反向代理运行,其中一些功能将不起作用,但有些功能却可以。不工作的传输是 websocket 和 flash,但 xhr-polling 和 jsonp-polling 应该可以工作。

以下是为 socket.io 设置传输配置选项的示例:

var io = require("socket.io").listen(server);
io.set("transports", ["xhr-polling", "jsonp-polling"]);

在我的 Apache 上,我使用正常的 基于名称的虚拟主机和反向代理设置,通过这些传输,socket.io 似乎可以正常工作。

Socket.io has multiple transport mechanisms. Some of them don't work if you run Apache as reverse proxy, but some do. Transports that don't work are websocket and flash, but xhr-polling and jsonp-polling should work.

Here's an example on setting the transports configuration option for socket.io:

var io = require("socket.io").listen(server);
io.set("transports", ["xhr-polling", "jsonp-polling"]);

On my Apache I'm using the normal name based virtual hosts and reverse proxy setup and with these transports the socket.io seems to be working.

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