Nginx反向代理功能-使用upstream指令反向代理多台web服务器案例

        Nginx反向代理功能-使用upstream指令反向代理多台web服务器案例

                                            作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

  Nginx可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能。接下来我们一起学习一下吧。

 

一.http upstream常用配置参数

server支持的parameters如下:
  weight=number 
    设置权重,默认为1。
  max_conns=number 
    给当前server设置最大活动链接数,默认为0表示没有限制。
  max_fails=number 
    对后端服务器连续监测失败多少次就标记为不可用。
  fail_timeout=time 
    对后端服务器的单次监测超时时间,默认为10秒。
  backup 
    设置为备份服务器,当所有服务器不可用时将重新启用次服务器。
  down 
    标记为down状态。
  resolve 
    当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx。
  hash KEY consistent;
    基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。所谓取模运算,就是计算两个数相除之后的余数,比如10%7=3, 7%4=3
  hash $request_uri consistent; 
    基于用户请求的uri做hash
  ip_hash;
    源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持。
  least_conn;
    最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器。

博主推荐阅读:
  https://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

 

二.试验数据准备

1>.试验操作环境

[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.0-957.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        311M        6.3G         11M        1.0G        7.0G
Swap:          7.9G          0B        7.9G
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.30.1.101 node101.yinzhengjie.org.cn node101.yinzhengjie.com
172.30.1.102 node102.yinzhengjie.org.cn
172.30.1.103 node103.yinzhengjie.org.cn
172.30.1.104 node104.yinzhengjie.org.cn
172.30.1.105 node105.yinzhengjie.org.cn
172.30.1.106 node106.yinzhengjie.org.cn
172.30.1.107 node107.yinzhengjie.org.cn
172.30.1.108 node108.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.试验架构说明

node101.yinzhengjie.org.cn
  Nginx反向代理服务器

node105.yinzhengjie.org.cn
  使用curl命令模拟客户端
node106.yinzhengjie.org.cn   Apache Httpd服务器
node107.yinzhengjie.org.cn   Apache Httpd服务器
node108.yinzhengjie.org.cn
  Apache Hattpd服务器

3>.node106.yinzhengjie.org.cn操作

[root@node106.yinzhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                    | 3.6 kB  00:00:00     
extras                                                                                  | 2.9 kB  00:00:00     
updates                                                                                 | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                           | 165 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                       | 153 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                                                   | 5.9 MB  00:00:09     
(4/4): base/7/x86_64/primary_db                                                                                                                      | 6.0 MB  00:00:10     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                   Arch                                 Version                                            Repository                          Size
============================================================================================================================================================================
Installing:
 httpd                                     x86_64                               2.4.6-90.el7.centos                                base                               2.7 M
Installing for dependencies:
 apr                                       x86_64                               1.4.8-5.el7                                        base                               103 k
 apr-util                                  x86_64                               1.5.2-6.el7                                        base                                92 k
 httpd-tools                               x86_64                               2.4.6-90.el7.centos                                base                                91 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): apr-1.4.8-5.el7.x86_64.rpm                                                                                                                    | 103 kB  00:00:00     
(2/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                                                                               |  92 kB  00:00:00     
(3/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                                                                                    |  91 kB  00:00:05     
(4/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                                                                                          | 2.7 MB  00:00:14     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       206 kB/s | 3.0 MB  00:00:14     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                                                                                   1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                                                                              2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                   3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                         4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                                                                                   1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                   2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                                                                              3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                         4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                                                                                        

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                          apr-util.x86_64 0:1.5.2-6.el7                          httpd-tools.x86_64 0:2.4.6-90.el7.centos                         

Complete!
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# yum -y install httpd
[root@node106.yinzhengjie.org.cn ~]# mkdir /var/www/html/app01
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# echo "node106.yinzhengjie.org.cn" > /var/www/html/app01/index.html
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# echo "linux106" > /var/www/html/app01/linux.html
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /var/www/html/app01/
total 8
-rw-r--r-- 1 root root 27 Dec 27 20:07 index.html
-rw-r--r-- 1 root root  9 Dec 27 21:05 linux.html
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# cat /var/www/html/app01/index.html
node106.yinzhengjie.org.cn
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# cat /var/www/html/app01/linux.html 
linux106
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# systemctl start httpd
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN      0      128                                     *:80                                                  *:*                  
LISTEN      0      128                                     *:22                                                  *:*                  
LISTEN      0      128                                    :::22                                                 :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]#

4>.node107.yinzhengjie.org.cn操作

[root@node107.yinzhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                    | 3.6 kB  00:00:00     
extras                                                                                  | 2.9 kB  00:00:00     
updates                                                                                 | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                           | 165 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                       | 153 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                                                   | 5.9 MB  00:00:09     
(4/4): base/7/x86_64/primary_db                                                                                                                      | 6.0 MB  00:00:10     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                   Arch                                 Version                                            Repository                          Size
============================================================================================================================================================================
Installing:
 httpd                                     x86_64                               2.4.6-90.el7.centos                                base                               2.7 M
Installing for dependencies:
 apr                                       x86_64                               1.4.8-5.el7                                        base                               103 k
 apr-util                                  x86_64                               1.5.2-6.el7                                        base                                92 k
 httpd-tools                               x86_64                               2.4.6-90.el7.centos                                base                                91 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): apr-1.4.8-5.el7.x86_64.rpm                                                                                                                    | 103 kB  00:00:00     
(2/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                                                                               |  92 kB  00:00:00     
(3/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                                                                                    |  91 kB  00:00:05     
(4/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                                                                                          | 2.7 MB  00:00:14     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       206 kB/s | 3.0 MB  00:00:14     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                                                                                   1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                                                                              2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                   3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                         4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                                                                                   1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                   2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                                                                              3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                         4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                                                                                        

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                          apr-util.x86_64 0:1.5.2-6.el7                          httpd-tools.x86_64 0:2.4.6-90.el7.centos                         

Complete!
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# yum -y install httpd 
[root@node107.yizhengjie.org.cn ~]# mkdir /var/www/html/app01
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# echo "node107.yinzhengjie.org.cn" > /var/www/html/app01/index.html
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# echo "linux107" > /var/www/html/app01/linux.html
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# ll /var/www/html/app01/
total 8
-rw-r--r-- 1 root root 27 Dec 27 20:05 index.html
-rw-r--r-- 1 root root  9 Dec 27 21:07 linux.html
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# cat /var/www/html/app01/linux.html 
linux107
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# cat /var/www/html/app01/index.html 
node107.yinzhengjie.org.cn
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# systemctl start httpd
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node107.yizhengjie.org.cn ~]#

5>.node108.yinzhengjie.org.cn操作

[root@node108.yinzhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                    | 3.6 kB  00:00:00     
extras                                                                                  | 2.9 kB  00:00:00     
updates                                                                                 | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                           | 165 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                       | 153 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                                                   | 5.9 MB  00:00:09     
(4/4): base/7/x86_64/primary_db                                                                                                                      | 6.0 MB  00:00:10     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                   Arch                                 Version                                            Repository                          Size
============================================================================================================================================================================
Installing:
 httpd                                     x86_64                               2.4.6-90.el7.centos                                base                               2.7 M
Installing for dependencies:
 apr                                       x86_64                               1.4.8-5.el7                                        base                               103 k
 apr-util                                  x86_64                               1.5.2-6.el7                                        base                                92 k
 httpd-tools                               x86_64                               2.4.6-90.el7.centos                                base                                91 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): apr-1.4.8-5.el7.x86_64.rpm                                                                                                                    | 103 kB  00:00:00     
(2/4): apr-util-1.5.2-6.el7.x86_64.rpm                                                                                                               |  92 kB  00:00:00     
(3/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                                                                                                    |  91 kB  00:00:05     
(4/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                                                                                          | 2.7 MB  00:00:14     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       206 kB/s | 3.0 MB  00:00:14     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                                                                                                   1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                                                                              2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                   3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                         4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                                                                                                   1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                                                                                                   2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                                                                              3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                                                                                         4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                                                                                        

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                          apr-util.x86_64 0:1.5.2-6.el7                          httpd-tools.x86_64 0:2.4.6-90.el7.centos                         

Complete!
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# yum -y install httpd
[root@node108.yinzhengjie.org.cn ~]# mkdir /var/www/html/app01
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# echo "node108.yinzhengjie.org.cn" > /var/www/html/app01/index.html
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# echo "linux108" > /var/www/html/app01/linux.html
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# ll /var/www/html/app01/
total 8
-rw-r--r-- 1 root root 27 Dec 27 20:08 index.html
-rw-r--r-- 1 root root  9 Dec 27 21:09 linux.html
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# cat /var/www/html/app01/linux.html 
linux108
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# cat /var/www/html/app01/index.html 
node108.yinzhengjie.org.cn
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# systemctl start httpd
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:80                                               *:*                  
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]#

 

三.在Nginx上配置服务器轮询案例(当权重一样的两个节点默认就是轮询模式,生产环境中推荐使用轮询策略)

1>.编辑Nginx的主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
   
http {
     include       mime.types;
       
     default_type  text/html;
    
     server_tokens off; 
      
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti
me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
 
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
  
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

2>.编辑nginx的子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
upstream webserver {
    server 172.30.1.106:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.107:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.108:80 weight=1 fail_timeout=5s max_fails=3 backup;
}


server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx/static/cn;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }

    location /app01 {
        proxy_pass http://webserver;
        proxy_connect_timeout 60s;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.重新加载nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     21509     1  0 Dec26 ?        00:00:00 nginx: master process nginx
nginx    22571 21509  0 18:43 ?        00:00:01 nginx: worker process
nginx    22572 21509  0 18:43 ?        00:00:01 nginx: worker process
nginx    22573 21509  0 18:43 ?        00:00:00 nginx: worker process
nginx    22574 21509  0 18:43 ?        00:00:01 nginx: worker process
nginx    22575 21509  0 18:43 ?        00:00:00 nginx: cache manager process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     21509     1  0 Dec26 ?        00:00:00 nginx: master process nginx
nginx    22800 21509  4 19:55 ?        00:00:00 nginx: worker process
nginx    22801 21509  6 19:55 ?        00:00:00 nginx: worker process
nginx    22802 21509  4 19:55 ?        00:00:00 nginx: worker process
nginx    22803 21509  6 19:55 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

4>.node105.yinzhengjie.org.cn模拟客户端访问

[root@node105.yinzhengjie.org.cn ~]# while true;do curl -L http://node101.yinzhengjie.org.cn/app01/index.html;sleep 1.5;done

5>.让node106.yinzhengjie.org.cn节点宕机,观察输出结果,此时node108.yinzhengjie.org.cn节点并未加入集群

[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:80                                 *:*                  
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# systemctl stop httpd
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

6>.让node107.yinzhengjie.org.cn节点也宕机,观察输出结果,此时node108.yinzhengjie.org.cn立即加入集群

[root@node107.yizhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:80                                               *:*                  
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# systemctl stop httpd
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
[root@node107.yizhengjie.org.cn ~]# 

 

四.基于源IP地址的hash调度案例(生产环境中并不推荐使用,会导致负载均衡器调度不均衡的情况)

1>.修改nginx的子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
upstream webserver {
    server 172.30.1.106:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.107:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.108:80 weight=1 fail_timeout=5s max_fails=3 backup;
    ip_hash;
}


server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx/static/cn;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }

    location /app01 {
        proxy_pass http://webserver;
        proxy_connect_timeout 60s;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.重新加载nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep 
root     22837     1  0 19:57 ?        00:00:00 nginx: master process nginx
nginx    22838 22837  0 19:57 ?        00:00:00 nginx: worker process
nginx    22839 22837  0 19:57 ?        00:00:00 nginx: worker process
nginx    22840 22837  0 19:57 ?        00:00:00 nginx: worker process
nginx    22841 22837  0 19:57 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep 
root     22837     1  0 19:57 ?        00:00:00 nginx: master process nginx
nginx    23006 22837  1 20:09 ?        00:00:00 nginx: worker process
nginx    23007 22837  1 20:09 ?        00:00:00 nginx: worker process
nginx    23008 22837  1 20:09 ?        00:00:00 nginx: worker process
nginx    23009 22837  1 20:09 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.node105.yinzhengjie.org.cn访问nginx观察调度情况,结果同一个IP地址的请求始终打到了同一台web服务器。 

4>.让node106.yinzhengjie.org.cn节点宕机,观察输出结果,此时node108.yinzhengjie.org.cn节点并未加入集群

[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:80                                 *:*                  
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# systemctl stop httpd
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

5>.让node107.yinzhengjie.org.cn节点也宕机,观察输出结果,此时node108.yinzhengjie.org.cn立即加入集群

[root@node107.yizhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:80                                               *:*                  
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# systemctl stop httpd
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# 

 

五.最少连接数策略(哪个节点连接数少就把请求调度给哪台服务器,这就是最少链接数策略,如果后端两台web服务器的配置一样的话该算法推荐使用)

1>.修改子配置文件 

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
upstream webserver {
    server 172.30.1.106:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.107:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.108:80 weight=1 fail_timeout=5s max_fails=3 backup;
    #ip_hash;
    least_conn;
}


server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx/static/cn;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }

    location /app01 {
        proxy_pass http://webserver;
        proxy_connect_timeout 60s;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

2>.重新加载nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     22837     1  0 19:57 ?        00:00:00 nginx: master process nginx
nginx    23006 22837  0 20:09 ?        00:00:00 nginx: worker process
nginx    23007 22837  0 20:09 ?        00:00:00 nginx: worker process
nginx    23008 22837  0 20:09 ?        00:00:00 nginx: worker process
nginx    23009 22837  0 20:09 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     22837     1  0 19:57 ?        00:00:00 nginx: master process nginx
nginx    23031 22837  0 20:29 ?        00:00:00 nginx: worker process
nginx    23032 22837  1 20:29 ?        00:00:00 nginx: worker process
nginx    23033 22837  1 20:29 ?        00:00:00 nginx: worker process
nginx    23034 22837  1 20:29 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

3>.在node105.yinzhengjie.org.cn节点测试

  看输出结果是否和轮询优点类似?不过,需要注意的是,无论是轮询还是最小连接数算法都解决不了会话保持的问题哟~

 

六.基于用户请求的uri做hash(这种策略一般是多用于缓存的)

1>.修改nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
upstream webserver {
    server 172.30.1.106:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.107:80 weight=1 fail_timeout=5s max_fails=3;
    server 172.30.1.108:80 weight=1 fail_timeout=5s max_fails=3 backup;
    #ip_hash;
    #least_conn;
    hash $request_uri consistent;
}


server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx/static/cn;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }

    location /app01 {
        proxy_pass http://webserver;
        proxy_connect_timeout 60s;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.重新加载nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     22837     1  0 19:57 ?        00:00:00 nginx: master process nginx
nginx    23031 22837  0 20:29 ?        00:00:00 nginx: worker process
nginx    23032 22837  0 20:29 ?        00:00:00 nginx: worker process
nginx    23033 22837  0 20:29 ?        00:00:00 nginx: worker process
nginx    23034 22837  0 20:29 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     22837     1  0 19:57 ?        00:00:00 nginx: master process nginx
nginx    23047 22837  0 20:39 ?        00:00:00 nginx: worker process
nginx    23048 22837  0 20:39 ?        00:00:00 nginx: worker process
nginx    23049 22837  0 20:39 ?        00:00:00 nginx: worker process
nginx    23050 22837  0 20:39 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.node105.yinzhengjie.org.cn访问"http://node101.yinzhengjie.org.cn/app01/index.html",由于访问的是同一个url因此会被调度到同一台服务器上

4>.node105.yinzhengjie.org.cn访问"http://node101.yinzhengjie.org.cn/app01/linux.html",由于访问的是同一个url因此会被调度到同一台服务器上

 

posted @ 2019-12-26 21:55  尹正杰  阅读(3442)  评论(0编辑  收藏  举报