环境:
rhel7 四台
IP:192.168.182.200 Nginx主服务器
192.168.182.201 Nginx从服务器
192.168.182.202 Tomcat服务器
192.168.182.203 Tomcat服务器
VIP:192.168.182.130
无防火墙无SELINUX
相应软件安装完毕
1.配置Nginx
http{
#设置默认类型为二进制流
default_type application/octet-stream;
server_names_hash_bucket_size 128;
#指定来自客户端请求头的headerbuffer大小,设置为32KB
client_header_buffer_size 32k;
#指定客户端请求中较大的消息头的缓存最大数量和大小,这里是4个32KB
large_client_header_buffers 4 32k;
#上传文件大小
client_max_body_size 356m;
#nginx的HttpLog模块指定,指定nginx日志的输出格式,输出格式为access
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access日志存在未知
access_log /usr/local/nginx/logs/access.log access;
#开启高效模式文件传输模式,将tcp_nopush和tcp_nodelay两个指另设置为on,用于防止网络阻塞。
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#设置客户端连接保持活动的超时时间
keepalive_timeout 65;
server_tokens off;
#客户端请求主体读取缓存
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_timeout 300;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;
#开启gzip
gzip on;
#允许压缩的最小字节数
gzip_min_length 1k;
#4个单位为16k的内存作为压缩结果流缓存
gzip_buffers 4 16k;
#设置识别HTTP协议版本,默认是1.1
gzip_http_version 1.1;
#gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU
gzip_comp_level 2;
#压缩的类型
gzip_types text/plain application/x-javascript text/css application/xml;
#让前端的缓存服务器混村经过的gzip压缩的页面
gzip_vary on;
upstream mycluster{
server 192.168.22.229:8080 weight=1;
server 192.168.22.230:8080 weight=1;
}
server{
listen 8088;
server_name 192.168.22.227;
charset utf-8; #设置编码为utf-8
#root html;
#location / {
# root html;
# index index.html index.htm;
#}
#location ~ .*\.(jsp|do|action)$
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://mycluster;
# 真实的客户端IP
proxy_set_header X-Real-IP $remote_addr;
# 请求头中Host信息
proxy_set_header Host $host;
# 代理路由信息,此处取IP有安全隐患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 真实的用户访问协议
proxy_set_header X-Forwarded-Proto $scheme;
}
#静态文件交给nginx处理
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root /usr/local/webapps;
expires 30d;
}
#静态文件交给nginx处理
location ~ .*\.(js|css)?$
{
root /usr/local/webapps;
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
另一个将主机IP改为192.168.182.201
2.配置Keepalived配置文件
vrrp_script check_nginx {
script "</dev/tcp/127.0.0.1/8088"
interval 3
weight -2
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.230/32 dev ens33 label ens33:0
}
track_script {
check_nginx
}
}
201keepalived配置:
vrrp_script check_nginx {
#script "/opt/chknginx.sh"
script "</dev/tcp/127.0.0.1/8088"
interval 3
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.230/32 dev ens33 label ens33:0
}
track_script {
chk_nginx
}
}
3.202、203主页配置
202
/var/lib/tomcat/webapps/drp/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Nginx动静分离+Keepalived高可用集群</title>
</head>
<body>
<h1>您正在访问:192.168.182.202</h1>
<img src="/drp/img/girl.jpg" alt="女孩" />
</body>
</html>
203
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Nginx动静分离+Keepalived高可用集群</title>
</head>
<body>
<h1>您正在访问:192.168.182.203</h1>
<img src="/drp/img/girl.jpg" alt="女孩" />
</body>
</html>
4.将图片上传到Nginx服务器并修改Nginx配置文件
#200的 /var/www/html下:
[root@rhel7 html]# tree
.
└── drp
└── img
└── girl.jpg
nginx.conf
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root /var/www/html; #这里修改
expires 30d;
}
#静态文件交给nginx处理
location ~ .*\.(js|css)?$
{
root /var/www/html;
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
201修改同200,访问网址,已经可以负载均衡
5.测试Keepalived主备切换
查看Keepalived主节点VIP
·关闭Keepalived进程
主节点(200):
从节点(201):
可见当主节点宕机或无法正常提供Keepalived服务时,VIP会自动由从节点继承,实现服务器层的高可用。
·停止Nginx服务
将主节点服务恢复后,停止Nginx服务,VIP又转移到了从节点上:
使用VIP访问:
刷新: