下文給大家?guī)?lái)Nginx是如何實(shí)現(xiàn)七層的負(fù)載均衡的,希望能夠給大家在實(shí)際運(yùn)用中帶來(lái)一定的幫助,負(fù)載均衡涉及的東西比較多,理論也不多,網(wǎng)上有很多書籍,今天我們就用創(chuàng)新互聯(lián)在行業(yè)內(nèi)累計(jì)的經(jīng)驗(yàn)來(lái)做一個(gè)解答。
嘉定網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,嘉定網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為嘉定近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的嘉定做網(wǎng)站的公司定做!
Nginx實(shí)現(xiàn)七層的負(fù)載均衡
調(diào)度到不同組后端云服務(wù)器
1. 動(dòng)靜分離
2. 網(wǎng)站進(jìn)行分區(qū)
=================================================================================
拓?fù)浣Y(jié)構(gòu)
[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
一、實(shí)施過(guò)程
方案一 根據(jù)站點(diǎn)分區(qū)進(jìn)行調(diào)度
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;
}
}
方案二 根據(jù)動(dòng)靜分離進(jìn)行調(diào)度
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實(shí)現(xiàn)調(diào)度器HA
注:主/備調(diào)度器均能夠?qū)崿F(xiàn)正常調(diào)度
1. 主/備調(diào)度器安裝軟件
[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 //心跳接口,盡量單獨(dú)連接心跳
virtual_router_id 80//整個(gè)集群的調(diào)度器一致
priority 100 //輔助改為50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
20.20.20.20
}
}
BACKUP2
3. 啟動(dòng)KeepAlived(主備均啟動(dòng))
[root@uplook ~]# chkconfig keepalived on
[root@uplook ~]# service keepalived start
[root@uplook ~]# ip addr
4. 擴(kuò)展對(duì)調(diào)度器Nginx健康檢查(可選)
思路:
讓Keepalived以一定時(shí)間間隔執(zhí)行一個(gè)外部腳本,腳本的功能是當(dāng)Nginx失敗,則關(guān)閉本機(jī)的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
}
}
注:必須先啟動(dòng)nginx,再啟動(dòng)keepalived
調(diào)度到同一組后端服務(wù)器
網(wǎng)站沒(méi)按業(yè)務(wù)/版塊拆分,所有后端服務(wù)器提供整站代碼。
=================================================================================
拓?fù)浣Y(jié)構(gòu)
[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
實(shí)施過(guò)程
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
=================================================================================
看了以上關(guān)于Nginx是如何實(shí)現(xiàn)七層的負(fù)載均衡的,如果大家還有什么地方需要了解的可以在創(chuàng)新互聯(lián)行業(yè)資訊里查找自己感興趣的或者找我們的專業(yè)技術(shù)工程師解答的,創(chuàng)新互聯(lián)技術(shù)工程師在行業(yè)內(nèi)擁有十幾年的經(jīng)驗(yàn)了。創(chuàng)新互聯(lián)官網(wǎng)鏈接www.cdcxhl.com