我已经在服务器上设置了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中配置它。
你也可以在一个服务器配置中为应用程序设置不同的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/;
}
}