負載均衡(Load Balance):將用戶的訪問分攤到多個服務器上,負載均衡的前提就是要有多臺服務器才能實現,也就是兩臺以上即可。
負載均衡也是反向代理所能實現的一個重要功能,區分於正向代理:
反向代理:以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端。
正向代理:一個位於客戶端和原始服務器(originserver)之間的服務器,爲了從原始服務器取得內容,客戶端向代理髮送一個請求並指定目標(原始服務器),然後代理向原始服務器轉交請求並將獲得的內容返回給客戶端。
nginx的負載均衡主要通過 upstream(ngx_http_upstream_module)和http_proxy(ngx_http_proxy_module)這兩個模塊完成。
1. upstream(ngx_http_upstream_module)
定義算法,設定負載均衡的服務器列表。
官方文檔:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
用法:
upstream backend //upstream是定義在server{ }之外的,不能定義在server{ }內部。 { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; }
Nginx的負載均衡模塊目前支持4種調度算法,下面進行分別介紹,其中後兩項屬於第三方調度算法。
輪詢(默認):每個請求按時間順序逐一分配到不同的後端服務器,如果後端某臺服務器宕機,故障系統被自動剔除,使用戶訪問不受影響。Weight 指定輪詢權值,Weight值越大,分配到的訪問機率越高,主要用於後端每個服務器性能不均的情況下。
ip_hash:每個請求按訪問IP的hash結果分配,這樣來自同一個IP的訪客固定訪問一個後端服務器,有效解決了動態網頁存在的session共享問題。
fair:這是比上面兩個更加智能的負載均衡算法。此種算法可以依據頁面大小和加載時間長短智能地進行負載均衡,也就是根據後端服務器的響應時間來分配請求,響應時間短的優先分配。Nginx本身是不支持fair的,如果需要使用這種調度算法,必須下載Nginx的upstream_fair模塊。
url_hash:此方法按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,可以進一步提高後端緩存服務器的效率。Nginx本身是不支持url_hash的,如果需要使用這種調度算法,必須安裝Nginx 的hash軟件包。
2. http_proxy(ngx_http_proxy_module)
官方文檔:http://nginx.org/en/docs/http/ngx_http_proxy_module.html
用法:
location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
此模塊主要是引用server{}外的upstream定義。
下面由實際情況來初步配置nginx的負載均衡:
LB機: 192.168.73.160
lamp機: 192.168.73.155
lnmp機: 192.168.73.156
注:爲了學習才分別搭建apache和nginx的web服務器,正常生產一般只配一種。
LB機安裝nginx
# yum install -y pcre-devel pcre openssl-devel 安裝必須的包 # tar zxvf nginx-1.6.3.tar.gz # useradd -M -s /sbin/nologin nginx # ./configure --prefix=/application/nginx --user=nginx--group=nginx --with-http_ssl_module --with-http_stub_status_module # make&&make install
編輯nginx配置文件:
worker_processes 1; error_log logs/error.log error; 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"'; upstream bbs_server_pools { #ip_hash; server 192.168.73.155:80; server 192.168.73.156:80; } server { listen 80; location / { proxy_pass http://bbs_server_pools; } } }
驗證:
LB主機配置完成,reload
# /etc/init.d/nginx reload nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx/conf/nginx.conf test is successful 重新載入 nginx: [確定]
在lamp主機編輯主頁文件:
# vim /var/html/www/index.php <?php echo "apache web "; ?>
在lnmp主機編輯主頁文件:
# vim /application/nginx/html/www/index.php <?php echo "nginx web "; ?>
使用其它同網段的主機訪問:
[root@mysql ~]# for n in `seq 10`;do curl 192.168.73.160;sleep 1;done nginx web apache web nginx web apache web nginx web apache web nginx web apache web nginx web apache web
簡單的輪詢負載均衡配置成功。
注:以上配置即可實現負載均衡功能,但請求bbs網站,返回的卻是www,原因是客戶請求BBS的header沒有被LB負載均衡器傳給web服務器,需要server標籤添加參數:
vim/application/nginx/conf/nginx.conf
proxy_set_header Host $host;
此時查看web服務器日誌,發現訪問地址顯示的是LB負載均衡的地址,我們需要的是用戶訪問源地址,需要在LB機nginx的server標籤中添加:
vim/application/nginx/conf/nginx.conf
proxy_set_header X-Forwarded-For $remote_addr;
同時web服務器的日誌格式也需要修改:
nginx服務器:vim /application/nginx/conf/nginx.conf
log_format main '$remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
apache服務器:vim /application/apache2/conf/httpd.conf
LogFormat "%h %l %u %t\"%r\" %>s %b \"%{X-Forwarded-For}i\""common
重啓nginx和apache即可