Linux架構中代理伺服器配置與負載均衡

本期內容概要

  • 代理
  • 負載均衡

內容詳細

1、代理

1.主要作用:
	將流量平均分配

2.代理的方式
	01 正向代理
		外部想要訪問伺服器 先找代理 找到之後還需要找伺服器
		應用:VPN

	02 反向代理
		外部想要訪問伺服器 只需要找代理 不需要找伺服器
		應用:負載均衡

1.1、Nginx代理服務支援的協議

ngx_http_uwsgi_module   :	Python協議

ngx_http_fastcgi_module :	PHP 協議

ngx_http_scgi_module    :	Java協議

ngx_http_v2_module      :	Golang協議

ngx_http_proxy_module   :	HTTP協議

1.2、Nginx代理實踐

1.2.1、部署web01伺服器
# 部署web01

	[root@web01 ~]# cd /etc/nginx/conf.d
	將所有 .conf 文件先打包

	[root@web01 conf.d]# vim game5.conf
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
}

	[root@web01 ~]# systemctl restart nginx
	瀏覽器訪問 192.168.15.7
	正常載入遊戲就是正常部署web01
1.2.2、部署lb01伺服器
# 部署Nginx(編譯安裝 不能yum安裝 否則負載均衡可能無法使用)

1.下載Nginx源程式碼包
	[root@lb01 ~]# wget //nginx.org/download/nginx-1.20.2.tar.gz

2.解壓
	[root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz

3.進入源程式碼目錄
	[root@lb01 ~]# cd nginx-1.20.2

4.安裝依賴包
	[root@lb01 nginx-1.20.2]# yum install openssl openssl-devel zlib zlib-devel -y

5.設置編譯參數
	[root@lb01 nginx-1.20.2]# ./configure  --with-http_gzip_static_module  --with-stream  --with-http_ssl_module

6.編譯
	[root@lb01 nginx-1.20.2]# make

7.安裝
	[root@lb01 nginx-1.20.2]# make install

8.優化
	[root@lb01 nginx-1.20.2]# mkdir /etc/nginx
	[root@lb01 nginx-1.20.2]# mv /usr/local/nginx/conf/* /etc/nginx/
	[root@lb01 nginx-1.20.2]# mkdir /etc/nginx/conf.d

	[root@lb01 nginx-1.20.2]# cd /etc/nginx/conf.d
	[root@lb01 conf.d]# cd ..
	[root@lb01 nginx]# >nginx.conf
	[root@lb01 nginx]# vim nginx.conf
	內容可以從web中複製
	[root@web01 nginx]# cat /etc/nginx/nginx.conf
	將所有內容寫入 
	[root@lb01 nginx]# vim nginx.conf
	但是 user  nginx; 要改為 user  www;

	[root@lb01 nginx]# groupadd www -g 666
	[root@lb01 nginx]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
	[root@lb01 nginx]# vim /usr/lib/systemd/system/nginx.service
	寫入內容可以從web中複製
	[root@web01 nginx]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=//nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

	[root@lb01 nginx]# cd /usr/local/nginx/sbin
	[root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/
	[root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf
	[root@lb01 sbin]# mkdir /var/log/nginx
	[root@lb01 sbin]# systemctl start nginx
	[root@lb01 sbin]# nginx -t


--------------------------------------------------------------------------------------------------------------

# 部署反向代理
	[root@lb01 sbin]# cd /etc/nginx/conf.d
	[root@lb01 conf.d]# vim game.conf
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //172.16.1.7:80;
    }
}

	[root@lb01 conf.d]# systemctl restart nginx

1.3、Nginx代理常用參數

1.3.1、添加發往後端伺服器的請求頭資訊
Syntax:    proxy_set_header field value;
Default:    proxy_set_header Host $http_host;
            proxy_set_header Connection close;
Context:    http, server, location
 
	[root@lb01 conf.d]# cd /etc/nginx/conf.d
	[root@lb01 conf.d]# vim game.conf
	在location / {}內 寫入以下內容:
        # 用戶請求的時候HOST的值是linux.proxy.com, 那麼代理服務會像後端傳遞請求的還是linux.proxy.com
        proxy_set_header Host $http_host;
        # 將$remote_addr的值放進變數X-Real-IP中,$remote_addr的值為客戶端的ip
        proxy_set_header X-Real-IP $remote_addr;
        # 客戶端通過代理服務訪問後端服務, 後端服務通過該變數會記錄真實客戶端地址
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx
1.3.2、代理到後端的TCP連接、響應、返回等超時時間
#nginx代理與後端伺服器連接超時時間(代理連接超時)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
 
#nginx代理等待後端伺服器的響應時間
Syntax:    proxy_read_timeout time;
Default:    proxy_read_timeout 60s;
Context:    http, server, location
 
#後端伺服器數據回傳給nginx代理超時時間
Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location


	[root@lb01 conf.d]# vim game.conf
	在location / {}內 寫入以下內容:
        proxy_connect_timeout 1s;
        proxy_read_timeout 3s;
        proxy_send_timeout 3s;

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx 
1.3.2、proxy_buffer代理緩衝區
#nignx會把後端返回的內容先放到緩衝區當中,然後再返回給客戶端,邊收邊傳, 不是全部接收完再傳給客戶端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
 
#設置nginx代理保存用戶頭資訊的緩衝區大小
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
 
#proxy_buffers 緩衝區
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location

	[root@lb01 conf.d]# vim game.conf
	在location / {}內 寫入以下內容:
        proxy_buffering on;
        proxy_buffer_size 8k;
        proxy_buffers 8 8k;

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx
1.3.3、配置代理優化文件
	[root@lb01 conf.d]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10s;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;

	[root@lb01 conf.d]# vim game.conf
	只保留並修改為以下內容即可:
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //172.16.1.7:80;
        include /etc/nginx/proxy_params;
    }
}
	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

