Socket.io与Apache配合使用

4

我在Node.js和socket.io方面缺乏经验,因此可能会问一些愚蠢的问题,首先对此致歉。

我正在尝试完成以下内容:

  1. Installed node on ubuntu where I have apache also installed.
  2. Created virtual host in apache and set it as proxy to node. My conf file looks like:

    <VirtualHost *:80>
        ServerAdmin giorgi@omedia.ge
        ServerName node.aidemo.info
        ServerAlias www.node.aidemo.info
    
        ProxyRequests off
        <Proxy *>
           Order deny,allow
           Allow from all
        </Proxy>
    
        <Location />
           ProxyPass http://127.0.0.1:8080
           ProxyPassReverse http://127.0.0.1:8080
        </Location>
    
    </VirtualHost>
    
  3. Have created simple js file for server (first server example in socket.io website) and started server from cli with command: node server.js. It starts perfectly and listens to 8080

  4. Created another virtualhost where I put clientside index.html (also from first example in socket.io). At first I had problem (and actually main problem is this), browser couldn't resolve path /socket.io/socket.io.js. Then I went to the url (http://localhost:8080/socket.io/socket.io.js) from lynx locally from terminal, downloaded that js and put locally with virtualhost near index.html. After this, browser could resolve that request, but I have error when socket.io.js itself is trying to get the url:

    http://localhost:8080/socket.io/1/?t=1347623348836
    

你有什么想法可以解决这个问题吗? 我的主要目标是拥有一个web url,通过它可以访问我的node服务器,并使用socket.io与其通信 - 例如创建非常简单的聊天。

我希望我表达清楚了。 谢谢所有尝试帮助我的人。


可能与http://serverfault.com/questions/290121/configuring-apache2-to-proxy-websocket重复。 - Ted Shaw
我的问题与具体问题有关:解决从Apache请求socket.io.js文件的问题。 - Jibla
3个回答

5
我是一位有用的助手,可以为你翻译文本。
我正在使用express + socket.io,并且它们在端口3001上监听。我想要http://example.com/folder重定向到我的Express应用程序,该应用程序在端口3001上监听(即到服务器端http://localhost: 3001)。
我已经完成了以下工作。
.html文件如下:
<script src='/folder/socket.io/socket.io.js'> </script>  
<script>  
var socket = io.connect('http://example.com', {resource: 'folder/socket.io'});  
...  
</script>  

我的apache2配置如下:

ProxyPreserveHost On
ProxyPass /folder/ http://localhost:3001/
ProxyPassReverse /folder/ http://localhost:3001/

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>

<Location /folder/>
    allow from all
</Location>

请注意,它需要启用模块proxy_http。要启用它,请运行此命令:
sudo a2enmod proxy_http; service apache2 restart

我使用这种方法时遇到了握手失败,HTTP代码为502,有什么线索吗? - Vicary

1
如果您将socket.io.js文件放在与index.html文件相同的本地位置,这并不能解决问题,因为您可能没有在main.js文件中更改socket变量中的url,请查看:
var socket = io.connect();

在你的 main.js/index.html(脚本)中,替换为:
var socket = io.connect(httpprotocol+hostname+httpport);

我的代码源如下:

var socket = io.connect('https://192.168.43.187:8443/');

0

http://localhost:8080显然对服务器外部的任何内容都不可用。

客户端JavaScript的io.connect()应该连接到http://node.aidemo.info,以便Apache可以将其发送到Node。

如果您已经打开了端口8080,则http://node.aidemo.info:8080也可能有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接