8

I have been searching the Internet for a week now trying to find a good solution for this. First I read about http-master but I have been having issues installing it. Then I saw a few others that didn't have the features and ease of configurations. Or the answers were all outdated

We want to develop website in NodeJS, currently have three, two for test/dev and one for production. Each of these are hosted on the same server, currently an Apache server with the same domain name but different ports. We have another website we are working on as well with a different domain name.

Question is, what is the best way to use NodeJS to serve all the sites and more? Going forward we want to develop all applications in NodeJS.

Server Specs: Windows Server 2012 R2 Two Cores 4 GB RAM

Kirill Fuchs
  • 13,446
  • 4
  • 42
  • 72
Brandon Wilson
  • 4,462
  • 7
  • 60
  • 90
  • I'm looking at VirtualHost definitions and mod_proxy directives to assign ports where node is running, but that is in a LAMP environment. Not sure about Windows... – Bosworth99 Feb 22 '16 at 23:48
  • Possible duplicate of [How do I host multiple Node.js sites on the same IP/server with different domains?](http://stackoverflow.com/questions/19254583/how-do-i-host-multiple-node-js-sites-on-the-same-ip-server-with-different-domain) which already has some great answers for multiple site strategies using Node.js. – Dwayne Charrington Feb 22 '16 at 23:49
  • @DigitalSea it is a close, but my server currently runs Windows and Apache. It would be nice to use what I already have in place. The accepted answer demonstrated examples of how to accomplish it. – Brandon Wilson Feb 23 '16 at 02:03

2 Answers2

10

You want to use a reverse proxy. nginx is a good choice because configuration is so simple. But the same can also be achieved with apache.

Basically you just run each application under a different port, and based on domain name proxy over requests. Here's an example setup using nginx.

## Upstream www.example.com ##
upstream examplelive  {
      server 8081; # nodejs server running on port 8081
}

## Upstream dev.example.com ##
upstream exampledev  {
      server 8082; # nodejs server running on port 8082
}

## www.example.com ##
server {
    listen       80;
    server_name  www.example.com;

    access_log  /var/log/nginx/log/www.example.access.log  main;
    error_log  /var/log/nginx/log/www.example.error.log;

    ## send request back to examplelive ##
    location / {
     proxy_pass  http://examplelive;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}


## dev.example.com ##
server {
   listen      80;
   server_name dev.example.com;

   access_log  /var/log/nginx/log/dev.example.com.access.log  main;
   error_log   /var/log/nginx/log/dev.example.com.error.log;

   location / {
        proxy_pass  http://exampledev;
        proxy_set_header        Host            static.example.com;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

The below apache example should work.

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName www.example.com

    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:8081/
            ProxyPassReverse http://localhost:8081/
    </Location>

</VirtualHost>
<VirtualHost *:80>
    ServerAdmin admin@dev.example.com
    ServerName  dev.example.com    

    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:8082/
            ProxyPassReverse http://localhost:8082/
    </Location>

</VirtualHost>
Kirill Fuchs
  • 13,446
  • 4
  • 42
  • 72
0

My preferred way of hosting multiple domains has always been to use vhost with Express, which allows you to match different domain names.

var mailapp = connect()
var staticapp = connect()

// create main app
var app = connect()

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

If you don't want to go through the hassle of getting multiple domains pointing to this server, I find that using multiple sub domains on one primary name (e.g. foo1.bar.com, foo2.bar.com and using a CNAME record with your DNS to point to those subdomains is very effective.

Jack Guy
  • 8,346
  • 8
  • 55
  • 86