2、負載均衡

# 為什麼要用負載均衡?
	反向代理 只會將外部請求轉發給某一台伺服器
	負載均衡 可以實現將外部請求轉發給多台伺服器

2.1、負載均衡的架構

通過代理將流量按照一定的比例,轉發到後端

2.2、負載均衡的實現

2.2.1、實現
'''將後端服務打包成一個IP連接池'''

1.反向代理
server {
   	listen 80;
   	server_name _;
   	location / {
        proxy_pass //[連接池];
   	}
}

2.IP連接池
upstream [連接池名稱] {
    server [ip]:[port];
    server [ip]:[port];
    server [ip]:[port];
}


# 實現客戶請求轉發至三台web伺服器

01 準備文件(注意在不同的伺服器操作)
	[root@web01 opt]# cd /opt
	[root@web01 opt]# tar -czvf Super_Marie.tar.gz Super_Marie
	[root@web01 opt]# scp Super_Marie.tar.gz 172.16.1.8:/opt/
	[root@web01 opt]# scp Super_Marie.tar.gz 172.16.1.9:/opt/

	[root@web02 opt]# tar -xf Super_Marie.tar.gz

	[root@web03 opt]# tar -xf Super_Marie.tar.gz

	沒有安裝nginx的 執行安裝
	[root@web02 opt]# yum install nginx -y
	[root@web03 opt]# yum install nginx -y

	在web01中
	[root@web01 conf.d]# cd /etc/nginx/conf.d
	[root@web01 conf.d]# scp game5.conf 172.16.1.8:/etc/nginx/conf.d/
	[root@web01 conf.d]# scp game5.conf 172.16.1.9:/etc/nginx/conf.d/

	在web02 和 web03中修改配置文件 保持一致
	[root@web03 ~]# cd /etc/nginx
	[root@web03 nginx]# vim nginx.conf
	[root@web02 ~]# cd /etc/nginx
	[root@web02 nginx]# vim nginx.conf
	將 :user nginx;
	改為 :user www;

	[root@web02 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web02 nginx]# systemctl restart nginx

	[root@web03 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web03 nginx]# systemctl restart nginx

02 測試
	在瀏覽器分別輸入 
	192.168.15.7 | 192.168.15.8 | 192.168.15.9
	正常訪問到遊戲 則正常

	如果有顯示頁面為 前期測試的考試系統頁面 則執行:
	systemctl disable --now httpd
	systemctl restart nginx

	到此 後端伺服器三台都部署完成

