博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx配置 负载均衡_如何配置NGINX负载平衡
阅读量:2532 次
发布时间:2019-05-11

本文共 8950 字,大约阅读时间需要 29 分钟。

nginx配置 负载均衡

The load balancing is the process of distributing traffic to multiple instances of an application. It is commonly used for better resource utilization, maximizing throughput and reducing latency apart from ensuring fault‑tolerance of deployed applications.

负载平衡是将流量分配到应用程序的多个实例的过程。 除确保已部署应用程序的容错能力外,它通常用于提高资源利用率,最大化吞吐量和减少延迟。

Load balancing can be implemented in transport (Layer 4) and application layer (Layer 7) of the OSI reference model. The load balancing at the transport layer is implemented with dedicated hardware while the load balancing at the application layer is implemented with specialized software like NGINX or HAPROXY.

可以在OSI参考模型的传输层(第4层)和应用程序层(第7层)中实现负载平衡。 传输层的负载平衡由专用硬件实现,而应用程序层的负载平衡由NGINX或HAPROXY等专用软件实现。

NGINX负载平衡 (NGINX Load Balancing)

Nginx Load Balancing

Nginx Load Balancing

Nginx负载平衡

NGINX is a modern, open-source and high-performance web server. Apart from serving static and dynamic content very efficiently, NGINX can also be configured to act as a load balancer that can handle a large number of incoming connections and distribute them to separate upstream servers for processing thereby achieving fault tolerance and better performance of deployed applications.
NGINX是一种现代的,开源的高性能Web服务器。 除了非常有效地提供静态和动态内容外,NGINX还可以配置为充当负载平衡器,该负载平衡器可以处理大量传入连接并将它们分配到单独的上游服务器进行处理,从而实现容错能力和已部署应用程序的更好性能。

This tutorial will describe how NGINX can be configured as a load balancer in Ubuntu 18.04.

本教程将介绍如何在Ubuntu 18.04中将NGINX配置为负载均衡器。

先决条件 (Prerequisite)

  • You have already installed NGINX by following our tutorial from .

    您已经按照的教程安装了NGINX。

将NGINX配置为负载均衡器 (Configure NGINX as a Load Balancer)

The load balancing configuration instructs NGINX how to process incoming traffic by distributing them to a chain of servers sitting preferably in a private network. These chain of servers are known as the backend or upstream servers.

负载平衡配置指示NGINX如何处理传入的流量,方法是将它们分配到最好位于专用网络中的服务器链中。 这些服务器链称为后端或上游服务器。

To configure NGINX as a load balancer, the first step is to include the upstream or backend servers in your configuration file for load balancing scheme of things. The upstream module of NGINX exactly does this by defining these upstream servers like the following:

要将NGINX配置为负载均衡器,第一步是在配置文件中包括上游或后端服务器,以实现负载均衡方案。 NGINX的上游模块通过定义这些上游服务器来准确地做到这一点,如下所示:

upstream backend {   server 10.5.6.21;    server 10.5.6.22;   server 10.5.6.23;}

The above upstream directive includes three servers with their private address. You can also define the upstream servers with their domain name, however, from performance and security point of view, it is better to refer them by their private address.

上面的上游指令包括三个具有专用地址的服务器。 您还可以使用域名定义上游服务器,但是,从性能和安全性的角度来看,最好通过其私有地址来引用它们。

Once upstream servers have been defined, you just need to refer them in the desired location block by using proxy_pass directive to load balance the traffic.

定义上游服务器后,您只需使用proxy_pass指令在所需的位置块中引用它们,即可对流量进行负载平衡。

