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

Asked

Viewed 951 times

4

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

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

  • 1

    I understand that the simplest thing is to check the Host: do request, do not need to install anything else, it is mere input condition. If you need much more complexity, better change technology..

1 answer

1

you can use NGINX as a reverse proxy in front of Nodejs.

In this approach you can run several processes with NODEJS, each in a port and configure in NGINX a subdomain for each process/API, so you only need to leave the standard HTTP port free on the server for external access.

Example:

Site: example.com.br
API: api.exemplo.com.br -> NODEJS at port 8888

Website: exemplo2.com.br
API: api.exemplo2.com.br -> NODEJS on port 9999


Configuration of the NGINX

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;
    }
}

http://www.devmedia.com.br/usando-nginx-como-proxy-reverso-e-diminuindo-o-consumo-do-servidor/21461

  • But I’m going to use this solution to work with multiple sites, so several ports. Isn’t it a little impractical to use so many ports? Find this solution better than using a single process and handle which app to use by dns?

  • The multiplicity of ports will only be a problem if you run more than 50,000 sites on the same computer, and this is certainly not a good idea (50,000 instances of Node.js should use a lot of memory...)

  • I understand, I’m definitely not gonna run more than 50,000 websites. In your opinion, epx, it is better to bet on a process for each site than on a single process managing everything?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.