Proxying websockets to multiple backends doesn't work
See original GitHub issueIs this a bug report?
Yes
Steps to reproduce
- Set up a proxy server with
app.use(
'/sockjs-node',
createProxyMiddleware('/sockjs-node', {
target: 'http://127.0.0.1:3000',
ws: true,
})
);
app.use(
'/socket.io',
createProxyMiddleware('/socket.io', {
target: 'http://127.0.0.1:4000',
ws: true,
})
);
The /sockjs-node
server can be quickly spun up with create-react-app
since that is the socket for Webpack hot reload. /socket.io
can be quickly spun up with a socket.io example server.
- Load up the React app in your browser. The
/sockjs-node
requests are not proxied correctly. The HPM log output shows that it is trying to proxy the/sockjs-node
request to port 4000, resulting in an ECONNREFUSED error.
Setup
- http-proxy-middleware: 1.0.5
- http-proxy-middleware configuration: see above
- server: express 4.17.1
client info
Chrome (latest) / Ubuntu 20.04 (Linux)
target server info
One is a webpack dev server, one is a simple express server with socket.io config
Reproducible Demo
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:14
Top Results From Across the Web
nginx proxy websocket to multi nodes - Stack Overflow
I use nginx as proxy server, backend are socketio services. ... to different backend server, have tried hash directive but doesn't work
Read more >NGINX double proxy not allowing websocket connections
When I only had one proxy I was able to make successful websocket connections, but after moving to two all websocket upgrade requests...
Read more >Improve reverse proxy, automatic handling of websockets
Fortunately it is possible to handle websocktets automatically by using the rewrite enginge: E.g. Reverse proxy xy.mydomain host to xy-backend.
Read more >How to Reverse Proxy Websockets with Apache 2.4 - Serverlab -
WebSockets were introduced to open two-way interactive ... send traffic to multiple backend servers, the proxy Load Balancer module is used.
Read more >Using NGINX as a WebSocket Proxy
For enterprise production use, where multiple WebSocket servers are needed for performance and high availability, a load balancing layer ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I found that if I change
to
Then the issue is fixed. No ideas yet as to why. However, I think this fix results in worse performance, because express is now going to be running that proxy middleware on every request, even ones that don’t match
/sockjs-node
. It might be OK though if you’re only using this for local development.You just saved my day with your fix. Spent 3 hours because I had this exact problem.
When proxying to several websocket servers Chrome was showing errors like “Invalid frame header” or “WebSocket connection failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1” which googling them up led to absolute non-sense.
I think this issue should really be patched up.