Single node.js http server accepting connections on multiple hostnames

537 views
Skip to first unread message

Tomi

unread,
Mar 12, 2011, 5:02:53 AM3/12/11
to nodejs
Hi folks. Is it possible to create single http server in node.js which
will accept connections on multiple hostnames? For example: I have a
number of subdomains (x1.domain.com, x2.domain.com, ... xn.domain.com)
bound to an IP address and I want node.js program to accept
connections only on x1.domain.com and x2.domain.com. Thanks for your
time and help.

This question is also at stackoverflow:

http://stackoverflow.com/questions/5262127/single-node-js-http-server-accepting-connections-on-multiple-hostnames

Marcel Laverdet

unread,
Mar 12, 2011, 1:57:59 PM3/12/11
to nod...@googlegroups.com
Nope. Host information is sent after a connection is established. You will want to put a lighttpd or nginx proxy in front of node if this is a problem.


--
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.


Tim Caswell

unread,
Mar 12, 2011, 8:25:07 PM3/12/11
to nod...@googlegroups.com
Or you can use node as the proxy and have it reverse proxy out to another server for the domains it doesn't want.  But yes, hostname is at the http header level and the port/IP and/or interface to bind to is much lower in the stack.

aaronblohowiak

unread,
Mar 12, 2011, 10:50:21 PM3/12/11
to nodejs
buuuuuut you can have different dns entries specify different IP
addresses for different subdomains

On Mar 12, 5:25 pm, Tim Caswell <t...@creationix.com> wrote:
> Or you can use node as the proxy and have it reverse proxy out to another
> server for the domains it doesn't want.  But yes, hostname is at the http
> header level and the port/IP and/or interface to bind to is much lower in
> the stack.
>
> On Sat, Mar 12, 2011 at 10:57 AM, Marcel Laverdet <mar...@laverdet.com>wrote:> Nope. Host information is sent after a connection is established. You will
> > want to put a lighttpd or nginx proxy in front of node if this is a problem.
>
> > On Sat, Mar 12, 2011 at 7:02 PM, Tomi <bosak.to...@gmail.com> wrote:
>
> >> Hi folks. Is it possible to create single http server in node.js which
> >> will accept connections on multiple hostnames? For example: I have a
> >> number of subdomains (x1.domain.com, x2.domain.com, ... xn.domain.com)
> >> bound to an IP address and I want node.js program to accept
> >> connections only on x1.domain.com and x2.domain.com. Thanks for your
> >> time and help.
>
> >> This question is also at stackoverflow:
>
> >>http://stackoverflow.com/questions/5262127/single-node-js-http-server...

Isaac Schlueter

unread,
Mar 13, 2011, 1:32:38 PM3/13/11
to nod...@googlegroups.com

Right, so, another way to do this would be to have multiple network interfaces with different IP addresses, and bind each node server to one of them. But that's not "virtual hosts" anymore, its just "hosts", and is generally more pricey. :)

--i

Jann Horn

unread,
Mar 13, 2011, 4:44:30 PM3/13/11
to nod...@googlegroups.com
Am Samstag, den 12.03.2011, 02:02 -0800 schrieb Tomi:
> Hi folks. Is it possible to create single http server in node.js which
> will accept connections on multiple hostnames? For example: I have a
> number of subdomains (x1.domain.com, x2.domain.com, ... xn.domain.com)
> bound to an IP address and I want node.js program to accept
> connections only on x1.domain.com and x2.domain.com. Thanks for your
> time and help.

Would you be happy with sending an "access denied" or letting the
connection collapse when the hostname is wrong?

Jann

signature.asc

Tomi

unread,
Mar 16, 2011, 6:12:33 PM3/16/11
to nodejs
In my case it is acceptable, however more suitable solution in general
would be probably to use dedicated IP address for selected subdomains
which needs to be served by node.js http server.
>  signature.asc
> < 1KViewDownload

Mike Repass

unread,
Mar 16, 2011, 3:58:25 PM3/16/11
to nod...@googlegroups.com, bosak...@gmail.com
Tomi - is it acceptable that your node.js server listens on all vhosts (and accepts connections on all of them), but immediately closes the connection or sends some appropriate error status if the request host is NOT x1 or x2?

In other words, accept all connections but reject those asking for hosts not on a whitelist?

I believe this is what Jann is suggesting below, I'm only pinging the thread since I got the accept on stack overflow and want to make sure we get the right answer :-)
Reply all
Reply to author
Forward
0 new messages