WebSockets反向代理

WebSockets反向代理

本文介绍了在相同URL上的Apache2 WebSockets反向代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将Apache2配置为代理WebSocket连接(例如,BrowserSync),如果它是在相同的URL上进行的,则仅区别是标头"Upgrade:websocket"和URL架构ws://?

How to configure Apache2 to proxy WebSocket connection (BrowserSync for example), if it's made on the same URL, with only difference being header "Upgrade: websocket" and URL schema ws://?

例如:

HTTP request:
GET http://example.com/browser-sync/socket.io/?... HTTP/1.1
...

WebSocket request:
GET ws://example.com/browser-sync/socket.io/?... HTTP/1.1
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
...

我找到的所有示例都仅重定向某些路径,例如< Location/ws> ..."或"ProxyPass/ws/ws://example.com/"

All examples I find, redirect some path only, like "<Location /ws>..." or "ProxyPass /ws/ ws://example.com/"

我当前的配置:

ProxyRequests off
<Location />
    ProxyPass http://127.0.0.1:3000/
    ProxyPassReverse /
</Location>

mod_proxy,mod_proxy_http和mod_proxy_wstunnel已启用.

mod_proxy, mod_proxy_http and mod_proxy_wstunnel are enabled.

推荐答案

回答自己.

使用RewriteEngine,这篇文章给出的提示以及WebSocket握手规范:

Using RewriteEngine, hint given by this post, and WebSocket handshake specification:

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

ProxyRequests off
<Location />
    ProxyPass http://127.0.0.1:3000/
    ProxyPassReverse /
</Location>

这篇关于在相同URL上的Apache2 WebSockets反向代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:07