nginx負載平衡安裝及測試

來源:互聯網
上載者:User
nginx負載平衡安裝及測試 1、配置3台機器

A伺服器IP :192.168.157.128 (主)

B伺服器IP :192.168.157.136

C伺服器IP :192.168.157.137

vim /etc/hosts

192.168.157.128 hadoop1
192.168.157.136 hadoop2
192.168.157.137 hadoop3

2、nginx.conf設定

A伺服器nginx.conf設定
開啟nginx.conf,檔案位置在nginx安裝目錄的conf目錄下。

在http段加入以下代碼

upstream a.com {
      server  192.168.157.136:80;
      server  192.168.157.137:80;
}
 
server{
    listen 80;
    server_name hadoop1;
    location / {
        proxy_pass         http://hadoop1;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

儲存重啟 nginx -s reload

分別配置B、C伺服器nginx.conf設定
開啟nginx.confi,在http段加入以下代碼

server{
    listen 80;
    server_name hadoop1;
    index index.html;
}

分別重啟B、C伺服器 nginx -s reload

區分介面:
192.168.157.128   vim  /usr/local/nginx/html/index.html 內容為hadoop1
192.168.157.136   vim  /usr/local/nginx/html/index.html 內容為hadoop2
192.168.157.137   vim  /usr/local/nginx/html/index.html 內容為hadoop3
3、分別重啟A、B、C伺服器

nginx -s reload

訪問http://hadoop1:80  重新整理介面  可以看到介面內容為hadoop2或者hadoop3
結論:
開啟瀏覽器訪問http://hadoop1:80 結果,重新整理會發現所有的請求均分別被主伺服器(192.168.157.128)分配到B伺服器(192.168.157.136)與C伺服器(192.168.157.137)上,實現了負載平衡效果。


4、類比一台伺服器宕機

假如其中一台伺服器宕機會怎樣。
當某台伺服器宕機了,是否會影響訪問呢。

我們先來看看執行個體,根據以上例子,假設C伺服器192.168.157.137這台機子宕機了(由於無法類比宕機,所以我就把C伺服器關機)然後再來訪問看看。

訪問結果:hadoop2

我們發現,雖然C伺服器(192.168.157.137)宕機了,但不影響網站訪問。這樣,就不會擔心在負載平衡模式下因為某台機子宕機而拖累整個網站了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.