下文給大家?guī)鞱ginx是如何實現(xiàn)七層的負載均衡的,希望能夠給大家在實際運用中帶來一定的幫助,負載均衡涉及的東西比較多,理論也不多,網上有很多書籍,今天我們就用創(chuàng)新互聯(lián)在行業(yè)內累計的經驗來做一個解答。
嘉定網站建設公司成都創(chuàng)新互聯(lián)公司,嘉定網站設計制作,有大型網站制作公司豐富經驗。已為嘉定近千家提供企業(yè)網站建設服務。企業(yè)網站搭建\成都外貿網站建設要多少錢,請找那個售后服務好的嘉定做網站的公司定做!
Nginx實現(xiàn)七層的負載均衡
調度到不同組后端云服務器
1. 動靜分離
2. 網站進行分區(qū)
=================================================================================
拓撲結構
[vip: 20.20.20.20]
[LB1 Nginx] [LB2 Nginx]
192.168.1.2 192.168.1.3
[index] [milis] [videos] [p_w_picpaths] [news]
1.11 1.21 1.31 1.41 1.51
1.12 1.22 1.32 1.42 1.52
1.13 1.23 1.33 1.43 1.53
... ... ... ... ...
/web /web/milis /web/videos /web/p_w_picpaths /web/news
index.html index.html index.html index.html
一、實施過程
方案一 根據站點分區(qū)進行調度
http {
upstream index {
server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream milis {
server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream videos {
server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream p_w_picpaths {
server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream news {
server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
}
server {
location / {
proxy_pass http://index;
}
location /news {
proxy_pass http://news;
}
location /milis {
proxy_pass http://milis;
}
location ~* \.(wmv|mp4|rmvb)$ {
proxy_pass http://videos;
}
location ~* \.(png|gif|jpg)$ {
proxy_pass http://p_w_picpaths;
}
}
方案二 根據動靜分離進行調度
http {
upstream htmlservers {
server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.2:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream phpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
}
server {
location ~* \.html$ {
proxy_pass http://htmlservers;
}
location ~* \.php$ {
proxy_pass http://phpservers;
}
}
}
二、Keepalived實現(xiàn)調度器HA
注:主/備調度器均能夠實現(xiàn)正常調度
1. 主/備調度器安裝軟件
[root@master ~]# yum -y install keepalived
[root@backup ~]# yum -y install keepalived
2. Keepalived
BACKUP1
[root@uplook ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_iddirector1 //輔助改為director2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0 //心跳接口,盡量單獨連接心跳
virtual_router_id 80//整個集群的調度器一致
priority 100 //輔助改為50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
20.20.20.20
}
}
BACKUP2
3. 啟動KeepAlived(主備均啟動)
[root@uplook ~]# chkconfig keepalived on
[root@uplook ~]# service keepalived start
[root@uplook ~]# ip addr
4. 擴展對調度器Nginx健康檢查(可選)
思路:
讓Keepalived以一定時間間隔執(zhí)行一個外部腳本,腳本的功能是當Nginx失敗,則關閉本機的Keepalived
a. script
[root@master ~]# cat /etc/keepalived/check_nginx_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi
[root@master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh
b. keepalived使用script
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_scriptcheck_nginx {
script "/etc/keepalived/check_nginx_status.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass uplook
}
virtual_ipaddress {
192.168.1.80
}
track_script {
check_nginx
}
}
注:必須先啟動nginx,再啟動keepalived
調度到同一組后端服務器
網站沒按業(yè)務/版塊拆分,所有后端服務器提供整站代碼。
=================================================================================
拓撲結構
[LB Nginx]
20.20.20.20
192.168.1.2
[httpd] [httpd] [httpd]
192.168.1.3 192.168.1.4 192.168.1.5
實施過程
1. nginx
http {
upstream httpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.100:80 backup; 等3、4、5 掛掉100上線
}
server {
location / {
proxy_pass http://httpservers;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
2.Apache LogFormat 可選
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
3. Nginx LogFormat
=================================================================================
看了以上關于Nginx是如何實現(xiàn)七層的負載均衡的,如果大家還有什么地方需要了解的可以在創(chuàng)新互聯(lián)行業(yè)資訊里查找自己感興趣的或者找我們的專業(yè)技術工程師解答的,創(chuàng)新互聯(lián)技術工程師在行業(yè)內擁有十幾年的經驗了。創(chuàng)新互聯(lián)官網鏈接muchs.cn
分享文章:Nginx是如何實現(xiàn)七層的負載均衡的
鏈接URL:http://muchs.cn/article22/jpedcc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站改版、、營銷型網站建設、云服務器、網站收錄、響應式網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)