简体   繁体   English

Node.js“服务器”与 Nginx 或 Apache 服务器相比如何?

[英]How does a Node.js “server” compare with Nginx or Apache servers?

I have been studying Node.js recently and came across some material on writing simple Node.js based servers.我最近一直在研究 Node.js,并且遇到了一些关于编写基于 Node.js 的简单服务器的材料。 For example, the following.例如,以下。

var express = require("express"),
http = require("http"), app;

// Create our Express-powered HTTP server
// and have it listen on port 3000
app = express();
http.createServer(app).listen(3000);

// set up our routes
app.get("/hello", function (req, res) {
    res.send("Hello World!");
});

app.get("/goodbye", function (req, res) {
    res.send("Goodbye World!");
});

Now, although I seem to understand what's going on in the code, I am slightly confused by the terminology.现在,虽然我似乎明白代码中发生了什么,但我对术语有点困惑。 When I hear the term server, I think about stuff like Apache or Nginx.当我听到服务器这个词时,我会想到像 Apache 或 Nginx 这样的东西。 I am used to thinking about them as being like a container that can hold my web applications.我习惯于将它们视为可以容纳我的 Web 应用程序的容器。 How does Node.js server differ from Nginx/Apache server? Node.js 服务器与 Nginx/Apache 服务器有何不同? Isn't it true that a Node.js based server (ie code) can still be placed within something like Nginx to run?难道基于 Node.js 的服务器(即代码)仍然可以放在 Nginx 之类的东西中运行吗? So why are both called "servers"?那么为什么两者都被称为“服务器”呢?

It's a server, yes.这是一个服务器,是的。

A node.js web application is a full-fledged web server just like Nginx or Apache. node.js Web 应用程序是一个成熟的 Web 服务器,就像 Nginx 或 Apache 一样。

You can indeed serve your node.js application without using any other web server.您确实可以在不使用任何其他 Web 服务器的情况下为您的 node.js 应用程序提供服务。 Just change your code to:只需将您的代码更改为:

app = express();
http.createServer(app).listen(80); // serve HTTP directly

Indeed, some projects use node.js as the front-end load balancer for other servers (including Apache).事实上,一些项目使用 node.js 作为其他服务器(包括 Apache)的前端负载均衡器。

Note that node.js is not the only development stack to do this.请注意,node.js 并不是执行此操作的唯一开发堆栈。 Web development frameworks in Go, Java and Swift also do this. Go、Java 和 Swift 中的 Web 开发框架也这样做。

Why?为什么?

In the beginning was the CGI.一开始是CGI。 CGI was fine and worked OK. CGI 很好,工作正常。 Apache would get a request, find that the url needs to execute a CGI app, execute that CGI app and pass data as environment variables, read the stdout and serve the data back to the browser. Apache 会收到一个请求,发现 url 需要执行一个 CGI 应用程序,执行该 CGI 应用程序并将数据作为环境变量传递,读取标准输出并将数据提供回浏览器。

