0

What is the best practice to handle virtual hosts in node.js?

I need to route domains to each individual http server..

http://api.localhost:8080 => localhost:9000
http://www.localhost:8080 => localhost:9001
https://secure.localhost:8080 => localhost:9002 // this request is HTTPS

I am using express http

Charles
  • 50,943
  • 13
  • 104
  • 142
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • [This question](http://stackoverflow.com/questions/8503841/virtual-hosting-with-standalone-node-js-server) might have the answers you're looking for. – Tim Brown Mar 12 '14 at 21:48
  • Can't get `http-proxy` to work http://stackoverflow.com/questions/22360318/node-http-proxy-route-domains-to-individual-http – clarkk Mar 12 '14 at 21:49
  • I use node-http-proxy and have some useful links in this post: http://stackoverflow.com/questions/12652863/nodejs-managed-hostings-vs-vps/12652947#12652947 – bryanmac Mar 12 '14 at 21:50

1 Answers1

1

It's very common to use nginx on port 80, and then define servers (vhosts) within nginx with a reverse proxy to your node servers. The reason it's so common is because nginx is exceptional at serving static content, so you let it do just that by telling it your public directory location.

Here's an example of a server (vhost) config. You would create one server { } block, and change the server_name for each vhost:

server {
  listen 80;
  server_name website.com;

  location / {
    proxy_pass http://127.0.0.1:3001;
  }
  location ~* ^.+\.(jpg|png|gif|woff|ico|map|js|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|flv|swf|html|htm)$ {
    root /home/empurium/code/davinci/public;
  }
}