时间:2020-02-20来源:系统城作者:电脑系统城
192.168.1.150 VOIP 虚拟IP
192.168.1.151 CNSHANK01 负载均衡1
192.168.1.152 CNSHANK02 负载均衡2
系统:Centos7.4
一、nginx配置(两台nginx配置一模一样,主页设置不同):
include mime.types;
default_type application/octet-stream;
limit_conn_zone $binary_remote_addr zone=addr:10m; #防IP***
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types; #协助部分浏览器(如firefox)识别网页文件的类型
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
client_body_buffer_size 1k; #指定客户端请求主体缓冲区大小
client_header_buffer_size 1k; #指定来自客户端请求头的headerbuffer大小
large_client_header_buffers 2 1k; #客户端请求中较大的消息头指定的缓存最大数量和大小
server_tokens off; #禁止在错误页面上显示nginx版本号
client_body_timeout 20; #设置客户端请求主体读取超时时间
client_header_timeout 20; #设置客户端请求头读取超时时间
gzip on;
upstream dynamic { #负载服务器名dynamic
ip hash; #采用ip_hash方式
server 192.168.1.154:80;
server 192.168.1.155:80;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name blog.dollarphp.com;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
limit_conn addr 100; #指定每个ip最多只允许建立100个连接
limit_rate 500k; #每个ip最大带宽是500k
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
index index.html index.htm;
autoindex on;
autoindex_localtime on;
}
location ~* ^.+.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
root html;
}
location ~* ^.+.(?![js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma])$ {
proxy_pass http://dynamic;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
设置配置主页测试文件
cd /usr/share/nginx/html
cp ./index.html ./index.html.bak
echo “CNSHANK01-192.168.1.151”>./index.html #在主机192.1681.151上
echo “CNSHANK02-192.168.1.152”>./index.html #在主机192.1681.152上
二、keepalived配置:
I、主服务器配置(192.168.1.151):
配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 { # 实例,一个vip一个实例
state MASTER #标识为主服务
interface ens33
virtual_router_id 51 #与从机保持一致即可
priority 100 #权重 ,高于从机
advert_int 1
authentication { # 认证授权,设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress { #虚拟ip地址
192.168.1.150
}
track_script { #nginx监控服务
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
添加服务检查脚本(check.sh):
vim /etc/keepalived/check.sh
#!/bin/bash
if [ "pgrep nginx | wc -l
" == "0" ] ; then
exit 1
else
exit 0
fi
date '+%F %T'
:切换到主机模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:切换到备机模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:宕机" >> /var/log/keepalived/keepalived.log给脚本添加可执行权限:
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh
创建日志目录:
mkdir /var/log/keepalived
II、备服务器配置(192.168.1.152):
安装:
yum -y install keepalived
配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 {
state BACKUP #BACKUP模式
interface ens33
virtual_router_id 51 #与主机一致
priority 99 #比主机小
advert_int 1
authentication { #与主机一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.150
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
pgrep nginx | wc -l
" == "0" ] ; thendate '+%F %T'
:切换到主机模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:切换到备机模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:宕机" >> /var/log/keepalived/keepalived.logII、停止主服务器:192.168.1.151后测试
查看日志:
tail /var/log/keepalived/keepalived.log
主从机日志都显示切到备机模式
测试完成
2024-08-21
win7系统不兼容冒险岛怎么解决教程2024-07-12
联想拯救者如何开启野兽模式教程2024-07-11
ThinkPad选件小红点多功能蓝牙键盘(4X30K12182)1.UltraISO安装系统U盘这一步应该不用过多讲述,无论是安装windows还是ubuntu系统,制作系统U盘都是必需的。博主使用的是UltralSO安装的系统U盘,安装之前肯定是需要去下载Ubuntu...
2024-07-11
谷歌浏览器自动刷新网页可以通过以下步骤实现:在扩展商店安装 “自动刷新” 扩展;启用扩展;配置刷新间隔和范围;点击扩展图标手动刷新网页。...
2024-07-10