1. 程式人生 > >Nginx反向代理和負載均衡應用實戰

Nginx反向代理和負載均衡應用實戰

linux 運維

本文檔主要介紹CentOS6.9系統用Nginx服務實現負載均衡功能


基本流程:

  1. Nginx負載均衡的環境準備

  2. Nginx實現一個簡單的負載均衡

  3. Nginx負載均衡配置實戰

4.Nginx負載均衡監測節點狀態





步驟一:Nginx負載均衡的環境準備



1.硬件準備(準備四臺VM虛擬機或物理服務器,兩臺做負載均衡,兩臺做web,如下表所示)

hostname IP 說明與介紹
fzMaster 192.168.1.59 Nginx主負載均衡器
fzBackup 192.168.1.58 Nginx備負載均衡器
web01 192.168.1.23 web01服務器
web02 192.168.1.25 web02服務器


2.系統和軟件準備

系統:

[root@fzMaster ~]# cat /etc/redhat-release

CentOS release 6.9 (Final)


[root@fzMaster ~]# uname -r

2.6.32-696.20.1.el6.x86_64


軟件: nginx-1.6.3.tar.gz (下載鏈接:http://nginx.org/download/nginx-1.6.3.tar.gz;其它版本只需改存在的版本號即可)


3.四臺服務器上都要源碼安裝nginx

1)yum安裝依賴軟件包

配置yum源請參考 http://blog.51cto.com/13707680/2104644

yum install openssl openssl-devel pcre pcre-devel -y

rpm -qa openssl openssl-devel pcre pcre-devel (命令執行完後檢查,這是運維專業的規範)


2)源碼安裝Nginx,命令集如下:

mkdir -p /home/ywxi/tools

cd /home/ywxi/tools/

wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

ls -l nginx-1.6.3.tar.gz

useradd nginx -s /sbin/nologin -M

tar xf nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module

make

make install

echo $?

ln -s /application/nginx-1.6.3/ /application/nginx

ll /application/nginx

ls -l /application/nginx/

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx

lsof -i :80

netstat -tnlp | grep 80


3)配置用於測試的web服務(web01和web02都要配置)

[root@web01 conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

log_format main '$remote_addr - $remote_user [$time_local] "$request"'

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

server {

listen 80;

server_name ywxi.com;

location / {

root html/ywxi;

index index.html index.htm;

}

access_log logs/access_ywxi.log main;

}

server {

listen 80;

server_name back.com;

location / {

root html/back;

index index.html index.htm;

}

access_log logs/access_back.log main;

}

}


3)向nginx代碼目錄下放置數據,用來測試

mkdir /application/nginx/html/{ywxi,back} (兩臺web都執行)


web01這臺主機上的操作

echo "192.168.1.23 web01 back" > /application/nginx/html/back/index.html

echo "192.168.1.23 web01 ywxi" > /application/nginx/html/ywxi/index.html


web02這臺主機上的操作

echo "192.168.1.25 web02 back" > /application/nginx/html/back/index.html

echo "192.168.1.25 web02 ywxi" > /application/nginx/html/ywxi/index.html


添加好後用fzMaster這臺服務器測試

[root@fzMaster ~]# tail -2 /etc/hosts

192.168.1.25 ywxi.com

192.168.1.23 back.com

[root@fzMaster ~]# curl ywxi.com

192.168.1.25 web02 ywxi

[root@fzMaster ~]# curl back.com

192.168.1.23 web01 back

通過上面這些配置就實現了兩臺Web服務器基於域名的虛擬主機配置了。




步驟二:Nginx實現一個簡單的負載均衡

第一臺Nginx負載均衡器的準備信息

hostname IP 說明與介紹
fzMaster 192.168.1.59 Nginx主負載均衡器


下面進行一個簡單的Nginx負載均衡配置,代理ywxi.com服務,節點為web01和web02。nginx.conf配置文件內容如下:

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream www_server_pools { #這裏是定義web服務器池,包含了23和25兩個web節點

server 192.168.1.23:80 weight=1;

server 192.168.1.25:80 weight=1;

}

server { #定義虛擬主機

listen 80;

server_name ywxi.com;

location / {

proxy_pass http://www_server_pools; #訪問ywxi.com,請求會發送給www_server_pools裏面的節點

}

}

}


報錯:[root@fzMaster conf]# nginx -s reload

nginx: [error] invalid PID number "" in "/application/nginx-1.6.3/logs/nginx.pid"

