简体   繁体   English

配置Apache 2.4.7代理socket.io

[英]Configure Apache 2.4.7 for proxying socket.io

On our server we are running Apache already on port 80, on this same server I want to serve socket.io and I would like to connect to the socket.io server also on port 80. So I tried to get Apache to proxy port 80. I found this stackoverflow entry: https://serverfault.com/questions/616370/configuring-apache-2-4-mod-proxy-wstunnel-for-socket-io-1-0 在我们的服务器上,我们已经在端口80上运行Apache,在我要为socket.io提供服务的同一台服务器上,我也想在端口80上连接到socket.io服务器。因此,我尝试将Apache代理到端口80我发现了这个stackoverflow条目: https : //serverfault.com/questions/616370/configuring-apache-2-4-mod-proxy-wstunnel-for-socket-io-1-0

What I did: 我做了什么:

I enabled the next modules in apache: 我在apache中启用了以下模块:

a2enmod rewrite proxy proxy_wstunnel

I created a new site in '/etc/apache2/sites-available/mysite.conf', with the next content: 我在“ /etc/apache2/sites-available/mysite.conf”中创建了一个新站点,其中包含以下内容:

<VirtualHost subdomain.mydomain.com:80>
    Servername subdomain.mydomain.com

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

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

On the same server I have the socket.io server running on port 3001: 在同一台服务器上,我在端口3001上运行socket.io服务器:

server.listen('3001', function(){
  console.log('Socket.io server listening on *:3001');
});


io.listen(server).on('connection', function(client) {
    // Rest of the code
}

And the socket.io clients are connecting on this subdomain configured into apache: 并且socket.io客户端正在配置为apache的此子域上进行连接:

var socket = io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}});

Once I startup a client. 一旦启动客户端。 I don't see anything coming into the socket.io server logging (I have added a lot of console.log() entries. All I see in the console of the client is the next error: 我没有看到任何信息进入socket.io服务器日志记录(我添加了很多console.log()条目。我在客户端控制台中看到的是下一个错误:

GET http://subdomain.mydomain.com:8100/socket.io/?extra_info=fe4567&EIO=3&transport=polling&t=1416987210079-31 net::ERR_CONNECTION_TIMED_OUT

If I connect the clients directly to port 3001 of the socket.io server http://subdomain.mydomain.com:3001 all is just working fine (when I open port 3001 on the apache server). 如果我将客户端直接连接到socket.io服务器http://subdomain.mydomain.com:3001端口3001,则一切工作正常(当我在apache服务器上打开端口3001时)。

I am running the next versions: 我正在运行下一个版本:

  • Apache: 2.4.7 阿帕奇:2.4.7
  • Socket.io server & client: 1.2.1 Socket.io服务器和客户端:1.2.1

EDIT: 编辑:

When I change the connection URL in var socket = io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}}); 当我在var socket = io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}});更改连接URL时var socket = io.connect('http://subdomain.mydomain.com/', {query: {extra_info : extra_info}}); to http://subdomain.mydomain.com:80 . http://subdomain.mydomain.com:80

I receive a different error: 我收到另一个错误:

GET http://subdomain.mydomain.com/socket.io/?user_id=2&EIO=3&transport=polling&t=1418122378582-40 
(index):1 XMLHttpRequest cannot load http://subdomain.mydomain.com/socket.io/?extra_info=sdferr&EIO=3&transport=polling&t=1418122378582-40. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.178.158:8100' is therefore not allowed access. The response had HTTP status code 500.

I though setting the Access-Control-Allow-Origin header in Apache would solve the issue, but it doesn't. 我虽然在Apache中设置Access-Control-Allow-Origin标头可以解决此问题,但事实并非如此。 I have added this to the file /etc/apache2/sites-available/mysite.conf : 我已将此添加到文件/etc/apache2/sites-available/mysite.conf

Header set Access-Control-Allow-Origin "*"

based on our comments, here is some thing you may try, not for sure. 根据我们的评论,这是您可以尝试做的一些事情,不确定。

not sure how to pass both port and extra_info query, but the idea is to set port. 不知道如何同时传递port和extra_info查询,但是想法是设置端口。

io.connect('http://subdomain.mydomain.com/', {port: 80})

or this 或这个

io.connect('http://subdomain.mydomain.com:80/', {query: {extra_info : extra_info}})

good luck. 祝好运。

There where 2 things going wrong. 哪里有2件事出了错。 First of all, you need to specify port 80 in the client to be able to connect to port 80. So I configured the client to connect as follows: 首先,您需要在客户端中指定端口80才能连接到端口80。因此,我将客户端配置为如下所示:

io.connect('http://subdomain.mydomain.com:80/', {query: {extra_info : extra_info}})

After this change you will get several CORS issues. 进行此更改后,您将遇到几个CORS问题。 The only thing I needed to change is how the client JS file is retrieved. 我唯一需要更改的是如何检索客户端JS文件。 In my AngularJS application I included the socket.io client JS file as follows: 在我的AngularJS应用程序中,我包含了socket.io客户端JS文件,如下所示:

<script src="https://cdn.socket.io/socket.io-1.2.1.js"></script>

I needed to change this to: 我需要将其更改为:

<script src="http://subdomain.mydomain.com/socket.io/socket.io.js"></script>

It seems that the socket.io client is also served automatically by your socket.io server. 似乎socket.io客户端也由您的socket.io服务器自动提供服务。 If you retrieve the client JS from your own server you will get no CORS issues. 如果您从自己的服务器检索客户端JS,则不会出现CORS问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM