【问题标题】:Comfiguring Apache for socket.io and SSL / WSS为 socket.io 和 SSL / WSS 配置 Apache
【发布时间】:2017-02-25 21:19:47
【问题描述】:

正如标题所示,我正在尝试让 Apache 和 Socket.io (node.js) 运行良好,尤其是在 SSL 上。目前,https://www.example.com 的客户端应用程序使用 Socket.io over SSL 连接到 [SSL 协议]://socket.example.com/socket.io/?query_stuff* 的服务器。与 wss:// 的连接总是失败,因此 Socket.io 降级为 https://,这样可以正常工作。但我想利用 websocket 协议,而不是依赖于通过 http(s) 进行轮询。

Linux 和 Apache

服务器版本: Apache/2.4.7 (Ubuntu)

相关模组: mod_proxy、mod_proxy_http、mod_proxy_wstunnel、mod_rewrite、mod_ssl

Iptables:我打开了 80、443 和 9000 端口。

虚拟主机:

我在 *:443 上创建了一个名为 socket.example.com 的虚拟主机。它的预期目的是反向代理 [wss,https]://socket.example.com/ 以指向运行在 http://localhost:9000/ 的 socket.io 服务器。就是这样,去掉了多余的部分:

<VirtualHost *:443>
    ServerName socket.example.com
    DocumentRoot /var/www/example/socket.io/

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

    ## I have also tried the following RewriteRules: ##
    # RewriteRule /(.*)         http://localhost:9000/$1 [P,L]
    # RewriteRule /(.*)         http://localhost:9000/socket.io/$1 [P,L]

    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/www.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/keys/0001_key-certbot.pem
    SSLCACertificateFile  /etc/letsencrypt/ca-bundle.pem

    SSLProxyEngine On
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    ProxyPass / http://localhost:9000/
    ProxyPassReverse / http://localhost:9000/
</VirtualHost>

通过 SSL 使用 PHP websockets 成功

在为我的 websocket 服务器切换到 node.js 之前,我使用上面的 Apache VirtualHost 成功地将 wss://socket.example.com/ws_daemon.php 路由到 ws:/ /localhost:9000/ws_daemon.php。在这种情况下,我 1. 删除了重写规则并 2. 将 ProxyPass 设置更改为:

ProxyPass / ws://localhost:9000/
ProxyPassReverse / ws://localhost:9000/

但同样的逻辑似乎并没有延续到 socket.io。

在这一点上,我已经没有想法了。任何帮助将不胜感激!

【问题讨论】:

    标签: node.js apache socket.io wss proxypass


    【解决方案1】:

    如果您使用的是&lt;Location&gt; 块,则应在其中添加以下行:

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule /socket.io/(.*) ws://localhost:3000/socket.io/$1 [P]
    

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 1970-01-01
      • 2018-08-04
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多