http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

Declare two different proxies with Websocket enabled does not work, or probably pathrewrite does not work

Open mburger81 opened this issue 8 years ago • 7 comments

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);

mburger81 avatar Aug 22 '17 10:08 mburger81

Try using your target without the '/abc' path: target: 'https://tun0.bla.bla.com'

chimurai avatar Aug 22 '17 11:08 chimurai

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.

mburger81 avatar Aug 22 '17 12:08 mburger81

Don't see /orma anywhere in the config you provided...

chimurai avatar Aug 22 '17 12:08 chimurai

Sorry orma is the name in the real world, it is abc here ☺️

mburger81 avatar Aug 22 '17 13:08 mburger81

Can you help? I tested it again and I think there is a big bug on this, or not?

mburger81 avatar Aug 24 '17 10:08 mburger81

Can you provide a minimal working example? If it is a bug it should be reproducible.

chimurai avatar Aug 24 '17 15:08 chimurai

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?

mburger81 avatar Aug 28 '17 14:08 mburger81