【问题标题】:NodeJs Error during WebSocket handshakeWebSocket握手期间的NodeJs错误
【发布时间】:2014-04-12 19:54:28
【问题描述】:

我能够建立从 clienJS 到 NodeJs 的 websocket 连接。但是当请求通过apache httpd时无法连接websocket。

使用 Httpd2.4.7,我收到以下错误。请让我知道需要更正的地方。

WebSocket 连接到“ws://172.27.38.86/socket.io/1/websocket/_uW8Sv7lgQfrZncTSzKu”失败:WebSocket 握手期间出错:意外响应代码:502

感谢和问候

贾瓦哈尔

【问题讨论】:

  • 错误日志说什么?你的 httpd 配置是什么?
  • 收到 502 响应代码错误,并且它没有命中节点 js 服务器。

标签: node.js apache


【解决方案1】:

我必须在 CentOs 中设置 ws 隧道,其中默认安装了 apache 2.2.15。

我尝试用 apache 2.2.15 修补 proxy_wstunnel 模块。但没有帮助。最后我决定按照http://httpd.apache.org/docs/2.4/install.html的官方文档删除apache 2.2.15并安装apache 2.4

安装后(我已经安装在默认位置/usr/local/apache2/),我做了以下的隧道工作

  1. 从 /usr/local/apache2/conf/httpd.conf 中取消注释以下行

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

  2. 添加用于隧道 websocket 请求的虚拟主机


<VirtualHost *:80>
    ServerName subdomain.mydomain.com
    ProxyPass / ws://localhost:8081/
    ProxyPassReverse / ws://localhost:8081/
</VirtualHost>

从我的前端,websocket 通过 ws://subdomain.mydomain.com:80 连接

  1. 使用重启 apache sudo /usr/local/apache2/bin/apachectl -k 重启

【讨论】:

    【解决方案2】:

    在 2.4.5 之后,apache 在其主干上包含一个名为 proxy_wstunnel 的模块,但在当前 ubuntu 生产版本的 apache (2.4.7) 上尚不可用。这有点痛苦,但大致按照on this blog-post 列出的步骤,我设法安装了模块并成功使用它。

    dpkg -s apache2 //this gives you the version of apache in my case "2.4.7-1ubuntu4.1"
    //then you checkout that version of apache
    svn co http://svn.apache.org/viewvc/httpd/httpd/tags/2.4.7/
    //you get into the directory just checked out
    cd 2.4.7
    //in there you checkout the Apache Portable Runtime Project and utils
    svn co http://svn.apache.org/repos/asf/apr/apr/branches/1.4.x srclib/apr
    svn co http://svn.apache.org/repos/asf/apr/apr-util/branches/1.3.x srclib/apr-util
    //you compile with the corresponding modules flags
    ./buildconf
    ./configure --enable-proxy=shared --enable-proxy_wstunnel=shared
    make
    
    //You copy the modules (mod_proxy and mod_proxy_wstunnel) to your apache working copy
    //It could be advisable to backup the old mods first
    sudo cp modules/proxy/.libs/mod_proxy{_wstunnel,}.so /usr/lib/apache2/modules/
    sudo chmod 644 /usr/lib/apache2/modules/mod_proxy{_wstunnel,}.so
    sudo echo -e "# Depends: proxy\nLoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so" | sudo tee -a /etc/apache2/mods-available/proxy_wstunnel.load
    
    //you then enable your module and restart your apache... so now the module is ready to use
    sudo a2enmod proxy_wstunnel
    sudo service apache2 restart
    

    毕竟,研究了一下我发现this page我相应地配置了我的apache vhost文件

    <VirtualHost *:80>
        ServerAdmin yourmail@mail.com
        ServerName yoursubdomain.yourdomain.info
        Redirect permanent / https://yoursubdomain.yourdomain.info
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerAdmin yourmail@mail.com
        ServerName yoursubdomain.yourdomain.info
    
        ProxyRequests off
    
            <Proxy *>
                Order deny,allow
                Allow from all
            </Proxy>
    
        ProxyPreserveHost On
    //these next two lines are to enable the wstunnel
        ProxyPass /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
        ProxyPassReverse /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
    //this line is to retrieve the socket.io.js to use
        ProxyPass /socket.io/ http://localhost:9091/socket.io/
    
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/yourcert.crt
        SSLCertificateKeyFile /etc/ssl/private/yourcert.key
        SSLCertificateChainFile /etc/ssl/certs/your_bundle.crt
    
        //your logs
        CustomLog /var/log/apache2/yoursubdomain.yourdomain.info.log combined
        ErrorLog /var/log/apache2/yoursubdomain.yourdomain.info.error.log
    </VirtualHost>
    

    还有 TaDaaa...我有一个工作节点 websocket 通过 apache 代理在端口 9091 上响应,该代理通过 443 标准 ssl 端口获取所有内容。

    我想 ubuntu 很快就会在他们的生产版本中包含这个模块,但同时这是要走的路

    【讨论】:

      【解决方案3】:

      Apache 在通过代理处理 websocket 方面很糟糕。我建议要么去掉 Apache 层,要么修改你的 socket.io 设置以使用 XHR 轮询。

      【讨论】:

      • 我尝试了以下配置,它不起作用 /n #ProxyPass /socket.io/ 172.27.38.93:9090/socket.io #ProxyPassReverse /socket.io/ 172.27.38.93:9090/socket.io #ProxyPass /ws/ ws:/ /172.27.38.93:9090/ #ProxyPassReverse /ws/ ws://172.27.38.93:9090/ #ProxyPass /socket.io/1/websocket ws://172.27.38.93:9090/socket.io/1/websocket # ProxyPassReverse /socket.io/1/websocket ws://172.27.38.93:9090/socket.io/1/websocket
      猜你喜欢
      • 2016-11-30
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2019-07-01
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多