Nginx 反向代理负载均衡

Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能

1、http upstream配置参数

# 自定义一组服务器,配置在http块内
upstream name {
 server .....
 ......
}

# 示例
upstream backend {
   server backend1.example.com weight=5;
   server 127.0.0.1:8080  max_fails=3 fail_timeout=30s;
   server unix:/tmp/backend3;
   server backup1.example.com backup;
}


server address [parameters];
# 配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
# server支持的parameters如下:
weight=number     # 设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number  # 给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number  # 后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测
fail_timeout=time # 后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
backup  # 设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器
down    # 标记为down状态,可以平滑下线后端服务器,新用户不再调度到此主机,旧用户不受影响
hash KEY [consistent];
# 基于指定请求报文中首部字段或者URI等key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算

# 示例
hash $request_uri consistent; #基于用户请求的uri做hash
hash $cookie_sessionid    #基于cookie中的sessionid这个key进行hash调度,实现会话绑定
ip_hash;
# 源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计算,以实现会话保持
# hash $remote_addr 则是对全部32bit的IPv4进行hash计算
least_conn;
# 最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC

2、部署代理后端多台web服务器

# 环境
192.168.1.51 nginx
192.168.1.52 web1
192.168.1.11 web2
# 后端服务器部署apache
[root@web1 ~]# yum install -y httpd
[root@web1 ~]# echo "192.168.1.52 Hello World Web1" > /var/www/html/index.html
[root@web1 ~]# systemctl enable --now httpd.service

[root@web2 ~]# yum install -y httpd
[root@web2 ~]# echo "192.168.1.11 Hello World Web2" > /var/www/html/index.html
[root@web2 ~]# systemctl enable --now httpd.service

[root@nginx01 ~]# curl http://192.168.1.11
192.168.1.11 Hello World Web2
[root@nginx01 ~]# curl http://192.168.1.52
192.168.1.52 Hello World Web1
# 配置 nginx 反向代理
[root@nginx01 ~]# vim /apps/nginx/conf/nginx.conf
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    include /apps/nginx/conf/conf.d/*.conf;
}

[root@nginx01 ~]# vim /apps/nginx/conf/conf.d/web.conf
upstream webserver {
    server 192.168.1.52:80 weight=1 fail_timeout=5s max_fails=3;
    server 192.168.1.11:80 weight=1 fail_timeout=5s max_fails=3;
}

server {
    listen 80;
    server_name 192.168.1.51;
    location / {
        index index.html index.php;
        root /data/nginx/html/pc;
    }

    location /web {
        index index.html;
        proxy_pass http://webserver/;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    }
}

[root@nginx01 ~]# systemctl restart nginx

[root@client ~]# curl http://192.168.1.51/web
192.168.1.52 Hello World Web1
[root@client ~]# curl http://192.168.1.51/web
192.168.1.11 Hello World Web2

[root@client ~]# while true;do curl http://192.168.1.51/web;sleep 1;done
192.168.1.11 Hello World Web2
192.168.1.52 Hello World Web1
192.168.1.11 Hello World Web2
192.168.1.52 Hello World Web1
192.168.1.11 Hello World Web2
192.168.1.52 Hello World Web1
192.168.1.11 Hello World Web2
192.168.1.52 Hello World Web1
192.168.1.11 Hello World Web2
192.168.1.52 Hello World Web1

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享