怎么樣實現nginx在http的負載均衡-創新互聯

下文給大家帶來怎么樣實現nginx在http的負載均衡,希望能夠給大家在實際運用中帶來一定的幫助,負載均衡涉及的東西比較多,理論也不多,網上有很多書籍,今天我們就用創新互聯在行業內累計的經驗來做一個解答。

創新互聯從2013年創立,是專業互聯網技術服務公司,擁有項目成都網站制作、成都做網站網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元豐澤做網站,已為上家服務,為豐澤各地企業和個人服務,聯系電話:13518219792

首先復習一下LB Cluster負載均衡集群

四層:

LVS

Nginx(stream)

Haproxy(mode_tcp)

七層

Http protocol

Nginx(http,upstream)

Haproxy(mode http)

Httpd/ats/perlbal/pound/…

怎么樣實現nginx在http的負載均衡

接下來如何實現nginx在http的負載均衡

ngx_stream_proxy_module模塊能為http服務做調度,其中stream模塊中有

專門的server子命令,不同于其他server,其他server是用于定義虛擬主機的

而stream模塊中的server是用來定義組中的一臺云服務器的,server可以重復使用多次,

定義多臺服務器,因此可實現服務器的負載均衡。

#################################################################

1、實驗環境準備,至少準備三臺主機,其中一臺做為nginx調度服務器,裝備兩塊網卡

分別在三臺主機上配置nginx,httpd和httpd,并測試可以成功訪問頁面

[root@localhost nginx]# curl http://172.18.10.10:80/test1.html

Test Page 1 on UpStream Server 1 (172.18.10.10)

[root@localhost 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頁面

[root@localhost ~]# vim /var/www/html/index.php

<h3>HTTPD listend on 80 Server1</h3>

<?php

    phpinfo();

?>

將實驗頁面發至172.18.10.11的頁面文件存放路徑下

[root@localhost ~]# scp /var/www/html/index.php 172.18.10.11:/var/www/html/

修改Server1為Server2

<h3>HTTPD listend on 80 Server2</h3>

<?php

    phpinfo();

?>

4、常識使用谷歌瀏覽器請求兩個地址,看看是否測試頁面能夠正常顯示--------經測試發現能夠正常顯示

5、配置靜態站點的nginx

將準備好的nginx安裝包分別scp到兩臺主機上

[root@localhost ~]# scp nginx-1.6.2-1.el6.ngx.x86_64.rpm 172.18.10.10:/root/

6、安裝nginx

[root@localhost ~]# 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>

保存退出

創建目錄

[root@localhost ~]# mkdir /var/www/shope

將index.php移至該目錄下

[root@localhost ~]# mv /var/www/html/index.php /var/www/shope/

檢查語法

[root@localhost ~]# httpd -t

重啟httpd服務

[root@localhost ~]# 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端口

結果正常

怎么樣實現nginx在http的負載均衡

172.18.10.11上:

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf

將虛擬主機監聽端口改為8080

  listen    80———————------》 listen    8080;

更改root路徑

root  /usr/share/nginx/html;—————》root  /data/html;

創建虛擬主機目錄路徑

[root@localhost ~]# mkdir /data/html -pv

mkdir: created directory `/data'

mkdir: created directory `/data/html'

將所有test文件移至/data/html目錄下

[root@localhost ~]# mv /var/www/html/test* /data/html/

啟動nginx服務,并且查看是否端口監聽

[root@localhost ~]# nginx

[root@localhost ~]# 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上

[root@localhost ~]# 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、編輯調度的方法

[root@localhost ~]# 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、從新加載測試

[root@localhost ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost ~]# nginx -s reload

打開谷歌瀏覽器,輸入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來測試

[root@localhost ~]# 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來測試

[root@localhost ~]# 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)

測試結果:實現動態內容的負責均衡

最終實現動靜分離,而在靜態內容上面我們還可以定義緩存,提升效率。

看了以上關于怎么樣實現nginx在http的負載均衡,如果大家還有什么地方需要了解的可以在創新互聯行業資訊里查找自己感興趣的或者找我們的專業技術工程師解答的,創新互聯技術工程師在行業內擁有十幾年的經驗了。創新互聯官網鏈接www.yisu.com

另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網站名稱:怎么樣實現nginx在http的負載均衡-創新互聯
新聞來源:http://m.kartarina.com/article14/cdsdde.html

成都網站建設公司_創新互聯,為您提供網頁設計公司全網營銷推廣關鍵詞優化動態網站定制網站品牌網站制作

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作
主站蜘蛛池模板: 影音先锋中文无码一区| 亚洲人av高清无码| 免费无码一区二区三区蜜桃大| av潮喷大喷水系列无码| 无码人妻精品一区二区| 久久无码中文字幕东京热| 自慰系列无码专区| 国产V亚洲V天堂无码| 国产精品无码aⅴ嫩草| 久久久久久无码Av成人影院 | 国产精品白浆在线观看无码专区| 色视频综合无码一区二区三区| 人禽无码视频在线观看| 久久亚洲av无码精品浪潮| 一本大道在线无码一区| 无码精品人妻一区二区三区人妻斩| 无码A级毛片日韩精品| 亚洲国产av高清无码| 国产在线观看无码免费视频 | 亚洲中文字幕无码久久2017| 亚洲Av无码国产一区二区| 国产AV无码专区亚洲AV男同| 日韩AV无码精品一二三区| 日本精品人妻无码免费大全 | 国产精品无码久久av| 中文字幕无码乱码人妻系列蜜桃| 亚洲AV无码国产丝袜在线观看| 成年无码av片在线| 国产成人无码免费网站| 无码国内精品久久人妻麻豆按摩| 中文字幕无码高清晰| 无码国产精品一区二区免费式芒果| 中文无码vs无码人妻 | 永久免费av无码网站韩国毛片| 手机永久无码国产AV毛片| 无码国产色欲XXXX视频| 亚洲AV综合色区无码二区爱AV| 亚洲AV无码国产精品色| 一本色道久久综合无码人妻| 中文字幕av无码无卡免费| 国外AV无码精品国产精品|