我已经在服务器上设置了Node.js和Nginx。现在我想用它,但是在我开始之前有两个问题:

How should they work together? How should I handle the requests? There are 2 concepts for a Node.js server, which one is better: a. Create a separate HTTP server for each website that needs it. Then load all JavaScript code at the start of the program, so the code is interpreted once. b. Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.

我不清楚如何正确使用Node.js。


当前回答

Nginx可以作为一个反向代理服务器,就像一个项目经理。当它得到一个请求时,它会分析它并将请求转发给上游(项目成员)或处理它自己。Nginx有两种处理请求的方式。

服务请求 将请求转发到另一个服务器 服务器{ server_name mydomain。示例sub.mydomain.example; 位置/ { proxy_pass http://127.0.0.1:8000; 主机$ Host; proxy_pass_request_headers; } 位置/静态/ { 别名/我/静态/文件/路径; } }

服务器请求

使用此配置,当请求URL为 Mydomain.example /static/myjs.js返回myjs.js文件 / /静态/文件/文件夹路径。当你配置Nginx服务时 静态文件,它处理请求本身。

将请求转发到另一个服务器

当请求URL是mydomain时。Nginx将转发 请求到http://127.0.0.1:8000。上运行的服务 Localhost 8000端口将接收请求并返回响应 Nginx返回客户端的响应。

当你在8000端口上运行node.js服务器时,Nginx将把请求转发给node.js。编写node.js逻辑并处理请求。你让你的nodejs服务器在Nginx服务器后面运行。

如果你想运行nodejs之外的其他服务,只需要在不同的端口上运行另一个服务,比如Django, flask, PHP,然后在Nginx中配置它。

其他回答

带有Nginx配置的Node.js。

$ sudo nano /etc/nginx/sites-available/subdomain.your-domain.example

添加以下配置,这样当我们从subdomain.your_domain.example来的时候,Nginx作为代理将流量重定向到端口3000

upstream subdomain.your-domain.example {
  server 127.0.0.1:3000;
}
server {
  listen 80;
  listen [::]:80;
  server_name subdomain.your-domain.example;
  access_log /var/log/nginx/subdomain.your-domain.access.log;
  error_log /var/log/nginx/subdomain.your-domain.error.log debug;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://subdomain.your-domain.example;
    proxy_redirect off;
  }
}

你也可以使用node.js生成静态文件到nginx提供的目录中。当然,站点的一些动态部分可以由node提供,而另一些则由nginx提供(静态)。

使用nginx提供的一些服务可以提高你的性能。

你也可以在一个服务器配置中为应用程序设置不同的url:

yourdomain。example/app1/* ->到本地运行的Node.js进程 http://127.0.0.1:3000 yourdomain。example/app2/* ->到Node.js进程 本地运行http://127.0.0.1:4000

在/etc/nginx/sites-enabled / yourdomain:

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.example;

    location ^~ /app1/{
        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://127.0.0.1:3000/;
    }

    location ^~ /app2/{
        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://127.0.0.1:4000/;
    }
}

重启Nginx:

sudo service nginx restart

启动应用程序。

节点app1.js

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello from app1!\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

节点app2.js

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello from app2!\n');
}).listen(4000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:4000/');

你也可以用Nginx设置多个域,转发到多个node.js进程。

例如实现这些:

domain1。example ->到本地运行的Node.js进程http://127.0.0.1:4000 domain2。example ->到本地运行的Node.js进程http://127.0.0.1:5000

这些端口(4000和5000)应该用来监听应用程序代码中的应用程序请求。

/etc/nginx/sites-enabled / domain1

server {
    listen 80;
    listen [::]:80;
    server_name domain1.example;
    access_log /var/log/nginx/domain1.access.log;
    location / {
        proxy_pass    http://127.0.0.1:4000/;
    }
}

在/etc/nginx/sites-enabled / domain2

server {
    listen 80;
    listen [::]:80;
    server_name domain2.example;
    access_log /var/log/nginx/domain2.access.log;
    location / {
        proxy_pass    http://127.0.0.1:5000/;
    }
}

回答你的问题2:

我会选择选项b,因为它消耗的资源少得多。使用选项“a”,每个客户端都会导致服务器消耗大量内存,加载所有你需要的文件(即使我喜欢php,这是它的一个问题)。使用选项“b”,您可以加载您的库(可重用代码),并在所有客户端请求之间共享它们。

但是要注意,如果你有多个核,你应该调整node.js来使用所有的核。