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

2019-01-21 19:49发布

问题:

  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?

回答1:

  • http://code.google.com/p/pywebsocket/
  • What popular webservers have support for HTML5 WebSocket?
  • https://serverfault.com/questions/201825/how-would-i-configure-a-websocket-server-to-run-alongside-a-webserver

Another possibility is to use mod_proxy in apache to redirect the requests to a websocket server.



回答2:

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(){...});
})


回答3:

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:

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



回答5:

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


回答6:

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.