天天看點

使用nginx+Apache負載均衡及動靜分離

使用nginx+Apache負載均衡及動靜分離

介紹

   LB負載均衡叢集分兩類: LVS (四層)和 nginx或haproxy (七層)

   用戶端都是通過通路分發器的VIP來通路網站 在七層中的網站頁面有: .php .html .png .jpeg .jsp 等, 有動态頁面有靜态頁面。 需要在應用層基于不同的應用進行分發。

一:實驗拓撲圖:

二:實驗目标

實戰:使用Apache+nginx實作動靜分離的負載均衡叢集

三:實驗環境

主機作用分類

主機名

IP位址

安裝軟體

Nginx,代理伺服器

xuegod63.cn

192.168.1.63

nginx-1.8.0.tar.gz

Apache,靜态頁面處理

xuegod62.cn

192.168.1.62

http

xuegod64.cn

192.168.1.64

Apache,動态頁面處理

Apache,圖檔處理

四:實驗代碼

1、配置分發器xuegod63(代理伺服器)

1)安裝nginx時必須先安裝相應的編譯工具

[root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake

[root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

zlib:  nginx提供gzip子產品,需要zlib庫支援

openssl:nginx提供ssl功能

pcre:支援位址重寫rewrite功能

2)安裝nginx:

[root@xuegod63 ~]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/

[root@xuegod63 ~]# cd /usr/local/src/nginx-1.8.0/

[root@xuegod63 nginx-1.8.0]# ./configure --prefix=/server/nginx-1.8.0 --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

參數解釋:

--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檔案支援(提供尋求記憶體使用基于時間的偏移量檔案)

3)編譯和安裝: 

[root@xuegod63 nginx-1.8.0]#make -j 4

[root@xuegod63 nginx-1.8.0]#make install

4)生成運作nginx的使用者: 

[root@xuegod63 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx

5)啟動nginx:

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx

[root@xuegod63 nginx-1.8.0]# echo '/server/nginx-1.8.0/sbin/nginx & ' >> /etc/rc.local

6)測試: http://192.168.1.63/

7)nginx服務日常操作: :

(1)測試配置檔案是否正确文法:

