Javascript 如何在多个域中安装多个nodejs应用程序?

Javascript 如何在多个域中安装多个nodejs应用程序?,javascript,node.js,nginx,express,Javascript,Node.js,Nginx,Express,在这段时间里,我读的比以往任何时候都多,这将是我的第一个网页,所以我决定挂载在nodejs上。我很快就制作了这个应用程序,并在localhost:9000中进行了测试 所以我想在VPS上运行更多的应用程序,我搜索信息,我有两个选择 首先使用nginx代理应用程序 upstream example1.com { server 127.0.0.1:3000; } server { listen 80; server_name www.example1.com; rewrite ^/(

在这段时间里,我读的比以往任何时候都多,这将是我的第一个网页,所以我决定挂载在nodejs上。我很快就制作了这个应用程序,并在localhost:9000中进行了测试

所以我想在VPS上运行更多的应用程序,我搜索信息,我有两个选择

首先使用nginx代理应用程序

upstream example1.com {
    server 127.0.0.1:3000;
}

server {
listen   80;
server_name  www.example1.com;
rewrite ^/(.*) http://example1.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example1.com;
    access_log /var/log/nginx/example1.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://example1.com;
      proxy_redirect off;
    }
 }
我不理解这个配置文件,因为我从来没有使用过nginx,所以我搜索第二个选项

使用expressjs()中的vhost

我正在使用expressjs,我知道如何配置,但有一些问题是关于wich是最好的选择,因为使用express()我有一个应用程序管理多个应用程序,所以我认为这不是一个好做法,也是一种资源浪费

大卫·埃利斯在《邮报》上说

如果您不需要使用WebSockets(或者任何HTTP 1.1特性),您可以使用NginX作为代理

优点是NginX可以处理的总负载比Node更高(基本上是静态编译的,专门用于这类事情),但是您失去了流式传输任何数据的能力(一次发送较小的数据块)

对于较小的站点,或者如果您不确定将来需要什么功能,那么最好还是坚持使用node http proxy,并且只有在您能够证明代理是服务器上的瓶颈时才切换到NginX。幸运的是,如果您以后确实需要NginX,那么设置NginX并不困难

从帖子中,我读了一个用很多应用程序配置xginx的例子,但我不知道如何使用它

upstream example1.com {
    server 127.0.0.1:3000;
}

server {
listen   80;
server_name  www.example1.com;
rewrite ^/(.*) http://example1.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example1.com;
    access_log /var/log/nginx/example1.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://example1.com;
      proxy_redirect off;
    }
 }

upstream example2.com {
    server 127.0.0.1:1111;
}

server {
listen   80;
server_name  www.example2.com;
rewrite ^/(.*) http://example2.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example2.com;
    access_log /var/log/nginx/example2.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://example2.com;
      proxy_redirect off;
    }
 }
所以问题是,使用nginx还是使用vhost,哪个是最好的选择

如果我必须使用nginx,那么有没有教程可以教我如何配置nginx来为node js上的许多应用提供服务


tnx all

您的Nginx配置示例似乎就是您要寻找的。您应该在/etc/nginx/sites available下创建配置文件,然后为要启用的配置文件创建一个符号链接,以启用/etc/nginx/sites


也许这会对您有所帮助-

它工作正常。。。使用一个app.js,但我如何添加另一个app,你能修改我的一个文件来做一个例子吗,我的第一个app是localhost:9000我的第二个app是localhost:8000i不知道如何配置两个app。。。只是,我从来没有使用过nginx,所以我不知道如何配置,可能是通过一个我可以练习的示例tnxy我们的示例似乎是正确的,除非您尝试提供关于它到底如何不起作用的其他信息,否则没有人能帮到您
upstream example1.com {
    server 127.0.0.1:3000;
}

server {
listen   80;
server_name  www.example1.com;
rewrite ^/(.*) http://example1.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example1.com;
    access_log /var/log/nginx/example1.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://example1.com;
      proxy_redirect off;
    }
 }

upstream example2.com {
    server 127.0.0.1:1111;
}

server {
listen   80;
server_name  www.example2.com;
rewrite ^/(.*) http://example2.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example2.com;
    access_log /var/log/nginx/example2.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://example2.com;
      proxy_redirect off;
    }
 }