【问题标题】:Configuring virtualhost for secure websocket using ratchet websocket library on an apache webserver在 apache 网络服务器上使用棘轮 websocket 库为安全 websocket 配置虚拟主机
【发布时间】:2020-07-26 00:27:39
【问题描述】:

我已经实现/尝试实现一个 websocket,用于在运行 linux 的 ec2 实例上与 apache webserver 上的用户之间进行通信。当我第一次配置它时,我让它工作,我的棘轮 websocket 指向端口 8081,没有任何 TLS。使用此配置,我能够升级到 websocket 并通过非安全 websocket 发送/接收数据。这只能通过 ip 地址实现,而不是通过实际的 url。 我正在一个子域中运行 websocket。

    <VirtualHost *:443>
        DocumentRoot "/var/www/html/video"
        ServerName video.domain.com

        SSLEngine on
        SSLCertificateFile ./certs/server.crt
        SSLCertificateKeyFile ./certs/server.key

#       ProxyPass /ratchet/ ws://video.domain.com:8081/

        <Directory "/var/www/html/video">
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>

当我使用基于 ip 的 websocket 连接通过 JS websocket API 连接到 websocket 时,上述解决方案有效。

我已经为 websocket API 尝试了 WSS、WS、带和不带端口等,但下面编写的代码仍然是我唯一可以开始工作的。

let socket = new WebSocket("ws://server_ip:8081");

我已经阅读了很多关于向 VH 添加 proxypass 的 stackoverflow 问题,但它并没有升级请求。此外,我尝试创建它自己的虚拟主机,但也不起作用。

我认为值得一提的是,我有一个通过代理请求的 cloudflare CDN。 希望能得到一些新鲜的眼睛。卡了一段时间。

【问题讨论】:

    标签: php apache websocket httpd.conf ratchet


    【解决方案1】:

    我没有足够的代表发表评论,所以回答它。

    我已经有一段时间没有涉足这个东西了,我的第一个想法是你确实需要一个 ProxyPass,但是当我查看我的配置时,情况并非如此。

    我很犹豫,我猜你的 VH 是这里的问题,你明确地在端口 443(https) 上监听,但我相信 wss 有另一个端口它在监听,所以也许你可以尝试另一个端口。除此之外,您还可以尝试执行new WebSocket('https://video.domain.com') 并在 VH 中启用代理,这样安全连接由 http 层处理。但是由于浏览器会尝试将请求升级到套接字,我怀疑这会起作用。

    我应该提到,在我的例子中,我使用 websockets 打开了一个 mqtt 连接,因为浏览器没有实现 mqtt,这是通过 wss 完成的。

    如果这些都不起作用,我可以尝试更深入地研究我使用的 mqtt 库的内部工作原理,以剖析连接是如何设置的。

    我希望这会有所帮助:D


    编辑

    由于 cmets 中没有足够的空间,我将其放在这里:

    与套接字无关,但与 apache 和代理有关:ProxyPass 指令有一个对应的 ProxyPassReverse 用于该目标。

    <virtualhost IPv4:443 [IPv6]:443>
            Servername knowledge.domain.com:443
            ServerAlias knowledge.domain.com
            ServerAdmin webmaster@domain.com
    
            DocumentRoot /path/to/documentRoot
    
            <Directory /path/to/documentRoot>
                    Options -Indexes -FollowSymLinks -SymLinksIfOwnerMatch
            </Directory>
    
            SSLEngine On
            SSLCertificateFile /path/to/ssl.crt
            SSLCertificateKeyFile /path/to/ssll.key
            SSLCACertificateFile /path/to/ssll.cer
    
            Header always set Strict-Transport-Security: "max-age=31536000; includeSubDomains; preload"
            Header always edit Set-Cookie (.*) "$1;HttpOnly;Secure"
    
            ProxyRequests Off
            ProxyPreserveHost On
            ProxyVia Full
            <Proxy *>
                    Require all granted
            </Proxy>
    
    
            <Location />
                    ProxyPass http://127.0.0.1:3000/
                    ProxyPassReverse http://127.0.0.1:3000/
            </Location>
    
            <Directory />
                    Options -FollowSymLinks -Indexes -SymLinksIfOwnerMatch
            </Directory>
    
            CustomLog "/path/to/logs/access.log" combined
            ErrorLog "/path/to/logs/error.log"
            LogLevel warn
    </virtualhost>
    
    

    这是我的 nodejs 应用程序代理配置示例

    【讨论】:

    • 嘿@chris,解决方案不仅是转发代理请求到websocket,还要反向。调查 mqtt 寿。与 java 配合得很好
    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 2019-12-29
    • 2019-07-02
    • 2012-07-05
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多