How to run different Node.js sites on the same server?

4

I have a Linux server and on this server I have several hosted sites, each with its own folder and files. I want to start using Node.js to create the APIs for each site. What is the best way to do this?

A process and port for each site or a single process using vhost, connect, and cluster? Apache? nginx?

    
asked by anonymous 12.11.2016 / 14:36

1 answer

1

You can use NGINX as a reverse proxy in front of NODEjs. Home In this approach you can run several processes with NODEJS, each on a port and set up a subdomain for each processes / API in NGINX, so it is only necessary to leave the standard HTTP port released on the server for external access.

Example:

Website: example.com Home API: api.example.com - > NODEJS on port 8888

SIte: example2.com Home API: api.example2.com - > NODEJS on port 9999


NGINX Configuration

server {
    listen 80;
    index index.html;

    server_name exemplo.com.br www.exemplo.com.br;
    root /usr/share/nginx/html/exemplo.com.br;

    location / {
            try_files $uri /index.html;
    }
}

server {
    listen 80;
    server_name api.exemplo.com.br;

    location / {
            proxy_pass http://127.0.0.1:8888;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
}

server {
    listen 80;
    index index.html;

    server_name exemplo2.com.br www.exemplo2.com.br;
    root /usr/share/nginx/html/exemplo2.com.br;

    location / {
            try_files $uri /index.html;
    }
}


server {
    listen 80;
    server_name api.exemplo2.com.br;

    location / {
            proxy_pass http://127.0.0.1:9999;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
}

link

    
14.11.2016 / 22:31