使用 Apache 和 Socket.io 的代理 WebSocket 连接

Proxy WebSocket connection using Apache & Socket.io

我在通过 Apache (2.4.6) 代理我的 WebSocket 连接 (Socket.io) 时遇到问题。我正在尝试使用 Socket.io 问题 https://github.com/Automattic/socket.io/issues/1696 中描述的设置,但我的开发人员工具中不断出现错误,表明在 WebSocket 握手期间连接失败:

WebSocket connection to 'ws://example.domain.com/socket.io/?EIO=3&transport=websocket&sid=KPd7VBy4Yi7mj-wAAABN' failed: Error during WebSocket handshake: 'Connection' header value must contain 'Upgrade'

发生这种情况是因为 Connection header 被设置为 Close 而不是 Upgrade

我已经确认 proxy_wstunnel_module 模块已加载,当我查看跟踪日志时,我可以看到它响应了一些请求,但我仍然收到上述错误。我绝对不是 Apache 专家,所以我认为我的 httpd.conf 文件中有些地方不正确(我尝试使用 Apache 文档确认所有行。

<VirtualHost *:80>

        ServerName example.domain.com
        ServerAlias example.domain.com

        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
        Header set Access-Control-Allow-Headers "content-type,x-requested-with"

        RewriteEngine on
        RewriteRule ^/$ /otherApp/home [PT]

        RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
        RewriteCond %{QUERY_STRING} transport=polling      [NC]
        RewriteRule /(.*)           http://localhost:3000/ [P,L]

        <Directory />
                Options All
                Order Deny,Allow
                Allow from all
        </Directory>

        ProxyRequests off

        <Proxy *>
                Order Deny,Allow
                Allow from all
        </Proxy>

        <Location />
                Order Deny,Allow
                Allow from all
        </Location>

        ProxyPass /socket.io            ws://localhost:3000/socket.io
        ProxyPassReverse /socket.io     ws://localhost:3000/socket.io

        ProxyPass /AnotherApp ajp://localhost:8009/location
        ProxyPass /AnotherApp2  ajp://somewhere.else:8009/location2

        DocumentRoot /opt/appThings
        <Directory /opt/appThings>
                Options +Indexes
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>

有没有人在我上面的 httpd.conf 文件中看到任何会导致此问题的明显内容?

如果需要其他相关信息,请告诉我。我查看了日志,但我不完全确定什么是相关的/我需要寻找什么。

嗯,我不完全确定答案是否正确,因为我的另一位同事是我的另一个同事想出来的 - 我也不确定他是否完全理解答案。

我认为我们发现的 mod_rewrite 模块版本存在错误。我们必须使用建议的补丁编译该模块的源代码,这样代理才能正常工作。

似乎还有某种 "header stripping" 请求通过防火墙。似乎正在剥离 Connection: Upgrade header 这显然导致我们的连接失败。

所以虽然我没有我想要的那么多细节,但希望这可以给其他人一些线索,让他们知道将来要寻找什么。