[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -t

nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok

nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful

(2)重新加載配置檔案

[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -s reload

(3)關閉與開啟nginx

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s stop

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s start #沒有start參數

nginx: invalid option: "-s start"

7)配置nginx成為分發器,實作動靜分離

[root@xuegod63 conf]# cd /server/nginx-1.8.0/conf #配置檔案目錄

[root@xuegod63 conf]# cp nginx.conf nginx.conf.back #備份一下配置檔案

[root@xuegod63 conf]# vim nginx.conf

[root@xuegod63 nginx-1.8.0]# vim /server/nginx-1.8.0/conf/nginx.conf #指定啟動nginx使用者

改:# user nobody;

為:user nginx nginx;

改:

43 location / {

44 root html;

45 index index.html index.htm; #在location / { 。。。} 中添加以下内容 #定義分發政策

    index index.html index.htm;

if ($request_uri ~* \.html$){

           proxy_pass http://htmlservers;  #比對到htm靜态類型通路的都會轉到html服務池中

}

if ($request_uri ~* \.php$){

          proxy_pass http://phpservers;  #比對到php動态類型檔案直接在nginx伺服器上解析了

如圖:

把一下内容注釋掉,比對到php動态類型檔案直接在nginx伺服器上解析了,不再解析給後端伺服器:

8)在配置檔案nginx.conf的最後一行}前,添加以下内容:

upstream htmlservers { #定義靜态檔案負載均衡伺服器組名稱

server 192.168.1.62:80;

server 192.168.1.64:80;

upstream phpservers{ #定義動态檔案負載均衡伺服器組名稱

upstream picservers { #定義圖檔檔案負載均衡伺服器組名稱

#後期工作中,根據工作中的需要,配置成具體業務的IP位址

9)重新加載nginx伺服器配置檔案: 

[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -t

[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -s reload

2、配置後端伺服器: xuegod62  

(1)配置web伺服器: 

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

(2)生成靜态測試檔案: 

root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html

(3)生成動态測試檔案: 

[root@xuegod62 html]#vim /var/www/html/test.php #寫如以下内容:

192.168.1.62-php

<?php

phpinfo();

?>

(4)生成圖檔檔案: 上傳如下圖檔,到“xuegod62網站/var/www/html/目錄下:

(5)啟動apache伺服器: 

[root@xuegod62 html]# service httpd restart

3、配置後端伺服器:xuegod64

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

(2)生成靜态測試檔案:

[root@xuegod64 html]#echo 192.168.1.64 > /var/www/html/index.html

[root@xuegod64 html]#vim /var/www/html/test.php #寫如以下内容:

192.168.1.64-php

(4)生成圖檔檔案: --上傳如下圖檔,到“xuegod64網站/var/www/html/目錄下:

(5)重新開機apache伺服器

[root@xuegod64 html]# service httpd restart

4、測試

(1)測試負載均衡及動靜分離---靜态頁面:

(2)測試動靜分離及負載均衡---動态頁面:

<a href="https://s1.51cto.com/wyfs02/M00/95/53/wKiom1kUD8qDFFS2AADb4Bf8HEA488.png" target="_blank"></a>

<a href="https://s4.51cto.com/wyfs02/M02/95/53/wKiom1kUD8uydNtSAAD92SUyyZo212.png" target="_blank"></a>

(3)測試圖檔負載均衡:

<a href="https://s2.51cto.com/wyfs02/M02/95/52/wKioL1kUD9nAIT7fAAGoEGCmIh0388.png" target="_blank"></a>

<a href="https://s2.51cto.com/wyfs02/M00/95/53/wKiom1kUD9rDPAH2AAG5UMxu3AE840.png" target="_blank"></a>

(4)測試自動剔除壞的節點:

[root@xuegod64 html]# service httpd stop

http://192.168.1.63/pic.jpg

5、測試性能: 

擴充: 檔案打開數過多

[root@xuegod63 html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #運作正常

[root@xuegod63 html]# ab -n 2000 -c 2000 http://192.168.1.62/index.html #報錯

This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)

socket: Too many open files (24) # 測試時,一次打開的socket檔案太多。

[root@xuegod63 ~]# ulimit -n

1024#系統預設一個程序最多同時允許打開1024的檔案

[root@xuegod63 ~]# ulimit -n 10240 #修改預設允許同僚打開10240個檔案

[root@xuegod63 ~]# ab -n 2000 -c 2000 http://192.168.1.62/index.html

Completed 200 requests

Completed 400 requests

.......

Completed 1800 requests

Completed 2000 requests

Finished 2000 requests

Server Software:        Apache/2.2.15

Server Hostname:        192.168.1.62

Server Port:            80

Document Path:          /index.html

Document Length:        13 bytes

Concurrency Level:      2000

Time taken for tests:   1.119 seconds

Complete requests:      2000

Failed requests:        0

Write errors:           0

Total transferred:      560000 bytes

HTML transferred:       26000 bytes

Requests per second:    1787.69 [#/sec] (mean)

Time per request:       1118.765 [ms] (mean)

Time per request:       0.559 [ms] (mean, across all concurrent requests)

Transfer rate:          488.82 [Kbytes/sec] received

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0   56 216.7      1    1062

Processing:     4   71 161.9     24     670

Waiting:        4   70 161.9     24     670

Total:         16  127 271.1     26    1087

Percentage of the requests served within a certain time (ms)

  50%     26

  66%     26

  75%     27

  80%     57

  90%    717

  95%    727

  98%   1085

  99%   1086

 100%   1087 (longest request)

本文轉自 于學康 51CTO部落格,原文連結:http://blog.51cto.com/blxueyuan/1924493,如需轉載請自行聯系原作者