http-proxy-middleware
http-proxy-middleware copied to clipboard
Declare two different proxies with Websocket enabled does not work, or probably pathrewrite does not work
Actual behavior
If we declare two proxies where we enable WebSocket upgrade, see ProxyMiddlwareLocal and proxyMiddlewareStun0 the Websocket request for the second proxies is redirected two the first. On launch the server we got this mapping:
[HPM] Proxy created: / -> http://192.168.8.1/abc
[HPM] Proxy rewrite rule created: "/proxy" ~> "
[HPM] Proxy created: / -> https://tun0.bla.bla.com/abc
[HPM] Proxy rewrite rule created: "/proxy/tun0" ~> ""
But when we launch a WebSocket over http://loclahost:3000/proxy/tun0/wsapi/v1/sensorsws we got a handshake error and se this in the log
[HPM] Rewriting path from "/proxy/tun0/wsapi/v1/sensorsws" to "/tun0/wsapi/v1/sensorsws"
[HPM] GET /proxy/tun0/wsapi/v1/sensorsws ~> http://192.168.8.1/abc
[HPM] Upgrading to WebSocket
So you can see here /proxy/tun0/wsapi/v1/sensorsws is rewritten to /tun0/wsapi/v1/sensorsws but it should be without tun0, it should be only /wsapi/v1/sensorsws and at the end you can see the the request is redirected to http://192.168.8.1/abc but it should be https://tun0.bla.bla.com/abc
If i change the rewritePath to a function like that:
pathRewrite: function(path, req) {
console.log(path, path.replace('/proxy/tun0', ''));
return path.replace('/proxy/tun0', '');
},
I can see that the path is not /proxy/tun0/.. as expected but it starts with /tun0, but also If i change the replace path to pelace("/tun0', '') I have the same problem.
All this is only if I use the proxy for a websocket call, if i use a http request for /proxy/tun0 anything is working well.
And If I disable from first proxy the ws support, also anything is working as expected.
Setup
- "http-proxy-middleware": "0.17.4"
- "express": "4.15.4"
proxy middleware and server configuration
var express = require('express');
var proxy = require('http-proxy-middleware');
/* LOCALE */
var proxyMiddlewareLocal = proxy({
target: 'http://192.168.8.1/abc',
ws: true,
pathRewrite: {
'/proxy': ''
},
logLevel: 'debug'
});
/* STUN */
var proxyMiddlewareStun0 = proxy({
target: 'https://tun0.bla.bla.com/abc',
ws: true,
changeOrigin: true,
headers: {
Origin: 'https://tun0.bla.bla.com'
},
pathRewrite: {
'/proxy/tun0': ''
},
logLevel: 'debug'
});
var app = express();
app.use('/proxy/wsapi', proxyMiddlewareLocal);
app.use('/proxy/tun0', proxyMiddlewareStun0);
console.log("listening on port 3000");
var server = app.listen(3000);
Try using your target without the '/abc' path: target: 'https://tun0.bla.bla.com'
If I do something like that:
/* LOCAL */
var proxyMiddlewareLocal = proxy({
target: 'http://192.168.8.1',
ws: true,
pathRewrite: {
'/proxy': '/abc'
},
logLevel: 'debug'
});
/* STUN */
var proxyMiddlewareStun0 = proxy({
target: 'https://tun0.bla.bla.com',
ws: true,
changeOrigin: true,
headers: {
Origin: 'https://tun0.bla.bla.com'
},
pathRewrite: {
'/proxy/tun0': '/abc'
},
logLevel: 'debug'
});
Removing abc from target and adding it into the pathRewrite, the proxy is now sending the request to the right url which is https://tun0.bla.bla.com but the pathRewrite does still not rewrite the url correct.
/proxy/tun0/wsapi/v1/sensorsws is replaced by /orma/tun0/wsapi/v1/sensorsws so there is always only proxy is replaced by orma and tun0 is still there.
Some ideas?
Edit:
Another thing I discovered, if I never use first proxy, so my test starts on using second part with second proxy, the pathRewrite for second proxy is always working without issue.
Don't see /orma anywhere in the config you provided...
Sorry orma is the name in the real world, it is abc here ☺️
Can you help? I tested it again and I think there is a big bug on this, or not?
Can you provide a minimal working example? If it is a bug it should be reproducible.
Hi, honestly it takes me a lot of time to do that. Because I don't can share my servers with you, so I have to implement firstly a test server to do this. So probably this takes me away at least one day of work so I'm not sure if and when I can do this.
But I'll do my best.
Can you perhaps share some stupid express server which we can use for do this?