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://?
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/"
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.
WebSocket over a Reverse Proxy. WebSocket communication can take place over any reverse proxy which is configured to perform forwarding at the transport layer. Some proxies are able to handle WebSocket communication from certain clients at the application layer.
WebSocket communication can take successfully take place in the presence of forward proxies, providing the client and proxy server have been configured properly to deal with it. This page explains how to configure a Universal Messaging JavaScript client and Apache serving as a forward proxy to permit WebSocket use.
In addition to being a "basic" web server, and providing static and dynamic content to end-users, Apache httpd (as well as most other web servers) can also act as a reverse proxy server, also-known-as a "gateway" server.
The wss protocol establishes a WebSocket over an encrypted TLS connection, while the ws protocol uses an unencrypted connection. At this point, the network connection remains open and can be used to send WebSocket messages in either direction.
Answering myself.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With