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因此會被調度到同一台服務器上

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM