Is it possible to use port 80 for both HTTP and we

2019-01-21 19:28发布

  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?

6条回答
一纸荒年 Trace。
2楼-- · 2019-01-21 19:54

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

查看更多
The star\"
3楼-- · 2019-01-21 19:59

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!

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-21 20:01

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.

查看更多
Root(大扎)
5楼-- · 2019-01-21 20:05

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
查看更多
劫难
7楼-- · 2019-01-21 20:19

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(){...});
})
查看更多
登录 后发表回答