1. 程式人生 > >nginx實現動靜分離負載均衡

nginx實現動靜分離負載均衡

found pen 創建 this yum put watermark ucc 訪問ip

拓撲:nginx代理分發器:192.168.2.130
web1===192.168.2.131
web2===192.168.2.132
源碼編譯安裝nginx
1、環境準備:
(1)安裝nginx時必須先安裝相應的編譯工具和相關依賴

[root@Qj01 ~]#yum -y install gcc gcc-c++ autoconf automake
[root@Qj01 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

zlib:nginx提供gzip模塊,需要zlib庫支持
openssl:nginx提供ssl功能

pcre:支持地址重寫rewrite功能
(2)安裝nginx並創建運行用戶

[root@Qj01 ~]# tar zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
[root@Qj01 ~]# cd /usr/local/src/nginx-1.8.0/
[root@Qj01 nginx-1.8.0]# ./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
[root@Qj01 nginx-1.8.0]# cat /proc/cpuinfo | grep processor
processor   : 0
processor   : 1
[root@Qj01 nginx-1.8.0]# cat /proc/cpuinfo | grep processor | wc -l
2
[root@Qj01 nginx-1.8.0]# make -j 2 && make install
[root@Qj01 nginx-1.8.0]# useradd -u 5000 -s /sbin/nologin  nginx

參數:
--with-http_dav_module 啟用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法)默認情況下為關閉,需編譯開啟

--with-http_stub_status_module 啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態)
--with-http_addition_module 啟用ngx_http_addition_module支持(作為一個輸出過濾器,支持不完全緩沖,分部分響應請求)
--with-http_sub_module 啟用ngx_http_sub_module支持(允許用一些其他文本替換nginx響應中的一些文本)
--with-http_flv_module 啟用ngx_http_flv_module支持(提供尋求內存使用基於時間的偏移量文件)
--with-http_mp4_module 啟用對mp4文件支持(提供尋求內存使用基於時間的偏移量文件)
[root@Qj01 nginx-1.8.0]# ./configure --help | grep mp4
--with-http_mp4_module enable ngx_http_mp4_module
(3)啟動nginx設置開機啟動

[root@Qj01 sbin]# /usr/local/nginx/sbin/nginx  
[root@Qj01 sbin]# netstat -anptu | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4816/nginx: master 
[root@Qj01 sbin]# echo "/usr/local/nginx/sbin/nginx &" >> /etc/rc.local 

(4)編輯配置文件

[root@Qj01 nginx]# cd /usr/local/nginx/conf/
[root@Qj01 conf]# cp nginx.conf nginx.conf.bak
改:# user nobody;
為:user nginx nginx;   

改:
43         location / {
 44             root   html;
 45             index  index.html index.htm;          #在location / { 。。。} 中添加以下內容  #定義分發策略
location / {
            root   html;
            index  index.html index.htm;

        if ($request_uri ~* \.html$){
                   proxy_pass http://htmlservers;
           }   
        if ($request_uri ~* \.php$){
                   proxy_pass http://phpservers;
           }   
                   proxy_pass http://picservers;

      }

技術分享圖片![]
把以下內容註釋掉,否則php文件直接在nginx服務器上解析了,不再解析給後端服務器:
技術分享圖片
#定義負載均衡設備的 Ip
在配置文件nginx.conf的最後一行}前,添加以下內容:

  upstream  htmlservers {  
         server 192.168.2.131:80;   
         server 192.168.2.132:80;
 }
 upstream  phpservers{
         server 192.168.2.131:80;   
         server 192.168.2.132:80;
 }
 upstream  picservers {
         server 192.168.2.131:80;   
         server 192.168.2.132:80;
 }

技術分享圖片
(5)檢查配置文件並重新加載nginx

[root@Qj01 conf]# /usr/local/nginx/sbin/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@Qj01 conf]# /usr/local/nginx/sbin/nginx  -s reload

2.配置後端兩臺web服務器

[root@Qj02 html]# yum install httpd  php -y
[root@Qj03 html]# yum install httpd  php -y

生成靜態測試文件:

[root@Qj02 html]#echo 192.168.2.131 > /var/www/html/index.html
[root@Qj03 html]#echo 192.168.2.132 > /var/www/html/index.html

生成動態測試文件:

[root@Qj02 html]#vim  /var/www/html/test.php   #寫如以下內容:
192.168.2.131-php
<?php
phpinfo();
?>
[root@Qj03 html]#vim  /var/www/html/test.php   #寫如以下內容:
192.168.2.132-php
<?php
phpinfo();
?>

生成圖片文件:
上傳如下圖片,到“web1網站/var/www/html/目錄下:

技術分享圖片
技術分享圖片
啟動apache服務器:

[root@Qj02 html]# service httpd restart
[root@Qj03 html]# service httpd restart

3.測試
技術分享圖片
技術分享圖片
技術分享圖片技術分享圖片
技術分享圖片
技術分享圖片

附加:
測試性能:
擴展: 文件打開數過多
[root@Qj02 html]# ab -n 1000 -c 1000 http://192.168.2.130/index.html #運行正常
[root@Qj02 html]# ab -n 2000 -c 2000 http://192.168.2.130/index.html #報錯
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.2.130 (be patient)
socket: Too many open files (24) # 測試時,一次打開的socket文件太多。

#ulimit -a #查看
#ulimit -n
1024
系統默認一個進程最多同時允許打開1024的文件
解決:
#ulimit -n 10240 #報錯的解決方法

Nginx負載的5種策略設置方法:
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
upstream backserver {
server 192.168.2.131;
server 192.168.2.132;
}

2、指定權重
指定輪詢幾率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
upstream backserver {
server 192.168.2.131 weight=1;
server 192.168.2.132 weight=2;
}

3、IP綁定 ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
upstream backserver {
ip_hash;
server 192.168.2.131:80;
server 192.168.2.132:80;
}

4、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream backserver {
server server1;
server server2;
fair;
}

5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器為緩存時比較有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

總結,擴展:
如有tomcat ,apache,squid 配置為如下:
[root@Qj02 html]#vim nginx.conf # 在最後添加以下內容。 定義服務器組
upstream tomcat_servers {
server 192.168.2.2:8080;
server 192.168.2.1:8080;
server 192.168.2.11:8080;
}
upstream apache_servers {
server 192.168.2.5:80;
server 192.168.2.177:80;
server 192.168.2.15:80;
}
upstream squid_servers {
server 192.168.2.26:3128;
server 192.168.2.55:3128;
server 192.168.2.18:3128;
}

nginx實現動靜分離負載均衡