是否可以将端口 80 用于 HTTP 和 Web 套接字流量?

发布于 2024-11-06 18:39:02 字数 352 浏览 0 评论 0原文

  1. 我正在构建一个使用 Web 套接字(技术上是 Flash 套接字)的网站,以提供实时通信。
  2. 我希望能够为公司/学术防火墙后面的人员提供支持,这些防火墙会阻止除端口 80 之外的所有内容
  3. 我希望能够在单台计算机上运行该网站

以前,我一直使用 Apache 与一些 python 结合进行 HTTP 服务在高编号套接字上侦听 websocket 内容,但这显然在这里不起作用。

我总是可以将 websocket 内容移至单独的服务器,但我想避免支付第二个 VPS 的费用(并且必须通过网络而不是本地与数据库通信)。有没有一个好的方法可以做到这一点(nodejs,nginx,..?),还是不值得头痛?

  1. I'm building a site that uses web sockets (technically Flash sockets) in order to provide real-time communication.
  2. I want to be able to support people behind corporate/academic firewalls that block everything except port 80
  3. I'd like to be able to run the site off of a single machine

Previously, I've been using Apache for HTTP serving combined with some python listening on a high-numbered socket for the websocket stuff, but that obviously won't work here.

I can always move the websocket stuff to a separate server, but I'd like to avoid paying for a second VPS (and have to talk to the database over the network instead of locally). Is there a good way to do this (nodejs, nginx, ..?), or is it not worth the headache?

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

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

发布评论

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

评论(6

迷爱 2024-11-13 18:39:02

是的,通过使用 node.jsExpressconnect 用于 HTTP 文件服务,socket.io 用于 WebSocket 内容。

例子:

var express = require("express");
var app = express.createServer(); 

app.get('/', function(req, res){ 
    res.redirect("/index.html");
}); 

app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});

app.listen(80); 

var io = require('socket.io'); 
var socket = io.listen(app); 
socket.on('connection', function(client){ 
  client.on('message', function(){...});
})

YES, by using node.js. Express or connect for the HTTP file serving and socket.io for the WebSocket stuff.

Example:

var express = require("express");
var app = express.createServer(); 

app.get('/', function(req, res){ 
    res.redirect("/index.html");
}); 

app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});

app.listen(80); 

var io = require('socket.io'); 
var socket = io.listen(app); 
socket.on('connection', function(client){ 
  client.on('message', function(){...});
})
假装不在乎 2024-11-13 18:39:02

当然你可以这样做。

首先你必须检查你的 Apache 版本。您应该有 2.4+ 版本。我将向您展示 Ubuntu 14.4 上我的服务器的命令。

其次,打开必要的 apache 模块:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

打开您的域的conf,在我的例子中,这是文件的路径:

/etc/apache2/sites-available/myDomain.pl.conf

接下来,附加此代码

<VirtualHost>

.
.
.

RewriteEngine on

RewriteCond %{QUERY_STRING} transport=polling
RewriteRule /(.*)$ http://localhost:3001/$1 [P]

ProxyRequests off
ProxyPass /socket.io ws://localhost:3001/socket.io
ProxyPassReverse /socket.io ws://localhost:3001/socket.io

ProxyPass        /socket.io http://localhost:3001/socket.io
ProxyPassReverse /socket.io http://localhost:3001/socket.io
</VirtualHost>

最后重新启动您的 apache

service apache2 restart

玩得开心!

Of course you can do this.

Firstly you have to check your Apache version. You should have 2.4+ version. I will show you command for my server on Ubuntu 14.4.

Secondly, turn on necessary apache modules:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

Open conf of your domain, in my case that was a path to file:

/etc/apache2/sites-available/myDomain.pl.conf

Next, append this code

<VirtualHost>

.
.
.

RewriteEngine on

RewriteCond %{QUERY_STRING} transport=polling
RewriteRule /(.*)$ http://localhost:3001/$1 [P]

ProxyRequests off
ProxyPass /socket.io ws://localhost:3001/socket.io
ProxyPassReverse /socket.io ws://localhost:3001/socket.io

ProxyPass        /socket.io http://localhost:3001/socket.io
ProxyPassReverse /socket.io http://localhost:3001/socket.io
</VirtualHost>

Finaly restart your apache

service apache2 restart

Have fun!

栀子花开つ 2024-11-13 18:39:02

对于安全的 websocket,我需要做的就是将这三行添加到我的 https virthualhost :

<VirtualHost *:443>

    [...https config...]

    SSLProxyEngine On
    ProxyPass "/web_socket"  "wss://localhost:7300/web_socket"
    ProxyPassReverse "/web_socket"  "wss://localhost:7300/web_socket"

</VirtualHost>

并启用 mod_proxy_wstunnel apache 模块(我正在运行 apache 2.4.10):

a2enmod proxy_wstunnel

For secure websocket, all I needed to do was to add these three lines to my https virthualhost :

<VirtualHost *:443>

    [...https config...]

    SSLProxyEngine On
    ProxyPass "/web_socket"  "wss://localhost:7300/web_socket"
    ProxyPassReverse "/web_socket"  "wss://localhost:7300/web_socket"

</VirtualHost>

And enable the mod_proxy_wstunnel apache module (I'm running apache 2.4.10) :

a2enmod proxy_wstunnel
攀登最高峰 2024-11-13 18:39:02

如果可以选择 Java 服务器,GlassFish(和 grizzly)在同一端口上支持 HTTP 和 Websocket 流量。

GlassFish (and grizzly) support both HTTP and websockets traffic on the same port if a java server is an option.

腹黑女流氓 2024-11-13 18:39:02

简而言之,答案是否定的,因为您只能让一个进程在一个端口上侦听。您可以尝试使用端口 443,因为只要您也不使用 https,该端口也不会被阻止。

The short answer is no, because you can only have one process listening on one port. You could try using port 443, since that is not going to be blocked either, as long as you also do not use https.

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