server {         ...         server_name SUBDOMAIN.DOMAIN.TLD         ...         location / {                      proxy_pass  https://backend;         }}

In the above server block, whenever traffic for the domain SUBDOMAIN.DOMAIN.TLD matches with root(/) location block, NGINX will forward the request to each upstream servers one by one. The default method of choosing an upstream server will be round robin although there are few other methods available to load balance the traffic which we will discuss in the next section.

在上述服务器块中,只要域SUBDOMAIN.DOMAIN.TLD流量与root(/)位置块匹配,NGINX就会将请求一一转发到每个上游服务器。 选择上游服务器的默认方法是轮询,尽管很少有其他方法可以负载均衡流量,我们将在下一节中讨论。

Let us now make use of above upstream and server block to create a load balancer for a domain. To do that, navigate to the NGINX server block configuration directory and create a configuration file using a text editor.

现在让我们利用上面的上游和服务器块为域创建负载均衡器。 为此,请导航至NGINX服务器块配置目录,并使用文本编辑器创建配置文件。

# cd /etc/nginx/sites-available/# vi load_balancer.confupstream backend {   server 10.0.2.144;   server 10.0.2.42;   server 10.0.2.44;   # add more servers here}server {           listen 80;            server_name SUBDOMAIN.DOMAIN.TLD;           location / {                        proxy_set_header Host $host;                                                  proxy_set_header X-Real-IP $remote_addr;                                                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                                  proxy_pass https://backend;           }}

Whenever traffic arrives at port 80 for the domain SUBDOMAIN.DOMAIN.TLD, NGINX will forward the request to each upstream servers defined in the upstream section in a round robin way and thus load balances the traffic. Make sure the hostname for proxy_pass directive must match with upstream module name.

每当流量到达域SUBDOMAIN.DOMAIN.TLD端口80时,NGINX都会以循环方式将请求转发到上游部分中定义的每个上游服务器,从而对流量进行负载平衡。 确保proxy_pass指令的主机名必须与上游模块名匹配。

Now check for any syntactical error in the NGINX configuration file and enable the server block. Once done reload NGINX to apply new settings.

现在检查NGINX配置文件中是否有语法错误,并启用服务器块。 完成后,重新加载NGINX以应用新设置。

# nginx -t# cd /etc/nginx/sites-enabled/# ln -s ../sites-available/load_balancer.conf .# systemctl reload nginx

To test the load balancer, make a CURL query to it. You will find that the load balancer is forwarding the request to each upstream servers one by one in a round robin method thereby load balancing the traffic.

要测试负载均衡器,请对其进行CURL查询。 您会发现负载均衡器正在以循环方式将请求逐个转发到每个上游服务器,从而对流量进行负载均衡。

CURL Query To NGINX Load balancer

CURL Query To Loadbalancer

CURL查询到负载均衡器

NGINX负载均衡方法 (NGINX Load balancing methods)

1.轮循 (1. Round robin)

The round robin scheme distributes the traffic to upstream servers equally and is the default scheme available if you don’t specify any. With this scheme each upstream server is selected one by one in turn according to the order you place them in the configuration file. That means clients request are served by any one of the listed upstream servers. The load balancer that we have configured earlier uses this scheme to choose an upstream server.

轮询方案将流量平均分配给上游服务器,并且是默认方案(如果您未指定)。 使用此方案,将根据将上游服务器放置在配置文件中的顺序依次选择每个上游服务器。 这意味着客户端请求由列出的任何上游服务器提供服务。 我们之前配置的负载均衡器使用此方案选择上游服务器。

2.最少连接 (2. Least connected)

With this scheme, load balancer assigns the request to the upstream server with the least number of active connections. This scheme is useful in a situation when active connection to the upstream server takes some time to complete or an upstream server is overloaded with active connections. To configure the least connected load balancing, add least_conn directive as the first line within the upstream module like below.

使用此方案,负载均衡器将请求分配给活动连接数最少的上游服务器。 当与上游服务器的活动连接需要一些时间才能完成,或者上游服务器的活动连接超载时,此方案非常有用。 要配置最少连接的负载平衡,请在上游模块中将least_conn指令添加为第一行,如下所示。

upstream wordpress_apps {   least_conn;   server 10.0.2.144;   server 10.0.2.42;   server 10.0.2.44;   # add more servers here}

3. IP哈希 (3. IP Hash)

This scheme selects an upstream server by generating a hash value based on the client’s IP address as a key. This allows the request from clients to be forwarded to the same upstream server provided it is available and the clients IP address has not changed. To configure IP hash method of load balancing, add ip_hash directive in the beginning within the upstream module like below.

该方案通过基于客户端的IP地址作为密钥生成哈希值来选择上游服务器。 这允许将来自客户端的请求转发到同一上游服务器,前提是该请求可用并且客户端IP地址未更改。 要配置IP哈希负载均衡方法,请在上游模块的开头添加ip_hash指令,如下所示。

upstream wordpress_apps {   ip_hash;   server 10.0.2.144;   server 10.0.2.42;   server 10.0.2.44;   # add more servers here}

4.加权 (4. Weighted)

The weighted method allows fine-tuning load balancing within the NGINX further. With this scheme, the upstream server with the highest weight is selected most often. This scheme is useful in the situation where the upstream server’s resources are not equal and favors the one with better available resources. To configure weighted load balancing, add weight directive after the URL parameter in the upstream section like below.

加权方法允许进一步微调NGINX内的负载平衡。 通过这种方案,最经常选择权重最高的上游服务器。 在上游服务器的资源不相等的情况下,此方案很有用,并且倾向于使用具有更好可用资源的服务器。 要配置加权负载平衡,请在上游部分的URL参数后面添加weight指令,如下所示。

upstream wordpress_apps {   server 10.0.2.144 weight=2;    server 10.0.2.42 weight=3;   server 10.0.2.44 weight=5;   # add more servers here}

With the above configuration out of every 10 requests, 2 requests are forwarded to the first server, 3 requests are forwarded to the second server and 5 requests are forwarded to the third server.

通过上述配置,每10个请求中,有2个请求被转发到第一服务器,有3个请求被转发到第二服务器,并且有5个请求被转发到第三服务器。

摘要 (Summary)

Load balancing with NGINX is relatively simple to set up yet a powerful way to increase throughput and better resource utilization for any web application. Further, load balancing increases the security of web application by placing the upstream servers in a private network. You can now proceed with implementing a load balancer in your environment by choosing a suitable method.

使用NGINX进行负载平衡相对容易设置,但是却是一种强大的方法,可以提高任何Web应用程序的吞吐量和更好的资源利用率。 此外,负载平衡通过将上游服务器放置在专用网络中来提高Web应用程序的安全性。 现在,您可以通过选择合适的方法来在您的环境中实施负载平衡器。

翻译自:

nginx配置 负载均衡

转载地址:http://qfqzd.baihongyu.com/

你可能感兴趣的文章
js与jQuery的区别——每日一记录
查看>>
MyBatis 处理sql中的 大于,小于,大于等于,小于等于
查看>>
Lunix文件的读写权限问题
查看>>
Liferay 7:portlet name
查看>>
PostgreSQL9.6.3的REDIS测试
查看>>
解决pycharm问题:module 'pip' has no attribute 'main'
查看>>
002 lambda表达式
查看>>
springboot添加自定义注解
查看>>
POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)
查看>>
JavaScript数组方法之reduce
查看>>
Linux常用命令之文件搜索命令
查看>>
thinkphp自定义权限管理之名称判断
查看>>
C++ ORM ODB 入门介绍(一)
查看>>
C#_02.14_基础五_.NET类
查看>>
Flask 学习资源
查看>>
Android SDK下载和更新失败的解决方法 分类: Android...
查看>>
MVC2 强类型的 HTML Helper
查看>>
开发 Windows 8 应用 - 0 - windows 8 开发资源
查看>>
生成二维码图片的工具类
查看>>
Surface Pro 4远程桌面分辨率问题
查看>>