前言;
上篇博文《keepalived_nginx實現discuz負載均衡和高可用》講到,由於nginx將health_check功能放入到了商業版本,導致社區版本的nginx進行負載均衡,無法對後端的RS主機進行健康狀態檢測,所以現在準備使用tengine來取代nginx。我們只需要將前一章節VS主機上的nginx替換爲tengine即可。
配置:
Host VS1
卸載nginx,安裝tengine
# yum remove -y nginx # yum groupinstall -y "Development tools" "Server Platform Development" # yum install -y pcre-devel # wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz # tar -xf tengine-2.0.3.tar.gz # cd tengine-2.0.3 # ./configure # make && make install # echo "PATH=/usr/local/nginx/sbin/:$PATH" > /etc/profile.d/nginx.sh # exec bash
創建sorry_server頁面文件
# cp /usr/local/nginx/html/index.html{,.bak} # echo "服務維護中,請稍後訪問." > /usr/local/nginx/html/index.html
修改tengine配置文件
# vim /usr/local/nginx/conf/nginx.conf http{ ... # 上游服務器, 即後端RS服務器. upstream backend { server 10.0.0.101 weight=1; server 10.0.0.102 weight=1; # sorry_server server 10.0.0.111:8080 backup; server 10.0.0.112:8080 backup; # 健康檢查 check interval=3000 rise=1 fall=3 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } server { listen 80; server_name localhost; # 當nginx將php代碼送至後端RS處理時請求頭中的Host值會是backend. # php代碼在RS上處理時,其內部代碼會去請求圖片/層疊樣式表等靜態資源以填充頁面. # 而php代碼去請求靜態資源時使用的是如http://backend/xxxx.gif這樣的url,自然是取不到的. # 所以我們要在nginx向後代理遇到Host爲backend時,將其轉換爲127.0.0.1. set $my_host $http_host; if ($http_host = "backend") { set $my_host "127.0.0.1"; } location / { proxy_pass http://backend; proxy_set_header Host $my_host; } } server { listen 8080; server_name localhost; charset utf-8; root /usr/local/nginx/html; index index.html index.htm; # sorry_server僅提供主頁面, 訪問其它資源也轉至主頁面. location ~ .* { error_page 404 /; } }
啓動服務:
# nginx -t the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful # nginx
Host VS2 做相同的配置
測試:
輪流關閉HostRS1和HostRS2,模擬RS主機宕機情形,可以發現用戶仍然可以正常訪問,說明用戶請求被調度到了正常的RS主機上
# tail /usr/local/nginx/logs/error.log 2016/05/14 14:57:37 [error] 26112#0: check time out with peer: 10.0.0.101:80 2016/05/14 14:57:41 [error] 26112#0: check time out with peer: 10.0.0.101:80 2016/05/14 14:57:46 [error] 26112#0: check time out with peer: 10.0.0.101:80 2016/05/14 14:57:50 [error] 26112#0: check time out with peer: 10.0.0.101:80 2016/05/14 14:57:55 [error] 26112#0: check time out with peer: 10.0.0.101:80 2016/05/14 14:57:59 [error] 26112#0: check time out with peer: 10.0.0.101:80
同時關閉RS1 和 RS2 會提示用戶服務維護中