Apache2 无法设置标头“连接”和“升级”

Flo*_*oel 2 apache websocket

我正在努力通过反向代理设置 websocket 连接。我终于有了 Nginx 的工作配置,但我更喜欢使用 Apache2。

这些是 Nginx 中所需的配置行:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
Run Code Online (Sandbox Code Playgroud)

我尝试将它们翻译为 Apache2,但似乎不起作用:

<If "%{HTTP:upgrade} == 'websocket'">
  RequestHeader add Upgrade "websocket"
</If>
RequestHeader set Connection "upgrade"  
Run Code Online (Sandbox Code Playgroud)

Apache2 只是忽略这些命令。当我更改标头名称时,它们不再被忽略,但这对我没有帮助:

RequestHeader set X-Connection "upgrade"
Run Code Online (Sandbox Code Playgroud)

所以:Apache2 似乎忽略了“Upgrade”和“Connection”标头的更改。

(我正在使用一个调试服务器,它打印收到的所有 HTTP 请求及其所有标头 - 因此我可以直接比较 Nginx 和 Apache2 请求 - 因此我知道 Apache2 正在忽略我的命令。)

我如何才能在 Apache2 中实现此功能?

Flo*_*oel 8

我自己找到了一个解决方案:Apache 不允许设置这些标头 - 相反,您需要使用重写引擎:

RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:3000/$1 [P,L]
Run Code Online (Sandbox Code Playgroud)

然后 Apache 将自动添加正确的“升级”和“连接”标头。