简体   繁体   English

Apache2 WebSockets在相同的URL上反向代理

[英]Apache2 WebSockets reverse proxy on same URL

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://? 如何将Apache2配置为代理WebSocket连接(例如BrowserSync),如果它是在同一个URL上进行的,只有区别是标题“Upgrade:websocket”和URL schema ws://?

For example: 例如:

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
...

All examples I find, redirect some path only, like "<Location /ws>..." or "ProxyPass /ws/ ws://example.com/" 我找到的所有示例,仅重定向某些路径,例如“<Location / ws> ...”或“ProxyPass / ws / ws://example.com/”

My current config: 我当前的配置:

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

mod_proxy, mod_proxy_http and mod_proxy_wstunnel are enabled. mod_proxy,mod_proxy_http和mod_proxy_wstunnel已启用。

Answering myself. 回答自己。

Using RewriteEngine, hint given by this post , and WebSocket handshake specification: 使用RewriteEngine, 这篇文章给出的提示和WebSocket握手规范:

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>

Thanks a lot to metalim! 非常感谢metalim! I publish the full configuration here for reference: 我在这里发布完整配置以供参考:

<VirtualHost *>
    ServerName example.org
    ServerAlias *.example.org
    ServerAdmin sysadmin@example.org
    ProxyRequests Off
    ProxyPreserveHost On

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

    ProxyPass / http://192.168.1.1/ retry=1
    ProxyPassReverse / http://192.168.1.1/

    ErrorLog /var/log/apache2/example.org.error.log
    CustomLog /var/log/apache2/example.org.access.log combined
</VirtualHost>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM