Nginx+keepalived配置的實(shí)現(xiàn)步驟
一、需要的服務(wù)
Nginx、keepalived、epel-release -y
二、安裝命令
yum install -y keepalived #安裝nginx以及拓展源 yum install epel-release -y yum install -y nginx
三、配置web服務(wù)
1、web1的nginx配置
[root@nginx1 ~]# vim /etc/nginx/conf.d/web.conf
server{
listen 8080;
root /usr/share/nginx/html;
index test.html;
}
[root@nginx1 ~]# echo "<h1>This is web1</h1>" > /usr/share/nginx/html/test.html2、web2的nginx配置
[root@nginx2 ~]# vim /etc/nginx/conf.d/web.conf
server{
listen 8080;
root /usr/share/nginx/html;
index test.html;
}
[root@nginx2 ~]# echo "<h1>This is web2</h1>" > /usr/share/nginx/html/test.html3、#啟動(dòng)
nginx -t nginx
四、 配置keepalived
以192.168.95.128 作為web1的master,192.168.95.129 作為web2為例
web1配置keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.95.128
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.95.128
}
}
virtual_server 192.168.95.128 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.95.128 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
web2配置keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.95.129
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.95.129
}
}
virtual_server 192.168.95.129 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.95.129 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
五、腳本編寫
mkdir /tools
cd /tools
# keepalived通知腳本
vim master.sh
ip=$(hostname -I | awk '{print $1}')
dt=$(date+'%Y%m%d %H:%M:%S')
echo "$0--${ip}--${dt}" >> /tmp/kp.log
vim backup.sh
ip=$(hostname -I | awk '{print $1}')
dt=$(date+'%Y%m%d %H:%M:%S')
echo "$0--${ip}--${dt}" >> /tmp/kp.log
vim fault.sh
ip=$(ip addr|grep inet| grep 192.168 |awk '{print $2}')
dt=$(date +'%Y%m%d %H:%M:%S')
echo "$0--${ip}--${dt}" >> /tmp/kp.log
vim stop.sh
ip=$(ip addr|grep inet| grep 192.168| awk '{print $2}')
dt=$(date +'%Y%m%d %H:%M:%S')
echo "$0--${ip}--${dt}" >> /tmp/kp.log
## keepalived健康檢查腳本
vim nginx_check.sh
#!/bin/bash
result=`pidof nginx`
if [ ! -z "${result}" ];
then
exit 0
else
exit 1
fi
# 注意腳本授權(quán),重啟keepalived
cd /tools/ && chmod +x *.sh
systemctl restart keepalived.service六、放在瀏覽器測(cè)試

到此這篇關(guān)于Nginx+keepalived配置的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Nginx keepalived配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx負(fù)載均衡下的webshell連接的實(shí)現(xiàn)
在解決shell文件上傳問題、命令執(zhí)行漂移等困難后,可實(shí)現(xiàn)正常的webshell上傳,本文主要介紹了Nginx負(fù)載均衡下的webshell連接的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
nginx鏡像構(gòu)建的知識(shí)點(diǎn)及方法步驟詳解
這篇文章主要為大家介紹了nginx鏡像構(gòu)建的知識(shí)點(diǎn)詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
prometheus監(jiān)控nginx并實(shí)現(xiàn)可視化的操作指南
Nginx是一款高性能的Web服務(wù)器,被廣泛應(yīng)用于各類的網(wǎng)站和應(yīng)用程序中,為了保證Nginx的正常工作,我們需要對(duì)其進(jìn)行監(jiān)控和管理,所以本文給大家介紹了prometheus監(jiān)控nginx并實(shí)現(xiàn)可視化的操作指南,需要的朋友可以參考下2024-05-05
解決Nginx啟動(dòng)報(bào)錯(cuò)Job for nginx.service failed
Nginx啟動(dòng)報(bào)錯(cuò),需檢查配置文件是否正確(執(zhí)行nginx -t),若錯(cuò)誤則修正并重載,若80端口被占用,查進(jìn)程并終止,重啟服務(wù)以解決2025-08-08
Nginx開啟一個(gè)參數(shù)就能讓你的WEB性能提升3倍的方法
這篇文章主要介紹了Nginx開啟一個(gè)參數(shù)就能讓你的WEB性能提升3倍的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-03-03
詳解Nginx中HTTP的keepalive相關(guān)配置
這篇文章主要介紹了Nginx中HTTP的keepalive相關(guān)配置,以及Nginx的Httpd守護(hù)進(jìn)程相關(guān)的keepalive timeout配置,需要的朋友可以參考下2016-01-01

