在 Apache 中,如何配置到 socket.io 的子 URL 重定向?

在 Apache 中,如何配置到 socket.io 的子 URL 重定向?

我有一个简单的 node.js websocket 服务器:

var io = require('socket.io')(12345);

io.on('connection', function(socket){
  console.log('a user connected');
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });

  socket.on('test', function(msg){
    console.log('test msg: ' + msg);
    io.emit('test', 'Test answer');
  });
});

我连接到它,它使用 iocat 正确响应:

me@whatever:~/prj/client$ iocat --socketio ws://localhost:12345 -e test
> adsa
> [ 'Test answer' ]

所以现在我想配置 apache 服务器的 ssl 以将我的项目中的某个子 url 重定向到这个 websocket

我已经将 Apache 配置为在https://localhost/prj

我希望 websocket 可以在https://localhost/prj/socket

因此,我搜索了网络,找到了两种可能的方法来做到这一点:

1 – 仅使用 ProxyPass

<Location /prj/socket>
    ProxyPass ws://localhost:12345
    ProxyPassReverse ws://localhost:12345
</Location>

2- 带重写的 ProxyPass

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

         ProxyPass        /agora/socket/socket.io  ws://localhost:12345/socket.io
         ProxyPassReverse /agora/socket/socket.io  ws://localhost:12345/socket.io
</Location>

但这些都不起作用。连接已建立,但服务器没有响应:

me@whatever:~/prj/client$ iocat --socketio https://localhost/prj/socket -e teste
> asdfasd
> asdasd
> 

我获得此信息的页面:

https://github.com/socketio/socket.io/issues/1696

https://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel

日志中没有错误,只是无法正常工作。因此,增加了 Apache LogLevel 以查看规则是如何被重写的。我将其设置为LogLevel alert rewrite:trace6。之后,我在 Web 服务器连接中获得了此日志:

[Tue Aug 01 13:22:14.510992 2017] [rewrite:trace2] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:36004] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:14.511023 2017] [rewrite:trace3] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:36004] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:14.511029 2017] [rewrite:trace1] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:36004] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:14.922563 2017] [rewrite:trace2] [pid 28401] mod_rewrite.c(476): [client 127.0.0.1:36010] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:14.922607 2017] [rewrite:trace3] [pid 28401] mod_rewrite.c(476): [client 127.0.0.1:36010] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:14.922616 2017] [rewrite:trace1] [pid 28401] mod_rewrite.c(476): [client 127.0.0.1:36010] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:15.609710 2017] [rewrite:trace2] [pid 28403] mod_rewrite.c(476): [client 127.0.0.1:36014] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:15.609755 2017] [rewrite:trace3] [pid 28403] mod_rewrite.c(476): [client 127.0.0.1:36014] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:15.609766 2017] [rewrite:trace1] [pid 28403] mod_rewrite.c(476): [client 127.0.0.1:36014] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:15.950666 2017] [rewrite:trace2] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:36020] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:15.950714 2017] [rewrite:trace3] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:36020] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:15.950724 2017] [rewrite:trace1] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:36020] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:18.329478 2017] [rewrite:trace2] [pid 28399] mod_rewrite.c(476): [client 127.0.0.1:36024] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:18.329512 2017] [rewrite:trace3] [pid 28399] mod_rewrite.c(476): [client 127.0.0.1:36024] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:18.329519 2017] [rewrite:trace1] [pid 28399] mod_rewrite.c(476): [client 127.0.0.1:36024] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/
[Tue Aug 01 13:22:18.668606 2017] [rewrite:trace2] [pid 28424] mod_rewrite.c(476): [client 127.0.0.1:36030] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:22:18.668649 2017] [rewrite:trace3] [pid 28424] mod_rewrite.c(476): [client 127.0.0.1:36030] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:22:18.668658 2017] [rewrite:trace1] [pid 28424] mod_rewrite.c(476): [client 127.0.0.1:36030] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78520a0/initial] pass through /socket.io/

当我尝试发送消息时出现以下情况:

[Tue Aug 01 13:26:55.653675 2017] [rewrite:trace2] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:37454] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:26:55.653723 2017] [rewrite:trace3] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:37454] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:26:55.653734 2017] [rewrite:trace1] [pid 28400] mod_rewrite.c(476): [client 127.0.0.1:37454] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/
[Tue Aug 01 13:26:55.987130 2017] [rewrite:trace2] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:37460] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] init rewrite engine with requested uri /socket.io/
[Tue Aug 01 13:26:55.987173 2017] [rewrite:trace3] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:37460] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] applying pattern '^/agora/socket/(.*)' to uri '/socket.io/'
[Tue Aug 01 13:26:55.987182 2017] [rewrite:trace1] [pid 28402] mod_rewrite.c(476): [client 127.0.0.1:37460] 127.0.0.1 - - [localhost/sid#7f48e6578ae0][rid#7f48d78500a0/initial] pass through /socket.io/

据我所知,重写规则正在应用。有人知道为什么服务器没有响应吗?

任何帮助都值得感激。谢谢。

答案1

最简单的东西往往最难找到:)

我用第一个解决方案解决了这个问题(仅使用 ProxyPass):

<Location /prj/socket/>
        ProxyPass        http://localhost:12345/a
        ProxyPassReverse http://localhost:12345/
</Location>

唯一的问题是我使用的是 ws:// 协议。当我改用 http:// 时,它工作正常。感谢所有愿意看的人 :)

相关内容