解決:ps -ef | grep nginx|head -1 |awk '{print $2}' > /application/nginx-1.6.3/logs/nginx.pid


netstat -tnlp |grep nginx

然後,檢查負載均衡測試結果。用linux作為客戶端來測試:

[root@fzMaster conf]# tail -1 /etc/hosts

192.168.1.59 ywxi.com #192.168.1.59為主負載均衡器的IP

[root@fzMaster conf]# curl ywxi.com

192.168.1.23 web01 ywxi

[root@fzMaster conf]# curl ywxi.com

192.168.1.25 web02 ywxi

[root@fzMaster conf]# curl ywxi.com

192.168.1.23 web01 ywxi

[root@fzMaster conf]# curl ywxi.com

192.168.1.25 web02 ywxi


可以看見請求都是一比一的分配,現在我們假設宕掉兩臺web服務器:

[root@fzMaster conf]# curl ywxi.com

<html>

<head><title>502 Bad Gateway</title></head>

<body bgcolor="white">

<center><h1>502 Bad Gateway</h1></center>

<hr><center>nginx/1.6.3</center>

</body>

</html>

Nginx代理下面沒有了節點,因為Nginx向用戶報告了502錯誤。


如果只宕掉一臺web,代理就不會去訪問宕機的web服務,只會訪問正常的web服務器

[root@fzMaster conf]# curl ywxi.com

192.168.1.23 web01 ywxi

[root@fzMaster conf]# curl ywxi.com

192.168.1.25 web02 ywxi

[root@fzMaster conf]# curl ywxi.com

192.168.1.25 web02 ywxi

[root@fzMaster conf]# curl ywxi.com

192.168.1.25 web02 ywxi

到這裏一臺簡單的Ngxin負載均衡器就搭建好了。




步驟三:Nginx負載均衡配置實戰

Nginx Web 服務器節點信息

hostname IP 說明與介紹
web01 192.168.1.23 web01服務器
web02 192.168.1.25 web02服務器

前文已經配置好兩臺web的nginx的配置文件

1)創建站點目錄及對應測試文件,命令如下:

[root@web01 conf]# for n in ywxi back;do cd /application/nginx/html/;mkdir -p $n;echo "$n.com23" >$n/index.html;done

[root@web02 html]# for n in ywxi back;do cd /application/nginx/html/;mkdir -p $n;echo "$n.com25" >$n/index.html;done

[root@web01 html]# cat ywxi/index.html

ywxi.com23

[root@web01 html]# cat back/index.html

back.com23


2)兩臺都做同樣的操作:

ln -s /application/nginx/sbin/nginx /bin/nginx

[root@web01 html]# nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@web01 html]# nginx -s reload

[root@web01 html]# netstat -tnlp | grep 80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12950/nginx


3)把域名加入hosts解析

[root@web01 html]# echo "192.168.1.23 ywxi.com " >> /etc/hosts

[root@web01 html]# echo "192.168.1.23 back.com " >> /etc/hosts

[root@web01 html]# tail -2 /etc/hosts

192.168.1.23 ywxi.com

192.168.1.23 back.com

[root@web02 html]# echo "192.168.1.25 ywxi.com " >> /etc/hosts

[root@web02 html]# echo "192.168.1.25 back.com " >> /etc/hosts

[root@web02 html]# tail -2 /etc/hosts

192.168.1.25 ywxi.com

192.168.1.25 back.com

web01測試:

[root@web01 html]# curl ywxi.com

ywxi.com23

[root@web01 html]# curl back.com

back.com23

web02測試:

[root@web02 back]# curl back.com

back.com25

[root@web02 back]# curl ywxi.com

ywxi.com25

到這裏,搭建配置虛擬主機就成功了。


4)Nginx負載均衡實踐

主Nginx負載服務器配置如下:

[root@fzMaster conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream www_server_pools { #這裏是定義web服務器池,包含了23和25兩個web節點

server 192.168.1.23:80 weight=1;

server 192.168.1.25:80 weight=1;

}

server { #定義虛擬主機

listen 80;

server_name back.com;

location / {

proxy_pass http://www_server_pools; #訪問ywxi.com,請求會發送給www_server_pools裏面的節點

}

}

}

配置hosts解析到代理的IP上,然後重新加載下服務,訪問測試:

[root@fzMaster conf]# tail -2 /etc/hosts

192.168.1.59 ywxi.com

192.168.1.59 back.com

[root@fzMaster conf]# nginx -s reload

[root@fzMaster conf]# curl back.com

ywxi.com23

[root@fzMaster conf]# curl back.com

