[英]Apache2 WebSockets reverse proxy on same URL
如何將Apache2配置為代理WebSocket連接(例如BrowserSync),如果它是在同一個URL上進行的,只有區別是標題“Upgrade:websocket”和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/”
我當前的配置:
ProxyRequests off
<Location />
ProxyPass http://127.0.0.1:3000/
ProxyPassReverse /
</Location>
mod_proxy,mod_proxy_http和mod_proxy_wstunnel已啟用。
回答自己。
使用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>
非常感謝metalim! 我在這里發布完整配置以供參考:
<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.