`
zsjg13
  • 浏览: 138271 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

运行多个 Node 服务器

阅读更多

    购买若干服务器很容易,然后在这些服务器上运行一些 Node 进程。But how can these distinct servers be coordinated such that they form part of a single application? One aspect of this problem concerns clustering multiple identical servers around a single entry point. How can client connections be shared arcoss a pool of servers?

 

1、Forward and reverse proxies

    A proxy is someone or something acting on behalf of another.

    A forward proxy normally works on behalf of clients in a private network,brokering requests to an outside network,such as retrieving data from the Internet. 


 

    网络管理员需要限制对外界的访问时就需要用到 forward proxies。如果用户通过 e-mail 附件下载恶意软件,管理员就可以阻止对那个地址的访问。在一个办公室网络中可能会限制对社交网站的访问。一些国家会通过这种方式限制对外界网站的访问。

 

    A reverse proxy,一点也不奇怪,工作方式正好相反,它接受来自公共网络的请求 and serving those requests within a private network the client has little much visibility into. Direct access to servers by clients is first delegated to a reverse proxy:



 

    This is the type of proxy we might use to balance requests from clients across many Node servers.

    我们将会在扩展 Node 时看看怎样实现 reverse proxies,讨论一些实现,如利用 Nginx 以及 利用 native Node modules。

 

2、Nginx 作为 proxy

    当把 Node 服务器隐藏在一个 proxy 后面时,Nginx 是一个受欢迎的选择。Nginx 是一个非常受欢迎的高性能 web 服务器,它常常被用作为一个 proxy 服务器。

【 Nginx 使用很少的资源就能够每秒处理更多的请求,就是因为它的架构。它有一个 master process,该 process 将工作委托给一个或更多个 worker processes。每个 worker 都是以 event-driven 或 asynchronous 的方式处理多个请求,它利用了来自 Linux 内核的特殊功能(epoll/select/poll)】。

    Nginx 还让简单的负载均衡变得容易。在例子中,我们会讲解如何用 Nginx 作为代理,同时还开箱即用得支持负载均衡。

    假设你已经安装了 Nginx,这个时候要修改 nginx.conf,特别是其中的 http 部分。目标就是创建一个 Nginx 服务器,它的唯一责任就是均匀地将请求分发到一组 Node 服务器。这样整个系统的容量就扩充了,同时又不需要改动关于一个客户端如何或应当连接到我们的应用程序的任何东西



 

    在 nginx.conf 中的 http 部分定义了一些 upstream servers that will be candidates for redirection:

http {

...

    upstream app_myservers {

        server first.example.com;

        server second.example.com;

        server third.example.com;

    }

...

 

}

    现在,我们已经建立好了 the candidate pool, 我们就需要配置 Nginx,让它将请求转发到它的其中一个成员。

server {

    listen       0.0.0.0:80;

    server_name  example.com my_website;

...

    location / {

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

        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://app_myservers/;

        proxy_redirect off;

    }

}

    关键的一行是:proxy_pass http://app_myservers/;  它和我们的 upstream 定义是一样的。发生什么事情这个时候应该很清楚了:一个 Nginx 服务器监听 80 端口,将会将请求传递到 app_myservers 中的一个服务器定义。如果 upstream 定义中只有一个服务器,那个服务器就会接管所有的 traffice。如果定义了多个服务器,则 Nginx 会尝试均匀地分发 traffic。

 

【也可以用上述方法负载均衡多个本地服务器。你只需要在不同的端口上运行不同的 Node 服务器,例如 127.0.0.1:8001,127.0.0.1:8002】

   

    因为我们很可能想要更精确地控制 traffice 是如何在 upstream servers 上分发的。所以有一些指令可以被运用到我们的 upstream server 定义上。

    为了控制 traffice 分发的相对权重,要用到 weight 指令:

upstream app_myservers {

    server first.example.com weight=1;

    server second.example.com weight=2; 

    server third.example.com weight=4;    

}

    还有2个有用的指令,它们一起管理着连接的失败:

.max_fails:This is the number of times communication with a server fails 

prior to marking that server as inoperative. The period of time within which 

these failures must occur is defined by fail_timeout.

.fail_timeout:The time slice during which max_fails must occur, indicating 

that a server is inoperative. This number also indicates the amount of time 

after a server is marked inoperative that Nginx will again attempt to reach 

the flagged server.

    例如:

upstream app_myservers {

    server first.myservers.com weight=1 max_fails=2 fail_

timeout=20s;

    server second.myservers.com weight=2 max_fails=10 fail_

timeout=5m;    

    server third.myservers.com weight=4;    

}

  • 大小: 16.3 KB
  • 大小: 16.7 KB
  • 大小: 33.5 KB
分享到:
评论

相关推荐

    Node.js-使用Node编写的一系列RESTAPI已运行在我自己的服务器中可直接调用同时提供https支持

    这个仓库是一个单独的 Node 应用,基于 restify 开发,运行后可提供多个 API 的服务。每个文件夹实现一个 API 功能

    Node.js(node-v16.15.1-win-x86.zip)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-win-x64.zip)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1.pkg)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1.tar.gz 源码)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-linux-s390x.tar.xz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-x64.msi)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-linux-arm64.tar.xz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-linux-x64.tar.xz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-linux-armv7l.tar.xz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-linux-ppc64le.tar.xz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-x86.msi)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-darwin-arm64.tar.gz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js(node-v16.15.1-aix-ppc64.tar.gz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    werx:多个进程上的完整 Node Web 服务器

    多个进程上的完整 Node Web 服务器(默认为 CPU 核心数)。 Node 的单个实例在单个线程中运行。 为了利用多核系统,werx 启动了一个 Node 进程集群来处理负载。 更新为使用 HTTP/2 ( )。 您将需要安装 Node.js...

    node-cafeteria:Simple Express应用程序可在单个端口上运行多个HTMLNodeExpress服务器

    只需下载Cafeteria并将其复制到您的服务器位置,然后运行npm install 。 如果要更改端口等,请编辑“ config.json”文件。将您的应用程序添加到app文件夹中。 使用node .启动服务器node . 命令,坐下来放松。 支持...

    基于Lua实现的脚本和服务器引擎Node-Lua.zip

    Node-Lua是一款基于Lua实现的脚本和服务器引擎,它支持构建海量Lua服务(Context_Lua)并以多线程方式运行在多核服务器上,采用了任务多路复用的设计方案,有效利用了多核优势。node-lua致力于构建一个快速、简单易用...

    Node.js(node-v16.15.1-darwin-x64.tar.gz)

    Node.js 应用程序运行单线程,尽管 Node.js 使用多个线程来处理文件和网络事件。由于 Node.js 的异步特性,它通常用于实时应用程序。 Node.js 内部使用 Google V8 JavaScript 引擎来执行代码;大部分基本模块都是用...

    Node.js实现简单聊天服务器

    Node.js 是一个基于Chrome JavaScript运行时建立的一个平台, 用来方便地搭建快速的,易于扩展的网络应用,今天我们来探讨下,如何使用node.js实现简单的聊天服务器

    projectile:一种无需代理即可在一个 node.js 服务器上运行多个基于 http 的项目的简单方法,并将不同的 url 路由到它们

    这是一种拥有多个 node.js http 服务器的简单方法,每个服务器都在不同的文件中,并在请求到达某些路径(例如 /my-chat-c​​lient 或 /hello)时将请求路由到它们,而无需使用代理,这允许更容易部署的东西,有时...

Global site tag (gtag.js) - Google Analytics