0

I want to setup a basic proxy like the one browser-sync has, for example I want to go to web1.domain1.xyz and it should proxy to web1.domain.xyz (so it's anything.domain1.xyz to anything.domain.xyz)

I got this part working already :

// HTTP Proxy 
var simpleHttp       = require("http");
var simpleHttpProxy  = require("http-proxy");
var simpleProxy      = simpleHttpProxy.createServer();

simpleHttp.createServer(function(req,res){
    var target = 'http://'+req.headers.host.replace(/domain1.xyz|domain2.xyz|domain3.xyz/gi,'domain.xyz');
    simpleProxy.web(req, res, {
        rewriteRules: true,
        xfwd: true,
        toProxy: true,
        changeOrigin: true,
        hostRewrite: true,
        autoRewrite: true,
        protocolRewrite: true,
        target: target
    });
}).listen(4000);

I would also like to be able to point multiple domains at it, for example domain1.com domain2.com domain3.com.

However it doesn't replace the links, I don't know how browser-sync does this, I've been studying https://github.com/BrowserSync/browser-sync/blob/master/lib/server/proxy-server.js and https://github.com/BrowserSync/browser-sync/blob/master/lib/server/proxy-utils.js

I see that they use a custom function to replace the links, however I didn't succeed in implementing their logic.

XkiD
  • 159
  • 1
  • 14

2 Answers2

0

I'm not sure how to do what you're asking for with http-proxy but I'm almost certain it can be done with express-vhost. There's even a suitable example in the README. I've always found express-vhost to be very light and functional.

    var connect = require('connect')
    var serveStatic = require('serve-static')
    var vhost = require('vhost')

    var mailapp = connect()

    // add middlewares to mailapp for mail.example.com

    // create app to serve static files on subdomain
    var staticapp = connect()
    staticapp.use(serveStatic('public'))

    // create main app
    var app = connect()

    // add vhost routing to main app for mail
    app.use(vhost('mail.example.com', mailapp))

    // route static assets for "assets-*" subdomain to get
    // around max host connections limit on browsers
    app.use(vhost('assets-*.example.com', staticapp))

    // add middlewares and main usage to app

    app.listen(3000)
leesio
  • 731
  • 3
  • 13
0

Edit: i thought you had a working example. Didn't understand the question correctly :-/ read further down:

You can chain the replace calls

// HTTP Proxy 
var simpleHttp       = require("http");
var simpleHttpProxy  = require("http-proxy");
var simpleProxy      = simpleHttpProxy.createServer();

var domains          = ['domain1.xyz','domain2.xyz','domain3.xyz'];

simpleHttp.createServer(function(req,res){

    // Calling replace for every entry in domains       
    var domain = req.headers.host;
    for (var i in domains) {
        domain = domain.replace('/'+domains[i]+'/gi','domain.xyz');
    }
    var target = 'http://'+domain;

    simpleProxy.web(req, res, {
        rewriteRules: true,
        xfwd: true,
        toProxy: true,
        changeOrigin: true,
        hostRewrite: true,
        autoRewrite: true,
        protocolRewrite: true,
        target: target
    });
}).listen(4000);

if you want to proxy to different servers you can make a dictionary and replace to that value

//Proxy domain1 => domain2, and domain3 => domain4
var domains = {'domain1.xyz':'domain2.xyz','domain3.xyz':'domain4.xyz'}
[...]

for (var i in domains) {
    domain = domain.replace('/'+i+'/gi',domains[i]);
}

edit: Replacing the links you really have to replace the links. Browser-sync uses Regex to parse the links out of the content and replace the link with a simple string.replace. The functions to look here is (https://github.com/BrowserSync/browser-sync/search?utf8=%E2%9C%93&q=rewriteLinks)

nv1t
  • 458
  • 2
  • 9
  • Hello, thanks for the reply, however the rewrite links part is actually the answer I need as I did succeed in making it work with multiple domains, I simply added them to `var target = 'http://'+req.headers.host.replace(/domain1.xyz|domain2.xyz|domain2.xyz/gi,'domain.xyz');` – XkiD May 25 '16 at 11:32