VHOST-Proxy

57 views
Skip to first unread message

Axel Kittenberger

unread,
May 29, 2012, 3:06:02 AM5/29/12
to nodejs
Hi list, yet another question (I know its getting much in the last days :-).

Suppose I want to host a second legacy website with Apache-PhP on my
VPS otherwise running awesome node.js (not my blog, its yet something
else). So I check request.headers.host and if its the VHOST for the
legacy website, I want to proxy that request to say localhost port
8080 running the Apache.

Any code snippets to properly and compactly proxy GET as well POST requests?

I've seen node-http-proxy, but its Documentation only explains howto
set up a standalone (reverse)proxy rather than integrate it to an
existing node.js server occupying the port 80.

Oliver Leics

unread,
May 29, 2012, 3:14:38 AM5/29/12
to nod...@googlegroups.com
var httpProxy = require('http-proxy')
;

httpProxy.createServer(options, function(req, res, proxy) {
if(req.headers.host === 'whatever.example.com') {
proxy.proxyRequest(req, res,
{ host: 'localhost'
, port: '8080'
}
)
return
}
res.writeHead(400, {})
res.write('Bad Request: Missing "Host"')
res.end()
})

- o
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en



--
Oliver Leics @ G+
https://plus.google.com/112912441146721682527

Axel Kittenberger

unread,
May 29, 2012, 3:25:25 AM5/29/12
to nod...@googlegroups.com
Thank you.

The thing that confuses me is, why httpProxy.createServer(...);

Cant I just create http.createServer() handle requests normally for
the main node site, and in my request handler create a proxyRequst
only when it relates the VHOST?

Marak Squires

unread,
May 29, 2012, 3:48:48 AM5/29/12
to nod...@googlegroups.com
Yes. https://github.com/nodejitsu/node-http-proxy/tree/master/examples
-- 
Marak Squires
Co-founder and Chief Evangelist
Nodejitsu, Inc.

Axel Kittenberger

unread,
May 29, 2012, 4:16:43 AM5/29/12
to nod...@googlegroups.com
No. All of the examples proxy through httpProxy.createServer

Oliver Leics

unread,
May 29, 2012, 5:03:07 AM5/29/12
to nod...@googlegroups.com
var http = require('http')
, httpProxy = require('http-proxy')
, proxy = new httpProxy.RoutingProxy()
;

http.createServer(function(req, res) {
if(req.headers.host === 'whatever.example.com') {
proxy.proxyRequest(req, res,
{ host: 'localhost'
, port: '8080'
}
)
return
}

res.writeHead(400)
res.end('Bad Request: Unknown "Host"')
}).listen(80)

- o

Axel Kittenberger

unread,
May 29, 2012, 5:24:07 AM5/29/12
to nod...@googlegroups.com
Awesome, thanks!

Oliver Leics

unread,
May 29, 2012, 6:39:05 AM5/29/12
to nod...@googlegroups.com
BTW: httpProxy.createServer(...) can proxy websocket-requests. And it
supports ssl connections.

Marak Squires

unread,
May 30, 2012, 3:04:07 PM5/30/12
to nod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages