012.Nginxvrrp
一 负载均衡概述
1.1 负载均衡介绍
二 Nginx负载均衡
2.1 优点
2.2 主要均衡机制
2.3 负载均衡策略
2.4 负载均衡内置变量
2.5 负载均衡健康检查
2.6 负载均衡状态码
状态 | 概述 |
max_fails | 允许请求失败的次数,默认为1。 |
max_conns | 限制最大接受的连接数。 |
fail_timeout | 在经历了max_fails次失败后,暂停服务的时间。 |
backup | 预留的备份机器。 |
down | 表示当前的server暂时不参与负载均衡。 |
三 默认轮询负载均衡
3.1 环境预设
主机 | IP | 备注 |
nginx01 | 172.24.10.21 | Nginx负载均衡 |
nginx02 | 172.24.10.22 | 后端RS 01 |
nginx03 | 172.24.10.23 | 后端RS 02 |
nginx04 | 172.24.10.24 | 后端RS 03 |
1 [root@nginx02 ~]# mkdir /usr/share/nginx/balanc/2 [root@nginx02 ~]# echo '<h1>Rs_172.24.10.22</h1>' > /usr/share/nginx/balanc/index.html
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF2 server {
3 listen 9090;
4 server_name 172.24.10.22;
5 location / {
6 root /usr/share/nginx/balanc;
7indexindex.html;
8 access_log /var/log/nginx/rs.access.log main;
9 error_log /var/log/nginx/rs.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx02 ~]# nginx -s reload #重载配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/balanc/2 [root@nginx03 ~]# echo '<h1>Rs_172.24.10.23</h1>' > /usr/share/nginx/balanc/index.html
1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF2 server {
3 listen 9090;
4 server_name 172.24.10.23;
5 location / {
6 root /usr/share/nginx/balanc/;
7indexindex.html;
8 access_log /var/log/nginx/rs.access.log main;
9 error_log /var/log/nginx/rs.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx03 ~]# nginx -s reload #重载配置文件
1 [root@nginx04 ~]# mkdir /usr/share/nginx/balanc/2 [root@nginx04 ~]# echo '<h1>Rs_172.24.10.24</h1>' > /usr/share/nginx/balanc/index.html
1 [root@nginx04 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF2 server {
3 listen 9090;
4 server_name 172.24.10.24;
5 location / {
6 root /usr/share/nginx/balanc/;
7indexindex.html;
8 access_log /var/log/nginx/rs.access.log main;
9 error_log /var/log/nginx/rs.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx04 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx04 ~]# nginx -s reload #重载配置文件
3 [root@client ~]# curl 172.24.10.22:9090
4 <h1>Rs_172.24.10.22</h1>
5 [root@client ~]# curl 172.24.10.23:9090
6 <h1>Rs_172.24.10.23</h1>
7 [root@client ~]# curl 172.24.10.24:9090
8 <h1>Rs_172.24.10.24</h1>
3.2 默认轮询方式配置
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/balance.conf2 upstream mybalance01 {
3 server 172.24.10.22:9090;
4 server 172.24.10.23:9090;
5 server 172.24.10.24:9090;
6 }
7
8 server {
9 listen 80;
10 server_name balance.linuxds.com;
11 access_log /var/log/nginx/mybalance.access.log main;
12 error_log /var/log/nginx/mybalance.error.log warn;
13 location / {
14 proxy_pass http://mybalance01;
15indexindex.html;
16 proxy_redirect off;
17 proxy_set_header Host $host;
18 proxy_set_header X-Real-IP $remote_addr;
19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
20 client_max_body_size 10m; #允许客户端请求的最大单文件字节数
21 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
22 proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
23 proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
24 proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
25 proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
26 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
27 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
28 proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
29 }
30 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@balance ~]# nginx -s reload #重载配置文件
3.3 确认测试
四 加权轮询负载均衡
4.1 环境预设
4.2 加权轮询方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf2 upstream mybalance01 {
3 server 172.24.9.11:9090 weight=1 max_fails=1 fail_timeout=2;
4 server 172.24.9.12:9090 weight=8 max_fails=2 fail_timeout=2;
5 server 172.24.9.13:9090 backup; #配置为备份服务器
6 }
7
8 server {
9 listen 80;
10 server_name balance.linuxds.com;
11 access_log /var/log/nginx/mybalance.access.log main;
12 error_log /var/log/nginx/mybalance.error.log warn;
13 location / {
14 proxy_pass http://mybalance01;
15indexindex.html;
16 proxy_redirect off;
17 proxy_set_header Host $host;
18 proxy_set_header X-Real-IP $remote_addr;
19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
20 client_max_body_size 10m;
21 client_body_buffer_size 128k;
22 proxy_connect_timeout 300;
23 proxy_send_timeout 300;
24 proxy_read_timeout 300;
25 proxy_buffer_size 4k;
26 proxy_buffers 4 32k;
27 proxy_busy_buffers_size 64k;
28 proxy_temp_file_write_size 64k;
29 }
30 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@balance ~]# nginx -s reload #重载配置文件
4.3 确认测试
五 最小连接负载均衡
5.1 环境预设
5.2 最小连接方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf2 upstream mybalance01 {
3 least_conn;
4 server 172.24.10.21:9090;
5 server 172.24.10.22:9090;
6 server 172.24.10.23:9090;
7 }
8
9 server {
10 listen 80;
11 server_name balance.linuxds.com;
12 access_log /var/log/nginx/mybalance.access.log main;
13 error_log /var/log/nginx/mybalance.error.log warn;
14 location / {
15 proxy_pass http://mybalance01;
16indexindex.html;
17 }
18 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@balance ~]# nginx -s reload #重载配置文件
5.3 确认测试
六 IP hash负载均衡
6.1 环境预设
6.2 ip hash方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf2 upstream mybalance01 {
3 ip_hash;
4 server 172.24.10.21:9090;
5 server 172.24.10.22:9090;
6 server 172.24.10.23:9090;
7 }
8
9 server {
10 listen 80;
11 server_name balance.linuxds.com;
12 access_log /var/log/nginx/mybalance.access.log main;
13 error_log /var/log/nginx/mybalance.error.log warn;
14 location / {
15 proxy_pass http://mybalance01;
16indexindex.html;
17 }
18 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@balance ~]# nginx -s reload #重载配置文件
6.3 确认测试
七 fair负载均衡
7.1 环境预设
7.2 fair方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf2 upstream mybalance01 {
3 fair;
4 server 172.24.10.21:9090;
5 server 172.24.10.22:9090;
6 server 172.24.10.23:9090;
7 }
8
9 server {
10 listen 80;
11 server_name balance.linuxds.com;
12 access_log /var/log/nginx/mybalance.access.log main;
13 error_log /var/log/nginx/mybalance.error.log warn;
14 location / {
15 proxy_pass http://mybalance01;
16indexindex.html;
17 }
18 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@balance ~]# nginx -s reload #重载配置文件
7.3 确认测试
八 url_hash负载均衡
8.1 环境预设
8.2 ip hash方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf2 upstream mybalance01 {
3 hash $request_uri;
4 hash_method crc32;
5 server 172.24.10.21:9090;
6 server 172.24.10.22:9090;
7 server 172.24.10.23:9090;
8 }
9
10 server {
11 listen 80;
12 server_name balance.linuxds.com;
13 access_log /var/log/nginx/mybalance.access.log main;
14 error_log /var/log/nginx/mybalance.error.log warn;
15 location / {
16 proxy_pass http://mybalance01;
17indexindex.html;
18 }
19 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@balance ~]# nginx -s reload #重载配置文件
8.3 确认测试
以上是 012.Nginxvrrp 的全部内容, 来源链接: utcz.com/a/57754.html