標籤:protocol 伺服器 虛擬機器主機 server stream
首先複習一下LB Cluster負載平衡叢集
四層:
LVS
Nginx(stream)
Haproxy(mode_tcp)
七層
Http protocol
Nginx(http,upstream)
Haproxy(mode http)
Httpd/ats/perlbal/pound/…
接下來如何?nginx在http的負載平衡
ngx_stream_proxy_module模組能為http服務做調度,其中stream模組中有
專門的server子命令,不同於其他server,其他server是用於定義虛擬機器主機的
而stream模組中的server是用來定義組中的一台伺服器的,server可以重複使用多次,
定義多台伺服器,因此可實現伺服器的負載平衡。
#################################################################
1、實驗環境準備,至少準備三台主機,其中一台做為nginx調度伺服器,裝備兩塊網卡
分別在三台主機上配置nginx,httpd和httpd,並測試可以成功訪問頁面
[[email protected] nginx]# curl http://172.18.10.10:80/test1.html
Test Page 1 on UpStream Server 1 (172.18.10.10)
[[email protected] nginx]# curl http://172.18.10.11:80/test1.html
Test Page 1 on UpStream Server 2 (172.18.10.11)
將172.18.10.10和172.18.10.11做為動態網站(安裝httpd+php,即ap,listen 80)
將172.18.10.10和172.18.10.11做為靜態網站(其中10.11安裝nginx,監聽8080,10.10配置虛擬機器主機,監聽80和8080)
2、實驗目的。實現nginx對靜態內容和動態內容的負載平衡
3、開始配置操作
在172.18.10.10下編輯php頁面
[[email protected] ~]# vim /var/www/html/index.php
<h1>HTTPD listend on 80 Server1</h1>
<?php
phpinfo();
?>
將實驗頁面發至172.18.10.11的分頁檔存放路徑下
[[email protected] ~]# scp /var/www/html/index.php 172.18.10.11:/var/www/html/
修改Server1為Server2
<h1>HTTPD listend on 80 Server2</h1>
<?php
phpinfo();
?>
4、常識使用Google瀏覽器請求兩個地址,看看是否測試頁面能夠正常顯示--------經測試發現能夠正常顯示
5、配置靜態網站的nginx
將準備好的nginx安裝包分別scp到兩台主機上
[[email protected] ~]# scp nginx-1.6.2-1.el6.ngx.x86_64.rpm 172.18.10.10:/root/
6、安裝nginx
[[email protected] ~]# yum install nginx-1.6.2-1.el6.ngx.x86_64.rpm
7、配置靜態網站的虛擬服務
172.18.10.10上:
注釋DocumentRoot路徑
#DocumentRoot "/var/www/html"
添加新的監聽連接埠
#Listen 12.34.56.78:80
Listen 80
Listen 8080
添加虛擬機器主機,分別監聽在80和8080連接埠上
<VirtualHost *:80>
DocumentRoot /var/www/shope
ServerName www.magedu.com
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot /var/www/html
ServerName imgs.magedu.com
</VirtualHost>
儲存退出
建立目錄
[[email protected] ~]# mkdir /var/www/shope
將index.php移至該目錄下
[[email protected] ~]# mv /var/www/html/index.php /var/www/shope/
檢查文法
[[email protected] ~]# httpd -t
重啟httpd服務
[[email protected] ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
在瀏覽器端分別訪問測試80和8080連接埠
結果正常
172.18.10.11上:
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
將虛擬機器主機監聽連接埠改為8080
listen 80———————------》 listen 8080;
更改root路徑
root /usr/share/nginx/html;—————》root /data/html;
建立虛擬機器主機目錄路徑
[[email protected] ~]# mkdir /data/html -pv
mkdir: created directory `/data‘
mkdir: created directory `/data/html‘
將所有test檔案移至/data/html目錄下
[[email protected] ~]# mv /var/www/html/test* /data/html/
啟動nginx服務,並且查看是否連接埠監聽
[[email protected] ~]# nginx
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25
訪問頁面。看看是否能夠正常訪問
8、配置nginx調度端的nginx服務在172.18.200.100上
[[email protected] ~]# vim /etc/nginx/nginx.conf
#使用預設的加權輪詢演算法,進行會發綁定
upstream websrvs {
server 172.18.10.10:80 weight=2 max_fails=2 fail_timeout=2;
server 172.18.10.11:80 weight=3;
}
upstream staticsrvs {
server 172.18.10.10:8080 weight=1;
server 172.18.10.11:8080 weight=1;
}
9、編輯調度的方法
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
index index.php index.html; ####全域定義,先後順序
location / {
proxy_pass http://websrvs; ####動態資源載入路徑定義
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~* \.(jpg|jpeg|png|gif|html)$ {
proxy_pass http://staticsrvs; #####靜態資源載入路徑定義
index index.php;
}
10、從新載入測試
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx -s reload
開啟Google瀏覽器,輸入http://172.18.200.100/重新整理頁面發現會有如下的頁面內容來回切換
HTTPD listend on 80 Server2
HTTPD listend on 80 Server1
請求http://172.18.200.100/index.php,發現也是如下頁面內容來回切換
HTTPD listend on 80 Server2
HTTPD listend on 80 Server1
從其他用戶端使用curl來測試
[[email protected] ~]# for ((i=1;i<=10;i++));do curl http://172.18.200.100/index.php; done
測試結果:實現動態內容的負載平衡
接下來請求靜態檔案:http://172.18.200.100/test1.html,不斷重新整理,發現得到如下內容來回切換
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
從其他用戶端使用curl來測試
[[email protected] ~]# for ((i=1;i<=10;i++));do curl http://172.18.200.100/test1.html; done
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
測試結果:實現動態內容的負責均衡
最終實現動靜分離,而在靜態內容上面我們還可以定義緩衝,提升效率。
本文出自 “12866758” 部落格,請務必保留此出處http://12876758.blog.51cto.com/12866758/1925249
實驗:部署實現nginx在http上的動靜分離和負載平衡