03 編輯IP連接池
	[root@lb01 conf.d]# vim game.conf
	upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        include /etc/nginx/proxy_params;
    }
}

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

04 通過監控日誌測試
	[root@web01 conf.d]# tail -f /var/log/nginx/access.log
	[root@web02 nginx]# tail -f /var/log/nginx/access.log
	[root@web03 nginx]# tail -f /var/log/nginx/access.log

	在瀏覽器訪問 192.168.15.5
	在三個web伺服器都顯示日誌 則代表實現了負載均衡

'''
監控日誌:
	tail -f /var/log/nginx/access.log
'''
2.2.2、負載均衡的比例
# 1.輪詢
	默認情況下,Nginx負載均衡就是輪詢狀態
	就是將客戶請求循環發送至每一台伺服器(類似於平均)

	[root@web01 ~]# cd /opt/Super_Marie
	[root@web02 ~]# cd /opt/Super_Marie
	[root@web03 ~]# cd /opt/Super_Marie

	[root@web01 Super_Marie]# echo web01 > web.html
	[root@web02 Super_Marie]# echo web02 > web.html
	[root@web03 Super_Marie]# echo web03 > web.html

	保持配置文件中 存在以下內容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        include /etc/nginx/proxy_params;
    }
}


	瀏覽器訪問:192.168.15.5/web.html
	循環顯示 web01 web02 web03 就正常



# 2.權重
	Nginx中的權重0-100,數字越大,權重越高
	weight數字越大 代表負載均衡會更大比例的將客戶請求轉發到對應的伺服器

	保持配置文件中 存在以下內容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 weight=9;
    server 172.16.1.8:80 weight=5;
    server 172.16.1.9:80 weight=1;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        include /etc/nginx/proxy_params;
    }
}
	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

	瀏覽器訪問:192.168.15.5/web.html
	刷新十次 6次顯示 web01
	3次 顯示 web02
	1次 顯示 web03


# 3.ip_hash
	每一個IP固定訪問某一個後端
	當有一個IP訪問一台伺服器時 該IP無論刷新多少次 都只會訪問該伺服器 其他IP只能訪問其他伺服器 

	保持配置文件中 存在以下內容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
    ip_hash;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx



	瀏覽器訪問:192.168.15.5/web.html
	如果首次顯示 web01
	那麼永遠都是 web01
	每一個IP固定訪問某一個後端

2.3、負載均衡後端狀態

狀態 概述
down 當前的server暫時不參與負載均衡
backup 預留的備份伺服器
max_fails 允許請求失敗的次數
fail_timeout 經過max_fails失敗後, 服務暫停時間
# 1.down狀態
	暫時不分配流量

	保持配置文件中 存在以下內容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 down;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx


	瀏覽器訪問:192.168.15.5/web.html
	永遠都不會顯示 web01 因為已經暫停流量轉發給 web01了
	


# 2.backup狀態
	只有當(沒有backup狀態的)所有的機器全部宕機,才能啟動

	保持配置文件中 存在以下內容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 backup;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx


	瀏覽器訪問:192.168.15.5/web.html
	在 web02 web03 正常啟動時
	永遠都不會顯示 web01 
	在其餘所有伺服器宕機時 
	此時客戶請求就只會訪問 web01



# 3.max_fails、fail_timeout(要連用)
	要與 proxy_next_upstream:後端錯誤標識 連用

	保持配置文件中 存在以下內容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass //supermarie;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

	瀏覽器訪問:192.168.15.5/web.html
	可以嘗試重啟 web01 | 置空 web.html
	就只會顯示 web02 web03


'''
proxy_next_upstream 可以跟的參數:

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

error   :	與伺服器建立連接,向其傳遞請求或讀取響應頭時發生錯誤;
timeout :	在與伺服器建立連接,向其傳遞請求或讀取響應頭時發生超時;
invalid_header :	伺服器返回空的或無效的響應;
http_500 :	伺服器返回程式碼為500的響應;
http_502 :	伺服器返回程式碼為502的響應;
http_503 :	伺服器返回程式碼為503的響應;
http_504 :	伺服器返回程式碼504的響應;
http_403 :	伺服器返回程式碼為403的響應;
http_404 :	伺服器返回程式碼為404的響應;
http_429 :	伺服器返回程式碼為429的響應(1.11.13);

non_idempotent :	通常,請求與 非冪等 方法(POST,LOCK,PATCH)不傳遞到請求是否已被發送到上游伺服器(1.9.13)的下一個伺服器; 啟用此選項顯式允許重試此類請求;

off :	禁用將請求傳遞給下一個伺服器。
'''

