標籤:host檔案 英文名稱 工作任務 伺服器 輸送量 nginx
標註:該文章屬於螞蟻課堂原創內容,其他網站轉載必須標註來源與螞蟻課堂。www.itmayiedu.com
1.1 什麼是負載平衡
負載平衡 建立在現有網路結構之上,它提供了一種廉價有效透明的方法擴充網路裝置和伺服器的頻寬、增加輸送量、加強網路資料處理能力、提高網路的靈活性和可用性。
負載平衡,英文名稱為Load Balance,其意思就是分攤到多個操作單元上進行執行,例如Web伺服器、FTP伺服器、企業關鍵應用伺服器和其它關鍵任務伺服器等,從而共同完成工作任務。
650) this.width=650;" src="http://www.itmayiedu.com/images/upload/ueditor/image/20170104/1483519667003048949.png" style="margin:0px auto;padding:0px;border:0px;vertical-align:baseline;background:transparent;height:auto;" alt="1483519667003048949.png" />
1.2 需求
nginx作為負載平衡伺服器,使用者請求先到達nginx,再由nginx根據負載配置將請求轉寄至 tomcat伺服器。
nginx負載平衡伺服器:127.0.0.1
tomcat1伺服器:127.0.0.1:8080
tomcat2伺服器:127.0.0.1:8082
真實環境中nginx、tomcat伺服器是分開的。
1.3 在host檔案中添加本地區名
127.0.0.1 www.itmayiedu.com
1.4 配置nginx的負載平衡
650) this.width=650;" src="http://www.itmayiedu.com/images/upload/ueditor/image/20170104/1483519667050046703.png" style="margin:0px auto;padding:0px;border:0px;vertical-align:baseline;background:transparent;height:auto;" alt="1483519667050046703.png" />
1.5 配置負載平衡的權重
650) this.width=650;" src="http://www.itmayiedu.com/images/upload/ueditor/image/20170104/1483519667205089000.png" style="margin:0px auto;padding:0px;border:0px;vertical-align:baseline;background:transparent;height:auto;" alt="1483519667205089000.png" />
使用weight配置負載平衡,weight預設為1,權重越高,處理請求越多。
節點說明:
在http節點裡添加:
#定義負載平衡裝置的 Ip及裝置狀態
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用負載的Server節點下添加
proxy_pass http://myServer;
upstream 每個裝置的狀態:
down 表示單前的server暫時不參與負載
weight 預設為1.weight越大,負載的權重就越大。
max_fails :允許請求失敗的次數預設為1.當超過最大次數時,返回proxy_next_upstream 模組定義的錯誤
fail_timeout:max_fails 次失敗後,暫停時間。
backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。 |
1.6 示範效果
650) this.width=650;" src="http://www.itmayiedu.com/images/upload/ueditor/image/20170104/1483519667237005015.png" style="margin:0px auto;padding:0px;border:0px;vertical-align:baseline;background:transparent;height:auto;" alt="1483519667237005015.png" />
650) this.width=650;" src="http://www.itmayiedu.com/images/upload/ueditor/image/20170104/1483519667454054911.png" style="margin:0px auto;padding:0px;border:0px;vertical-align:baseline;background:transparent;height:auto;" alt="1483519667454054911.png" />
地址爛輸入:www.itmayiedu.com 通過nginx伺服器做負載平衡,隨機轉寄到8080伺服器或8082伺服器上。
本文出自 “螞蟻課堂” 部落格,請務必保留此出處http://itmayiedu.blog.51cto.com/6234756/1889008
使用nginx+tomcat負載平衡