ywxi.com25

從測試結果來看,已經實現了反向代理、負載均衡功能,但是有一個特殊問題,出來的不是帶back.com的內容,而是ywxi.com的內容。(提示:這裏代理了多個虛擬主機)

解決:

加一行配置即可 proxy_set_header Host $host;

[root@fzMaster conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream www_server_pools {

server 192.168.1.23 weight=1;

server 192.168.1.25 weight=1;

}

server {

listen 80;

server_name back.com;

location / {

proxy_pass http://www_server_pools;

proxy_set_header Host $host; #在代理向後端服務器發送的http請求頭中加入hosts字段信息,用於當後端服務器配置有多個虛擬主機時,可以識別代理的是哪個虛擬主機。這是節點服務器多虛擬主機時的關鍵配置

}

}

}



重新加載Nginx服務,並用curl測試檢查下,結果如下:

[root@fzMaster conf]# nginx -s reload

[root@fzMaster conf]# curl back.com

back.com23

[root@fzMaster conf]# curl back.com

back.com25

可以看到這次訪問的結果與訪問的域名就完全對應上了,這樣代理多虛擬主機的節點服務器就正常了。


5)經過反向代理後的節點服務器記錄用戶真實IP實戰


proxy_set_header X-Forwarded-For $remote_addr; #在代理向後端服務器發送的http請求頭中加入X-Forwarded-For字段信息,用於後端服務器程序,日誌等接收記錄真實用戶的IP,而不是代理服務器的IP。 這個地方對應web01和web02的Nginx配置文件中"$http_x_forwarded_for"'日誌參數

配置如下:

[root@fzMaster conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream www_server_pools {

server 192.168.1.23 weight=1;

server 192.168.1.25 weight=1;

}

server {

listen 80;

server_name back.com;

location / {

proxy_pass http://www_server_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

}

配置了日誌參數後,用客戶端192.168.1.10測試訪問:

[root@web01 conf]# tail -2 /application/nginx/logs/access_back.log

192.168.1.59 - - [24/Apr/2018:16:05:15 +0800] "GET / HTTP/1.0"200 11 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "192.168.1.10"

192.168.1.59 - - [24/Apr/2018:16:05:16 +0800] "GET / HTTP/1.0"200 11 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "192.168.1.10"


192.168.1.59為反向代理的IP,192.168.1.10為客戶端的真實IP。到這裏,Nginx負載均衡配置實戰已經完成了。




步驟四:Nginx負載均衡監測節點狀態

Tengine(Nginx的分支)模塊nginx_upstream_check_module,用於提供主動式後端服務器健康檢查。通過它可以檢測後端realserver的健康狀態,如果後端realserver不可用,則所有請求就不會轉發到該節點上。我們這裏用Nginx打補丁的方式將該模塊添加到Nginx中。

https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master #模塊下載地址

1)安裝nginx_upstream_check_module模塊。

[root@fzMaster conf]# nginx -v

nginx version: nginx/1.6.3


命令集如下:

cd /home/ywxi/tools/

unzip nginx_upstream_check_module-master.zip

patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch

cd nginx-1.6.3

./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx_upstream_check_module-master

make #如果是新裝的nginx接下來需要make install ,這個是給已經安裝的nginx系統打監控補丁就不需要make install了。make的作用就是重新生成Nginx二進制啟動命令而已

mv /application/nginx/sbin/nginx{,.ori}

cp ./objs/nginx /application/nginx/sbin/

nginx -t


[root@fzMaster nginx-1.6.3]# nginx -V

nginx version: nginx/1.6.3

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx_upstream_check_module-master


2)配置Nginx健康檢查,配置文件如下:

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream www_server_pools {

server 192.168.1.23:80 weight=1;

server 192.168.1.25:80 weight=1;

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}

server {

listen 80;

server_name back.com;

location / {

proxy_pass http://www_server_pools;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

location /status {

check_status;

access_log off;

}

}

}

[root@fzMaster conf]# nginx -s stop #此處必須重啟,重新加載不生效

[root@fzMaster conf]# nginx


訪問http://192.168.1.59/status頁面如下。這是正常狀態下Nginx節點健康檢查狀態

技術分享圖片

[root@web01 conf]# nginx -s stop #假設宕掉一臺web01

訪問http://192.168.1.59/status頁面如下。這是不正常狀態下Nginx節點健康檢查狀態

技術分享圖片

到這裏本文章就完成了。


























































Nginx反向代理和負載均衡應用實戰