2.4、負載均衡部署BBS

2.4.1、部署後端服務
# 部署Python
	1.創建用戶
	[root@web02 ~]# groupadd django -g 888
	[root@web02 ~]# useradd django -u 888 -g 888 -r -M -s /bin/sh
    
    [root@web03 ~]# groupadd django -g 888
	[root@web03 ~]# useradd django -u 888 -g 888 -r -M -s /bin/sh
    
    2.安裝依賴軟體
	[root@web02 ~]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
	[root@web03 ~]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
    
    
# 部署Django和uwsgi 
	3.安裝Django和uwsgi
	[root@web01 ~]# scp bbs.zip 172.16.1.8:/opt/
	[root@web01 ~]# scp bbs.zip 172.16.1.9:/opt/
    
	[root@web02 ~]# pip3 install django==1.11
	[root@web02 ~]# pip3 install uwsgi
	[root@web02 ~]# pip3 install pymysql
    
	[root@web03 ~]# pip3 install django==1.11
	[root@web03 ~]# pip3 install uwsgi
	[root@web03 ~]# pip3 install pymysql
    
	4.創建項目
	[root@web02 ~]# cd /opt/
	[root@web02 opt]# unzip bbs.zip
	[root@web02 opt]# cd bbs
	[root@web02 bbs]# vim bbs/settings.py
	修改下面兩處
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}


	[root@web03 ~]# cd /opt/
	[root@web03 opt]# unzip bbs.zip
	[root@web03 opt]# cd bbs
	[root@web03 bbs]# vim bbs/settings.py
	修改下面兩處
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}
    
    
    
# 啟動測試
[root@web02 bbs]# python3 manage.py runserver 0.0.0.0:8000
[root@web03 bbs]# python3 manage.py runserver 0.0.0.0:8000


# 配置並啟動
	5.編輯項目配置文件
	[root@web02 bbs]# vim /opt/bbs/myweb_uwsgi.ini

	[root@web03 bbs]# vim /opt/bbs/myweb_uwsgi.ini
    
    
	6.啟動uwsgi
	測試
	[root@web02 bbs]# uwsgi --ini myweb_uwsgi.ini --uid 666
	啟動
	[root@web02 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
[uWSGI] getting INI configuration from myweb_uwsgi.ini


	[root@web03 bbs]# uwsgi --ini myweb_uwsgi.ini --uid 666
	啟動
	[root@web02 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
[uWSGI] getting INI configuration from myweb_uwsgi.ini

-d    :	以守護進程方式運行
--ini :	指定配置文件路徑
--uid :	指定uid
    
    
	7.編輯Nginx配置文件
	[root@web01 conf.d]# vim /etc/nginx/conf.d/python.conf
	[root@web02 bbs]# vim /etc/nginx/conf.d/python.conf
	[root@web03 bbs]# vim /etc/nginx/conf.d/python.conf
	寫入以下內容:
    server {
    listen 80;
    server_name py.test.com;
    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
        uwsgi_param UWSGI_SCRIPT bbs.wsgi;
        uwsgi_param UWSGI_CHDIR /opt/bbs;
        index  index.html index.htm;
        client_max_body_size 35m;
    }
}
    
	8.重啟Nginx配置
	[root@web01 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@web01 conf.d]# systemctl restart nginx
    
    
    [root@web02 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@web02 conf.d]# systemctl restart nginx

    
	[root@web03 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@web03 conf.d]# systemctl restart nginx
2.4.2、部署負載均衡
[root@lb01 conf.d]# cd /etc/nginx/conf.d
[root@lb01 conf.d]# cp game.conf python.conf


[root@lb01 conf.d]# vim python.conf
upstream bbs {
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}

server {
    listen 80;
    server_name py.test.com;
    location / {
        proxy_pass //bbs;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}

瀏覽器連接 py.test.com 如下圖就正常了

image