【问题标题】:Network proxy blocks socket.io网络代理阻止 socket.io
【发布时间】:2015-09-18 12:14:32
【问题描述】:

我在同一台服务器上使用nodejssocket.io 和 apache。 Apache 将请求从端口 80 重定向到 443,然后使用 SSLProxyEngine 间接重定向到端口 3000 上的 SSL-NodeJS 服务器 - 工作正常,

问题:

在某些地区,只允许使用 80 和 443 端口,不允许使用 3000 端口。

当我改变时:

io.connect('https://domain.com:3000', { path: '/socket.io' });

到这里:

io.connect('https://domain.com', { path: '/socket.io' });
io.connect('https://domain.com:443', { path: '/socket.io' });
// whatever ...

服务器无法访问 (net::ERR_CONNECTION_TIMED_OUT)。

我尝试将端口设置为 FTP (21) 等公共端口,但浏览器表示不允许使用此端口。

你有什么想法吗?

这里有一些文件:

Apache 虚拟主机:

<VirtualHost *:443>
    ServerName domain.com

    SSLProxyEngine On
    RequestHeader set Front-End-Https "On"
    ProxyPass / https://domain.com:3000/ retry=1 acquire=3000 timeout=600 Keepalive=On
    ProxyPassReverse / https://domain.com:3000/
    // localhost not working 

    SSLEngine on
    SSLCertificateKeyFile    ...
    SSLCertificateFile       ...
    SSLCertificateChainFile  ...

</VirtualHost>

(我用 domain.com 替换了我的域)

【问题讨论】:

  • 您找到解决方案了吗?

标签: node.js apache sockets proxy


【解决方案1】:

解决方案 工作得很好。

HTTP => com.domain.conf

<VirtualHost *:80>
    ServerName domain.com

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:3000/$1 [P,L]

    <Location />
        ProxyPass http://127.0.0.1:3000/
        ProxyPassReverse http://127.0.0.1:3000/
    </Location>

</VirtualHost>

HTTPS(使用 LetsEncrypt)=> com.domain-le-ssl.conf

<IfModule mod_ssl.c>

    <VirtualHost *:443>
        ServerName domain.com

        RewriteEngine On
        RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
        RewriteCond %{QUERY_STRING} transport=websocket    [NC]
        RewriteRule /(.*)           ws://localhost:3000/$1 [P,L]

        <Location />
            ProxyPass http://127.0.0.1:3000/
            ProxyPassReverse http://127.0.0.1:3000/
        </Location>

        SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
</IfModule>

client.js

var url = window.location.href;
var domain = url.split("/")[0] + "//" + url.split("/")[2];

var socket = io.connect(domain, { path: '/socket.io' });

socket.on('hello', function(callback) {
    callback(); // confirm receiving "hello"
});

app.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var port = process.env.PORT || 3000;
var io = require('socket.io')(server);

server.listen(port, function(err) {
    console.log('HTTP-Server started on port ' +     port);
});

app.use(express.static(__dirname + '/public'));

io.on('connection', function(socket) {

    console.log('connection: ' + socket.id);

    socket.emit('hello', function(res) {
        console.log('Client received "hello"');
    });

    socket.on('disconnect', function(exception) {
        console.log('disconnect: ' + socket.id);
    });

});

提示:如果您尚未安装防火墙,请安装并阻止端口 3000(或您正在使用的端口)。

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2021-12-02
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    相关资源
    最近更新 更多