Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS multiple websites

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

like image 981
Brandon Wilson Avatar asked Feb 22 '16 23:02

Brandon Wilson


1 Answers

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 [email protected]
    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 [email protected]
    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>
like image 103
Kirill Fuchs Avatar answered Oct 22 '22 21:10

Kirill Fuchs