S
S
Svyatoslav Khusamov2015-10-27 16:49:57
Node.js
Svyatoslav Khusamov, 2015-10-27 16:49:57

How to run multiple web applications under node.js at the same time?

Let's say I created one web application. And put it on port 80.
How can I now run a second web application on the same server?
It must be on the same port, i.e. 80th.
So that both applications respond to their domain names.
The problem is further complicated by the limitation of RAM:
when there are 100 sites, there will be 100 copies of node.js in RAM. Now one application takes 60-80MB, it will need 8GB of RAM ... There is no such RAM.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2015-10-27
@khusamov

It is necessary to raise nginx and create two domains in it, each will proxy its own applications. Nod applications will have to be moved from port 80 to something unoccupied (each to its own). An example config for one such application:

upstream app1 {
    server 127.0.0.1:3003; # порт, который слушает приложения
}

server {
    listen 80;

    # черновики для разных серверов, раcкомментить нужный по необходимости
  
    # home box
    #set $apppath /home/hogart/projects/app1;
    #server_name app1.local;

    # beta
    #set $apppath /home/hogart/app1;
    #server_name app1.kitmanov.name;

    # production
    #set $apppath /home/hogart/app1;
    #server_name app1.info;


    root $apppath;

    # раздача статики
    location ~ /style|js|img/ {
        root $apppath/public/;
        gzip on;
        gzip_static on;
        gzip_types text/css application/x-javascript;
        gzip_proxied no-store no-cache private expired auth;
    }

    location / {
        proxy_pass http://app1; # из upstream (см. выше)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr; # удалить, если приложению не нужен реальный IP юзера
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for; # удалить, если приложению не нужен реальный IP юзера
        proxy_cache_bypass $http_upgrade;
    }
}

S
Super User, 2015-10-27
@sergeystepanov1988

I agree with Konstantin Kitmanov , you need to put nginx in front of the node and run each node process on its port. From the outside, only nginx on port 80 will be visible, but inside node processes can have any port.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question