Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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://?

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.

like image 453
metalim Avatar asked Apr 22 '15 08:04

metalim


People also ask

Do WebSockets work through reverse proxy?

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.

Can WebSocket be proxied?

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.

Can Apache reverse proxy?

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.

What is the difference between WS and WSS?

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.


1 Answers

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>
like image 80
metalim Avatar answered Sep 19 '22 19:09

metalim