一、keepalived 脑裂
备用节点如何知道主节点是否宕机?
备用节点一直在PING挂载在主节点的VIP.主节点的VIP会给备用节点会回复PONG,证明主节点并没有宕机。如果主节点没有回复,则备用节点则会启动自己的VIP。
1.脑裂的故障
1.网线松动,网络故障
2.服务器硬件故障
3.服务器之间开启了防火墙
2.脑裂模拟-------正常人谁模拟啊,除非真的脑裂。
1)开启防火墙
`开启了防火墙,lb01和lb02之间的 keepalived 大家都互相PING不通。所以主节点以及备用节点。都开启了VIP以及VMAC
[root@lb01 ~]
[root@lb01 ~]
inet 10.0.0.3/32 scope global eth0
[root@lb02 ~]
[root@lb02 ~]
inet 10.0.0.3/32 scope global eth0
2)开启防火墙,单独添加端口开启
`因为开启了firewalld防火墙,默认拒绝所有连接,要开启80端口
[root@lb01 ~]# firewall-cmd --add-service=http
success
[root@lb02 ~]# firewall-cmd --add-service=http
success
`因为开启了firewalld防火墙,默认拒绝所有连接,我们用的是HTTPS,还要开启443端口,要开启80端口
[root@lb01 ~]
success
[root@lb02 ~]
success
3)关闭防火墙
[root@lb02 ~]
[root@lb02 ~]
[root@lb01 ~]
[root@lb01 ~]
inet 10.0.0.3/32 scope global eth0
3.脑裂解决的办法
` 因为是大家互PING,所以主节点以及备用节点,都需要编写并执行此脚本。
[root@lb02 ~]# vim check_keepalive.sh
#!/bin/sh
vip=10.0.0.3
lb01_ip=172.16.1.4
while true;do
ssh $lb01_ip 'ip addr | grep 10.0.0.3' &>/dev/null
if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then
echo "ha is split brain.warning."
else
echo "ha is ok"
fi
sleep 3
done
二、高可用keepalived与nginx
Nginx默认监听在所有的IP地址上,VIP会飘到一台节点上,相当于那台nginx多了VIP这么一个网卡,所以可以访问到nginx所在机器
但是.....如果nginx宕机,会导致用户请求失败,但是keepalived没有挂掉不会进行切换,所以需要编写一个脚本检测Nginx的存活状态,如果不存活则kill掉keepalived
1.nginx故障切换脚本
[root@lb01 ~]
nginxpid=$(ps -ef | grep [n]ginx | wc -l)
if [ $nginxpid -eq 0 ];then
systemctl start nginx &>/dev/null
sleep 3
nginxpid=$(ps -ef | grep [n]ginx | wc -l)
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
fi
fi
[root@lb01 ~]
2.使用keepalived配置文件调用nginx切换脚本
1)配置抢占式时
[root@lb02 ~]
global_defs {
router_id lb02
}
vrrp_script check_web {
script "/root/check_web.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 50
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
track_script {
check_web
}
}
2)配置非抢占式时
[root@lb02 ~]
[root@lb01 ~]
global_defs {
router_id lb01
}
vrrp_script check_web {
script "/root/check_web.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
track_script {
check_web
}
}
3.测试
1.将VIP所在机器nginx的配置文件修改错误
2.停止nginx
3.查看VIP是否切换
Nginx常见问题
一、nginx多server优先级
在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如何配置多个相同的server_name,会导致server_name出现优先级访问冲突。
1.准备多个配置文件
[root@web01 conf.d]
server {
listen 80;
server_name localhost test1.com;
location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]
server {
listen 80;
server_name localhost test2.com;
location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]
server {
listen 80;
server_name localhost test3.com;
location / {
root /code/test3;
index index.html;
}
}
2.创建站点文件
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
3.访问测试
1.重启,访问IP
[root@web01 conf.d]
1)用户第一次访问,读取server1.conf配置返回结果
[root@web01 ~]
test1
2)此时将server1.conf修改为server4.conf重启nginx
[root@web01 conf.d]
[root@web01 conf.d]
3)再次访问时,读取server2.conf配置返回结果
[root@web01 conf.d]
test2
2.配置hosts,访问域名
10.0.0.7 test1.com test2.com test3.com
4.多server优先级总结
1.首先选择所有的'字符串完全匹配'的'server_name'。(完全匹配 www.mumusir.com)
2.选择'通配符在前面'的server_name,如 '*.mumusir.com'
3.选择'通配符在后面'的server_name,如 'www.mumusir.*'
4.最后选择使用'正则表达式'匹配的server_name,如:'~^www\.(.*)\.com$'
5.如果全部都没有匹配到,那么将选择在'listen配置项'后加入'[default_server]'的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件
5.多server优先级总结验证
1)配置完全匹配的配置文件
[root@web01 conf.d]
server {
listen 80;
server_name www.test.com;
location / {
root /code/test;
index index.html;
}
}
[root@web01 conf.d]
2)配置通配符在前面的配置文件
[root@web01 conf.d]
server {
listen 80;
server_name *.test.com;
location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]
3)配置通配符在后面的配置文件
[root@web01 conf.d]
server {
listen 80;
server_name www.test.*;
location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]
4)正则表达式的配置文件
[root@web01 conf.d]
server {
listen 80;
server_name ~^www\.(.*)\.com$;
location / {
root /code/test3;
index index.html;
}
}
[root@web01 conf.d]
5)配置default_server的配置文件
[root@web01 conf.d]
server {
listen 80 default_server;
server_name localhost;
location / {
root /code/test4;
index index.html;
}
}
[root@web01 conf.d]
6)放在第一个的配置文件
[root@web01 conf.d]
server {
listen 80;
server_name localhost;
location / {
root /code/test5;
index index.html;
}
}
[root@web01 conf.d]
7)重启nginx测试
10.0.0.7 www.test.com
二、nginx禁止IP访问
当用户通过访问IP或者未知域名访问你得网站的时候,你希望禁止显示任何有效内容,可以给他返回500,目前国内很多机房都要求网站关闭空主机头,防止未备案的域名指向过来造成麻烦
1.禁止IP访问直接返回错误
[root@web01 conf.d]
server {
listen 80 default_server;
server_name localhost;
return 500;
}
2.引流的方式,跳转到其他网站
[root@web01 conf.d]
server {
listen 80 default_server;
server_name localhost;
return 302 http://www.baidu.com;
}
3.返回指定的内容
[root@web01 conf.d]
server {
listen 80 default_server;
server_name localhost;
default_type text/plain;
return 200 "请使用域名访问正规网站!!!";
}
4.跳转到指定文件
[root@web01 ~]
server {
listen 80 default_server;
server_name localhost;
root /code;
rewrite (.*) /1.jpg;
}
三、nginx的include
一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。
假设现在希望快速的关闭一个站点,该怎么办?
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释
Include包含的作用是为了简化主配置文件,便于人类可读。
inlcude /etc/nginx/online/*.conf
/etc/nginx/offline
四、nginx的root与alias
root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias定义的路径
1.root和alias的配置
[root@lb01 conf.d]
server {
listen 80;
server_name image.com;
location /picture {
root /code;
}
}
[root@lb01 conf.d]
server {
listen 80;
server_name image.com;
location /picture {
alias /code;
}
}
2.线上配置
server {
listen 80;
server_name image.com;
location / {
root /code;
}
location ~* ^.*\.(png|jpg|gif)$ {
alias /code/images/;
}
}
五、Nginx调整上传文件大小
在nginx使用上传文件的过程中,通常需要设置文件大小限制,避免出现413 Request Entity Too Large
1.nginx上传文件大小限制配置语法
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location
2.nginx长传文件大小限制配置示例
server {
listen 80;
server_name _;
client_max_body_size 200m;
}