1.1 HTTP Load Balancing

Problem

You need to distribute load between two or more HTTP servers.

问题

将用户请求分发到 2 台以上 HTTP 服务器。


Solution

Use NGINX’s HTTP module to load balance over HTTP servers

using the upstream block:

upstream backend {
    server 10.10.12.45:80 weight=1;
    server app.example.com:80 weight=2;
}
server {
    location / {
        proxy_pass http://backend;
    }
}

This configuration balances load across two HTTP servers on port

  1. The weight parameter instructs NGINX to pass twice as many

connections to the second server, and the weight parameter defaults

to 1.

解决方案

使用 NGINX 的 HTTP 模块,将请求分发到有 upstream 块级指令代理的 HTTP

服务器集群,实现负载均衡:

upstream backend {
    server 10.10.12.45:80 weight=1;
    server app.example.com:80 weight=2;
}
server {
    location / {
        proxy_pass http://backend;
    }
}

配置中启用了两台默认 80 端口 HTTP 服务器构成服务器集群。weight 参数表示

没三个请求将有 2 个请求分发到 app.example.com:80 服务器,它的默认值为 1。


Discussion

The HTTP upstream module controls the load balancing for HTTP.

This module defines a pool of destinations, either a list of Unix

sockets, IP addresses, and DNS records, or a mix. The upstream

module also defines how any individual request is assigned to any of

the upstream servers.

Each upstream destination is defined in the upstream pool by the

server directive. The server directive is provided a Unix socket, IP

address, or an FQDN, along with a number of optional parameters.

The optional parameters give more control over the routing of

requests. These parameters include the weight of the server in the

balancing algorithm; whether the server is in standby mode, avail‐

able, or unavailable; and how to determine if the server is unavail‐

able. NGINX Plus provides a number of other convenient

parameters like connection limits to the server, advanced DNS reso‐

lution control, and the ability to slowly ramp up connections to a

server after it starts.

结论

HTTP 模块的 upstream 用于设置被代理的 HTTP 服务器实现负载均衡。模块

内定义一个目标服务器连接池,它可以是 UNIX 套接字、IP 地址、DNS 记录

或它们的混合使用配置;此外 upstream 还可以通过 weight 参数配置,如何

分发请求到应用服务器。

所有 HTTP 服务器在 upstream 块级指令中由 server 指令配置完成。server

指令接收 UNIX 套接字、IP 地址或 FQDN(Fully Qualified Domain Name: 全限

定域名) 及一些可选参数。可选参数能够精细化控制请求分发。它们包括用于负

载均衡算法的 weight 参数;判断目标服务器是否可用,及如何判断服务器可用

性的 max_fails 指令和 fail_timeout 指令。NGINX Plus 版本提供了许多其他

方便的参数,比如服务器的连接限制、高级DNS解析控制,以及在服务器启动后

缓慢地连接到服务器的能力。

results matching ""

    No results matching ""