运行多个 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:



 

扫描二维码关注公众号,回复: 514574 查看本文章

    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;    

}

猜你喜欢

转载自zsjg13.iteye.com/blog/2239461
今日推荐