ngx_http_upstream_module 示例
示例拓扑图
配置示例(默认权重)
1、Nginx 代理服务器配置
[root@Neo_Tang conf.d]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255
[root@Neo_Tang conf.d]# vim ../nginx.conf # 在 Nginx 主配置文件里进行 upstream 名称定义
http {
upstream webservers {
server 192.168.10.12:80;
server 192.168.10.13:80;
}
}
[root@Neo_Tang conf.d]# cat neotang.conf # 在 .conf 文件中进行 upstream 名称引用
server {
listen 80;
server_name www.neotang.com;
location / {
proxy_pass http://webservers;
}
}
[root@Neo_Tang conf.d]# pwd
/etc/nginx/conf.d
2、Nginx 代理服务器进行后端服务器访问
要确保 Nginx 代理服务器首先能访问后端被代理的服务器。
[root@Neo_Tang conf.d]# curl http://192.168.10.12/index.html
<h1>HTTP web-server-1 192.168.10.12</h1>
[root@Neo_Tang conf.d]# curl http://192.168.10.13/index.html
<h1>HTTP web-server-2 192.168.10.13</h1>
3.用户进行访问
在未进行权重设置时,默认权重一样,访问占比一样。
[root@neo ~]# ifconfig | grep "inet 192"
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
[root@neo ~]# for i in {1..10};do curl http://www.neotang.com; done
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
配置示例(配置权重)
1、Nginx 代理服务器配置
[root@Neo_Tang conf.d]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255
[root@Neo_Tang conf.d]# vim ../nginx.conf # 在 Nginx 主配置文件里进行 upstream 名称定义
http {
upstream webservers {
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=1;
}
}
[root@Neo_Tang conf.d]# cat neotang.conf # 在 .conf 文件中进行 upstream 名称引用
server {
listen 80;
server_name www.neotang.com;
location / {
proxy_pass http://webservers;
}
}
[root@Neo_Tang conf.d]# pwd
/etc/nginx/conf.d
[root@Neo_Tang conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@Neo_Tang conf.d]# nginx -s reload
2、Nginx 代理服务器进行后端服务器访问
要确保 Nginx 代理服务器首先能访问后端被代理的服务器。
[root@Neo_Tang conf.d]# curl http://192.168.10.12/index.html
<h1>HTTP web-server-1 192.168.10.12</h1>
[root@Neo_Tang conf.d]# curl http://192.168.10.13/index.html
<h1>HTTP web-server-2 192.168.10.13</h1>
3.用户进行访问
在配置权重后,访问比与设置的权重比一致。
[root@neo ~]# ifconfig | grep "inet 192"
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
[root@neo ~]# for i in {1..10};do curl http://www.neotang.com; done
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
<h1>HTTP web-server-2 192.168.10.13</h1>
<h1>HTTP web-server-1 192.168.10.12</h1>
配置示例(健康机制监测 和 backup/down 应用)
1、健康机制监测
默认 Nginx 的 upstream 具有健康机制监测,当某一台服务器的down后,所有的服务会被发到另外一台服务器上。
-
对 web-server2 进行服务停止
[root@Neo_Tang html]# systemctl stop httpd.service
-
用户进行访问
[root@neo ~]# for i in {1..10};do curl http://www.neotang.com; done <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1>
-
对 web-server2 进行服务进行重新启动
[root@Neo_Tang html]# systemctl start httpd.service
-
用户再次进行访问
[root@neo ~]# for i in {1..10};do curl http://www.neotang.com; done <h1>HTTP web-server-2 192.168.10.13</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-2 192.168.10.13</h1> <h1>HTTP web-server-2 192.168.10.13</h1> <h1>HTTP web-server-1 192.168.10.12</h1> <h1>HTTP web-server-1 192.168.10.12</h1>
2、手动配置健康机制监测
[root@Neo_Tang conf.d]# vim ../nginx.conf
http {
upstream webservers {
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=1 fail_timeout=1 max_fails=3;
}
}
3、配置 say sorry 服务器
如果两个后端服务器全挂了,可以配置 say sorry 服务器(后加backup,平时不会被调度)
[root@Neo_Tang ~]# ifconfig | grep "inet 192"
inet 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255
inet 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255
[root@Neo_Tang ~]# cat /usr/share/nginx/html/index.html
<h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1>
[root@Neo_Tang conf.d]# vim ../nginx.conf
http {
upstream webservers {
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=1 fail_timeout=1 max_fails=3;
server 127.0.0.1:80 backup;
}
}
-
对 web-server1 进行服务停止
[root@neo-neo ~]# ifconfig | grep "inet 192" inet 192.168.10.12 netmask 255.255.255.0 broadcast 192.168.10.255 [root@neo-neo ~]# systemctl stop httpd.service
-
对 web-server2 进行服务停止
[root@Neo_Tang ~]# ifconfig | grep "inet 192" inet 192.168.10.13 netmask 255.255.255.0 broadcast 192.168.10.255 [root@Neo_Tang ~]# systemctl stop httpd.service
-
此时用户进行访问
[root@Neo_Tang ~]# for i in {1..10};do curl http://www.neotang.com; done <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1> <h1>So Sorry! Maintance Time! Nginx Server 192.168.1.11!</h1>
3、其它参数说明(down / ip_hash / hash)
http {
upstream webservers {
server 192.168.10.12:80 weight=2 down;
# 相关主机后面加 down,相当于软down,请求不会送达此主机,但此主机并没有down,一般处于维护的目的进行此设置
server 192.168.10.13:80 weight=1 fail_timeout=1 max_fails=3;
server 127.0.0.1:80 backup;
}
}
[root@Neo_Tang conf.d]# vim ../nginx.conf
http {
upstream webservers {
ip_hash; # 同一服务器IP的请求都转向同一后端服务器,等同于 hash $remote_addr
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=1 fail_timeout=1 max_fails=3;
}
}
[root@Neo_Tang conf.d]# vim ../nginx.conf
http {
upstream webservers {
hash $request_uri consistent; # 请求同一URI会被调度到相同的后端主机(客户端服务器做缓存时,特别有用)
server 192.168.10.12:80 weight=2;
server 192.168.10.13:80 weight=1 fail_timeout=1 max_fails=3;
}
}
3.1 hash 示例
-
Nginx 代理服务器配置
http { upstream webservers { hash $request_uri consistent; server 192.168.10.12:80 weight=2; server 192.168.10.13:80 weight=1; #server 127.0.0.1:80 bakup; # backup 不能与 hash 同时使用 } } [root@Neo_Tang ~]# vim /etc/nginx/conf.d/neotang.conf server { listen 80; server_name www.neotang.com; location / { proxy_pass http://webservers; } } [root@Neo_Tang ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@Neo_Tang ~]# nginx -s reload
-
客户端进行访问(发现只要是访问同一网页的请求,一定会被送往同一后端服务器)
[root@neo ~]# for i in {1..5};do for j in {1..5};do curl http://www.neotang.com/index$i.html; done; done <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-2 192.168.10.13 index-3</h1> <h1>HTTP web-server-2 192.168.10.13 index-3</h1> <h1>HTTP web-server-2 192.168.10.13 index-3</h1> <h1>HTTP web-server-2 192.168.10.13 index-3</h1> <h1>HTTP web-server-2 192.168.10.13 index-3</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1>
3.2 ip_hash 示例
-
Nginx 服务器设置
http { upstream webservers { ip_hash; server 192.168.10.12:80 weight=2; server 192.168.10.13:80 weight=1; #server 127.0.0.1:80 bakup; # backup 不能与 hash 同时使用 } } [root@Neo_Tang ~]# vim /etc/nginx/conf.d/neotang.conf server { listen 80; server_name www.neotang.com; location / { proxy_pass http://webservers; } } [root@Neo_Tang ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@Neo_Tang ~]# nginx -s reload
-
客户端进行访问(发现同一客户端服务器访问均被送往同一后端服务器)
[root@neo ~]# for i in {1..5};do for j in {1..5};do curl http://www.neotang.com/index$i.html; done; done <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-1</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-2</h1> <h1>HTTP web-server-1 192.168.10.12 index-3</h1> <h1>HTTP web-server-1 192.168.10.12 index-3</h1> <h1>HTTP web-server-1 192.168.10.12 index-3</h1> <h1>HTTP web-server-1 192.168.10.12 index-3</h1> <h1>HTTP web-server-1 192.168.10.12 index-3</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-4</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1> <h1>HTTP web-server-1 192.168.10.12 index-5</h1>