gpt4 book ai didi

node.js - 使用 NGINX 代理的多个 Node.js 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:42 28 4
gpt4 key购买 nike

目标:

在不同的文档根下使用多个彼此独立的实时 Node.js 服务器。

使用 NGINX

server {
server_name .lolwut1.com;
root /var/www/html/lolwut1;
# proxy pass to nodejs
location / {
proxy_pass http://127.0.0.1:5001/;
}
}

server {
server_name .lolwut2.com;
root /var/www/html/lolwut2;
# proxy pass to nodejs
location / {
proxy_pass http://127.0.0.1:5002/;
}
}

/var/www/html/lolwut1/app.js

var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("lolwut1\n");
});
server.listen(5001);

/var/www/html/lolwut2/app.js

var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("lolwut2\n");
});
server.listen(5002);

所以当我...

node app.js in /var/www/html/lolwut1/app.js 并点击 lolwut1.com 我全部好的。

问题:

  1. 但是现在如果我想启动第二个 Node 服务器怎么办?
  2. 这是一个不好的方法吗?...我是否以错误的方式思考这个问题?
  3. What are the advantages/disadvantages of using node.js with a connect.vhost directive as a router rather than NGINX?

最佳答案

  1. 使用forever启动和停止您的 Node 应用程序。
  2. 你做对了!很长一段时间以来,这种方法对我来说一直很有效。
  3. 连接虚拟主机优势:您不必安装和配置 nginx。整个堆栈是node.js。

    Nginx 优势: Nginx 是一个成熟、稳定的 Web 服务器。它不太可能崩溃或表现出奇怪的行为。它还可以托管您的静态站点、PHP 站点等。

    如果是我,除非我需要 Nginx 的某些特定功能,否则我会选择 Connect vhost 或 node-http-proxy为了拥有一个全 Node.js 堆栈。

关于node.js - 使用 NGINX 代理的多个 Node.js 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14993833/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com