您好,登录后才能下订单哦!
下文给大家带来负载均衡Nginx配置流程及步骤介绍,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用亿速云在行业内累计的经验来做一个解答。
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 / {
proxy_pass http://dynamic; #负载均衡 代理
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_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后测试
用域名测试
修改本地hosts文件C:\Windows\System32\drivers\etc\hosts文件加入
192.168.1.150 www.bbs.com
看了以上关于负载均衡Nginx配置流程及步骤介绍,如果大家还有什么地方需要了解的可以在亿速云行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,亿速云技术工程师在行业内拥有十几年的经验了。亿速云官网链接www.yisu.com
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。