zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

Nginx 负载均衡配置

2023-09-14 09:16:41 时间

upstream 参数 

参数描述
down不参与负载
weight权重
max_fails失败多少次 认为主机已挂掉则,踢出
fail_timeout踢出后重新探测时间
backup备用服务
max_conns允许最大连接数
slow_start当节点恢复,不立即加入

负载均衡策略 

轮询默认方式
weight权重方式
ip_hash依据ip分配方式
least_conn最少连接方式
fair(第三方)响应时间方式
url_hash(第三方)依据URL分配方式

配置负载均衡

# 新建一个配置文件
vim /etc/nginx/conf.d/balance.conf
# 在http节点里面增加upstream节点
upstream lb {                         # 连接池,提供存放web服务的服务器地址
    server 192.168.0.108 weight=5;    # 一台服务器地址 权重5/6
    server 192.168.0.177 weight=1;    # 一台服务器地址 权重1/6
}

# 在server节点下location节点增加内容
server {
    
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://lb;                          # 指定代理连接池
        proxy_set_header Host $host;                   # 转发请求头信息
        proxy_set_header X-Forward-For $remote_addr;   # 转发请求IP地址
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}