Node.js + Nginx - 现在怎么办?
我已经在我的服务器上设置了 Node.js 和 Nginx。现在我想使用它,但是,在开始之前有两个问题:
- 它们应该如何协同工作?我应该如何处理这些请求?
Node.js 服务器有 2 个概念,哪一个更好:
a.为每个需要它的网站创建一个单独的 HTTP 服务器。然后在程序开始时加载所有JavaScript代码,因此代码被解释一次。
b.创建一个处理所有 Node.js 请求的 Node.js 服务器。这会读取请求的文件并评估其内容。因此,文件会根据每个请求进行解释,但服务器逻辑要简单得多。
我不清楚如何正确使用 Node.js。
I've set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions:
- 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.
It's not clear for me how to use Node.js correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Nginx 作为前端服务器,在本例中将请求代理到 Node.js 服务器。因此,您需要为节点设置 Nginx 配置文件。
这就是我在 Ubuntu 盒子中所做的:
在
/etc/nginx/sites-available/
创建文件yourdomain.example
:在其中您应该有类似以下内容的内容:
如果您希望 Nginx (>= 1.3.13) 也处理 websocket 请求,请在
location /
部分添加以下行:完成此设置后,您必须启用配置中定义的站点上面的文件:
在
/var/www/yourdomain/app.js
创建节点服务器应用程序并在localhost:3000
运行它测试语法错误:
重新启动 Nginx:
最后启动节点服务器:
现在您应该在
yourdomain.example
处看到“Hello World”启动节点服务器的最后一个注意事项:您应该为节点守护程序使用某种监视系统。有一个很棒的关于带有 upstart 和 monit 的节点教程。
Nginx works as a front end server, which in this case proxies the requests to a node.js server. Therefore you need to set up an Nginx config file for node.
This is what I have done in my Ubuntu box:
Create the file
yourdomain.example
at/etc/nginx/sites-available/
:In it you should have something like:
If you want Nginx (>= 1.3.13) to handle websocket requests as well, add the following lines in the
location /
section:Once you have this setup you must enable the site defined in the config file above:
Create your node server app at
/var/www/yourdomain/app.js
and run it atlocalhost:3000
Test for syntax mistakes:
Restart Nginx:
Lastly start the node server:
Now you should see "Hello World" at
yourdomain.example
One last note with to starting the node server: you should use some kind of monitoring system for the node daemon. There is an awesome tutorial on node with upstart and monit.
你还可以使用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
在 /etc/nginx/sites-enabled/domain2
You can also setup multiple domain with Nginx, forwarding to multiple node.js processes.
For example to achieve these:
domain1.example
-> to Node.js process running locallyhttp://127.0.0.1:4000
domain2.example
-> to Node.js process running locallyhttp://127.0.0.1:5000
These ports (4000 and 5000) should be used to listen the app requests in your app code.
/etc/nginx/sites-enabled/domain1
In /etc/nginx/sites-enabled/domain2
您还可以在一个服务器配置中为应用程序使用不同的 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 中:
重新启动 Nginx:
启动应用程序。
节点app1.js
节点app2.js
You can also have different URLs for apps in one server configuration:
yourdomain.example/app1/*
-> to Node.js process running locallyhttp://127.0.0.1:3000
yourdomain.example/app2/*
-> to Node.js processrunning locally
http://127.0.0.1:4000
In /etc/nginx/sites-enabled/yourdomain:
Restart Nginx:
Starting applications.
node app1.js
node app2.js
我通过 Nginx 代理独立的 Node Express 应用程序。
因此,可以轻松安装新应用程序,并且我还可以在不同位置的同一服务器上运行其他内容。
以下是我使用 Nginx 配置示例进行设置的更多详细信息:
来自: http://skovalyov.blogspot.dk/2012 /07/deploy-multiple-node-applications-on.html
I proxy independent Node Express applications through Nginx.
Thus new applications can be easily mounted and I can also run other stuff on the same server at different locations.
Here are more details on my setup with Nginx configuration example:
From: http://skovalyov.blogspot.dk/2012/07/deploy-multiple-node-applications-on.html
带有 Nginx 配置的 Node.js。
添加以下配置,以便当我们来自
subdomain.your_domain.example
时,Nginx 充当代理重定向到来自服务器的端口 3000 流量Node.js with Nginx configuration.
add the following configuration so that Nginx acting as a proxy redirect to port 3000 traffic from the server when we come from
subdomain.your_domain.example
我在 Github 上创建了一个可以克隆的存储库,vagrant-node-nginx-boilerplate
基本上
/var/www/nodeapp
中的 Node.js 应用程序是,
/etc/nginx/sites-available/
中的 nginx 配置是I made a repository in Github which you can clone, vagrant-node-nginx-boilerplate
basically the node.js app at
/var/www/nodeapp
isand the nginx config at
/etc/nginx/sites-available/
is回答你的问题2:
我会使用选项b只是因为它消耗的资源少得多。使用选项“a”,每个客户端都会导致服务器消耗大量内存,加载您需要的所有文件(即使我喜欢php,这是它的问题之一)。使用选项“b”,您可以加载库(可重用代码)并在所有客户端请求之间共享它们。
但请注意,如果您有多个核心,则应该调整 node.js 以使用所有核心。
answering your question 2:
I would use option
b
simply because it consumes much less resources. with option 'a', every client will cause the server to consume a lot of memory, loading all the files you need (even though i like php, this is one of the problems with it). With option 'b' you can load your libraries (reusable code) and share them among all client requests.But be ware that if you have multiple cores you should tweak node.js to use all of them.
Nginx 可以充当反向代理服务器,其工作方式就像项目管理器一样。当它收到请求时,它会对其进行分析并将请求转发给上游(项目成员)或自行处理。 Nginx 根据其配置方式有两种处理请求的方法。
服务请求
将请求转发到另一台服务器
<前><代码>服务器{
服务器名称 mydomain.example sub.mydomain.example;
地点 /{
proxy_pass http://127.0.0.1:8000;
proxy_set_header 主机 $host;
proxy_pass_request_headers 开启;
}
位置/静态/{
别名 /my/static/files/path;
}
}
服务器请求
将请求转发到另一台服务器
当您在端口 8000 上运行 Node.js 服务器时,Nginx 会将请求转发到 Node.js。编写node.js逻辑并处理请求。就这样,你的 Nodejs 服务器就在 Nginx 服务器后面运行了。
如果你想运行除nodejs之外的任何其他服务,只需在不同的端口上运行另一个服务,如Django、flask、PHP,并在Nginx中配置它。
Nginx can act as a reverse proxy server which works just like a project manager. When it gets a request it analyses it and forwards the request to upstream(project members) or handles itself. Nginx has two ways of handling a request based on how its configured.
serve the request
forward the request to another server
Server the request
forward the request to another server
When you run node.js server on the port 8000 Nginx will forward the request to node.js. Write node.js logic and handle the request. That's it you have your nodejs server running behind the Nginx server.
If you wish to run any other services other than nodejs just run another service like Django, flask, PHP on different ports and config it in Nginx.
您还可以使用 node.js 将静态文件生成到 nginx 提供的目录中。当然,站点的某些动态部分可以由 Node 提供服务,另一些则由 nginx(静态)提供服务。
让其中一些由 nginx 提供服务可以提高您的性能。
You could also use node.js to generate static files into a directory served by nginx. Of course, some dynamic parts of your site could be served by node, and some by nginx (static).
Having some of them served by nginx increases your performance..
我们可以通过 Nginx 作为反向代理轻松设置 Nodejs 应用程序。
以下配置假设 NodeJS 应用程序在 127.0.0.1:8080 上运行,
在上述设置中,您的 Nodejs 应用程序将
get
HTTP_HOST
标头,您可以在其中应用域特定逻辑来提供响应。 '您的应用程序必须由流程管理器管理,例如 pm2 或处理情况/重用套接字或资源等的主管。
设置错误报告服务以获取生产错误,例如 sentry 或 < a href="http://rollbar.com/" rel="nofollow noreferrer">滚动条
注意:您可以设置处理域特定请求路由的逻辑,创建一个 用于expressjs应用程序的中间件
We can easily setup a Nodejs app by Nginx acting as a reverse proxy.
The following configuration assumes the NodeJS application is running on 127.0.0.1:8080,
in above setup your Nodejs app will,
get
HTTP_HOST
header where you can apply domain specific logic to serve the response. 'Your Application must be managed by a process manager like pm2 or supervisor for handling situations/reusing sockets or resources etc.
Setup an error reporting service for getting production errors like sentry or rollbar
NOTE: you can setup logic for handing domain specific request routes, create a middleware for expressjs application
Nginx 和 Nodejs 的最佳且更简单的设置是使用 Nginx 作为启用 proxy_protocol 的 HTTP 和 TCP 负载均衡器。在这种情况下,Nginx 将能够将传入请求代理到 Nodejs,并终止与后端 Nginx 服务器的 SSL 连接,而不是与代理服务器本身的 SSL 连接。 (SSL-PassThrough)
在我看来,给出非 SSL 示例是没有意义的,因为所有 Web 应用程序都(或应该)使用安全环境。
代理服务器的示例配置,位于 /etc/nginx/nginx.conf
现在,让我们处理后端 Web 服务器。
/etc/nginx/nginx.conf:
现在,让我们在 /etc/nginx/sites-available/example.com-https.conf 中使用启用了 SSL 和 proxy_protocol 的配置来配置虚拟主机:
最后,2 个 Nodejs Web 服务器的示例:
第一台服务器:
第二台服务器:
现在一切都应该完美运行并且负载平衡。
不久前我写了关于 如何在 Docker 中设置 Nginx 作为 TCP 负载均衡器。检查一下您是否正在使用 Docker。
The best and simpler setup with Nginx and Nodejs is to use Nginx as an HTTP and TCP load balancer with proxy_protocol enabled. In this context, Nginx will be able to proxy incoming requests to nodejs, and also terminate SSL connections to the backend Nginx server(s), and not to the proxy server itself. (SSL-PassThrough)
In my opinion, there is no point in giving non-SSL examples, since all web apps are (or should be) using secure environments.
Example config for the proxy server, in /etc/nginx/nginx.conf
Now, let's handle the backend webserver.
/etc/nginx/nginx.conf:
Now, let's configure the virtual host with this SSL and proxy_protocol enabled config at /etc/nginx/sites-available/example.com-https.conf:
And lastly, a sample of 2 nodejs webservers:
First server:
Second server:
Now everything should be perfectly working and load-balanced.
A while back I wrote about How to set up Nginx as a TCP load balancer in Docker. Check it out if you are using Docker.
如果你想管理每个微服务并运行它,你可以使用pm2运行nodejs。节点将在一个端口中运行,只需在 Nginx 中配置该端口即可(
/etc/nginx/sites-enabled/domain.example
)使用 ping 检查 localhost 是否正在运行。
这
是最好的,正如你所说的也更容易
You can run nodejs using pm2 if you want to manage each microservice means and run it. Node will be running in a port right just configure that port in Nginx (
/etc/nginx/sites-enabled/domain.example
)Check weather localhost is running or not by using ping.
And
This is best and as you said easier too