The problem is that it is slow.问题是它很慢。 It's OK when the CGI app was a small statically compiled C program but a group of small statically compiled C programs became hard to maintain.当 CGI 应用程序是一个小的静态编译的 C 程序时是可以的,但是一组小的静态编译的 C 程序变得难以维护。 So people started writing in scripting languages.于是人们开始用脚本语言写作。 Then that became hard to maintain and people started developing object oriented MVC frameworks.然后这变得难以维护,人们开始开发面向对象的 MVC 框架。 Now we started having trouble - EVERY REQUEST must compile all those classes and create all those objects just to serve some HTML, even if there's nothing dynamic to serve (because the framework needs to figure out that there's nothing dynamic to serve).现在我们开始遇到麻烦 - 每个请求都必须编译所有这些类并创建所有这些对象来提供一些 HTML,即使没有任何动态可以提供(因为框架需要弄清楚没有任何动态可以提供)。

What if we don't need to create all those objects every request?如果我们不需要在每个请求中创建所有这些对象怎么办?

That was what people thought.人们是这么想的。 And from trying to solve that problem came several strategies.从试图解决这个问题中产生了几种策略。 One of the earliest was to embed interpreters directly in web servers like mod_php in Apache.最早的方法之一是将解释器直接嵌入到 Web 服务器中,例如 Apache 中的mod_php Compiled classes and objects can be stored in global variables and therefore cached.编译后的类和对象可以存储在全局变量中,因此可以缓存。 Another strategy was to do pre-compilation.另一种策略是进行预编译。 And yet another strategy was to run the application as a regular server process and talk with the web server using a custom protocol like FastCGI.另一个策略是将应用程序作为常规服务器进程运行,并使用 FastCGI 等自定义协议与 Web 服务器通信。

Then some developers started simply using HTTP as their app->server protocol.然后一些开发人员开始简单地使用 HTTP 作为他们的应用程序->服务器协议。 In effect, the app is also an HTTP server.实际上,该应用程序也是一个 HTTP 服务器。 The advantage of this is that you don't need to implement any new, possibly buggy, possibly not tested protocol and you can debug your app directly using a web browser (or also commonly, curl ).这样做的好处是您不需要实现任何新的、可能有问题、可能未经测试的协议,并且您可以直接使用网络浏览器(或通常使用curl )调试您的应用程序。 And you don't need a modified web server to support your app, just any web server that can do reverse proxying or redirects.而且您不需要修改的 Web 服务器来支持您的应用程序,只需任何可以执行反向代理或重定向的 Web 服务器。

Why use Apache/Nginx?为什么使用 Apache/Nginx?

When you serve a node.js app note that you are the author of your own web server.当您提供 node.js 应用程序时,请注意您是自己的 Web 服务器的作者。 Any potential bug in your app is a directly exploitable bug on the internet.您的应用程序中的任何潜在错误都是互联网上可直接利用的错误。 Some people are (justifiably) not comfortable with this.有些人(有理由)对此感到不舒服。

Adding a layer of Apache or Nginx in front of your node.js app means you have a battle-tested, security-hardened piece of software on the live internet as an interface to your app.在您的 node.js 应用程序前面添加一层 Apache 或 Nginx 意味着您在实时互联网上拥有一个经过实战测试、经过安全加固的软件,作为您的应用程序的接口。 It adds a tiny bit of latency (the reverse proxying) but most consider it worth it.它增加了一点延迟(反向代理),但大多数人认为这是值得的。

This used to be the standard advice in the early days of node.js.这曾经是 node.js 早期的标准建议。 But these days there are also sites and web services that exposes node.js directly to the internet.但是现在也有一些站点和 Web 服务将 node.js 直接暴露给 Internet。 The http.Server module is now fairly well battle-tested on the internet to be trusted. http.Server模块现在在互联网上经过了相当好的http.Server测试,值得信赖。

NodeJs creates its own server. NodeJs 创建自己的服务器。 As you can see, terminology is quite clear:如您所见,术语非常清楚:

http.createServer(app).listen(3000);

Create a server and listen for http requests on port 3000.创建一个服务器并在端口 3000 上侦听 http 请求。

We used nginx in one of our project, but it was more like a loadbalancer for multiple nodejs instances.我们在我们的一个项目中使用了 nginx,但它更像是多个 nodejs 实例的负载均衡器。

Lets say you have two nodejs instances running on port 3000 and 3001, Now you can still use nginx as a server to listen your actual http calls on port 80 , and may want to redirect your request to nodejs server or maybe some other server, more like a loadbalancer .假设您有两个 nodejs 实例在端口 3000 和 3001 上运行,现在您仍然可以使用nginx作为服务器来侦听port 80上的实际http调用,并且可能希望将您的请求重定向到nodejs服务器或其他服务器,更多就像一个loadbalancer So you can still use whatever nginx provides with nodejs .所以你仍然可以使用nginxnodejs提供的任何东西。

A good question already asked here . 这里已经问一个好问题。

Assume there is a hotel named Apache Hotel which has a waiter for each customer.假设有一家名为 Apache Hotel 的酒店,它为每位顾客配备了一名服务员。

As soon as the customer orders a salad, the waiter goes to the chef and tells him.顾客一点了沙拉,服务员就会去找厨师告诉他。 While the chef prepares the food, the waiter waits.当厨师准备食物时,服务员在等待。 Here,在这里,

Chef => File System,

Waiter => Thread,

Customer => Event.

Even when the customer orders water the waiter brings only after serving the salad.即使顾客点了水,服务员也是在沙拉上完之后才拿来的。 The waiter keeps on waiting until the salad is prepared by the chef.服务员一直在等待,直到厨师准备好沙拉。 This state is referred as blocking state.这种状态称为阻塞状态。 Even if the hotel grows each customer should have different waiters to serve.即使酒店发展壮大,每个客户也应该有不同的服务员来服务。 This increases the blocking of threads(waiters).这增加了线程(服务员)的阻塞。

Now, coming to Node Hotel there is only one waiter for all the customers.现在,来到节点酒店,所有客人都只有一个服务员。 If first customer orders soup the waiter tells the chef and goes to second customer.如果第一位顾客点了汤,服务员会告诉厨师,然后去找第二位顾客。 After the food is ready the waiter delivers to the customer.食物准备好后,服务员将食物送到顾客手中。 Here the customer will not wait.在这里,客户不会等待。 This state is referred as Non-Blocking state.这种状态称为非阻塞状态。 The single waiter(Thread) servers all the customer and makes them happy.单个服务员(线程)为所有客户提供服务并让他们开心。

Thus, Node which is a single threaded application is very fast.因此,作为单线程应用程序的 Node 速度非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM