IP地址规划
前端服务器
master 192.168.1.112
slave 192.168.1.113
节点服务器
192.168.1.114
192.168.1.115
1、开启主备服务器的upstream模块 在http模块下添加配置分发节点
upstream aaa.xftz.cn{
server 192.168.1.114;
server 192.168.1.115;
}
upstream bbb.xftz.cn{
server 192.168.1.114;
server 192.168.1.115;
}
server {
listen 80;
server_name aaa.xftz.cn;
location / {
proxy_pass http://aaa.xftz.cn;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include /home/webserver/nginx/conf/proxy.conf;
}
}
server {
listen 80;
server_name bbb.xftz.cn;
location / {
proxy_pass http://bbb.xftz.cn;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include /home/webserver/nginx/conf/proxy.conf;
}
include /home/webserver/nginx/conf/proxy.conf //include指令包含进来一个proxy配置文件
}
proxy.conf配置文件内容
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-RealIP $remote_addr;
proxy_set_header Host $host;
client_body_buffer_size 128k;
client_max_body_size 8m;
proxy_connect_timeout 30s;
proxy_send_timeout 1m;
proxy_read_timeout 1m;
proxy_temp_file_write_size 1024m;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
#proxy_set_header(设定header)
#proxy_hide_header(隐藏header)
#proxy_pass_header(通过header)
X-Forwarded-For X-RealIP 分别设置变量Forwarded-For设置的变量作用为后端服务器可以获取客户的真实访问IP通过哪个代理服务器访问的后端服务器 X-RealIP设置的变量表示后端服务器可以获取到用户的真实IP
#proxy_connect_timeout(代理连接超时)
#proxy_send_timeout(代理发送超时)
#proxy_read_timeout(代理接收超时)
#proxy_temp_file_write_size(设定缓存文件夹大小)
#proxy_buffer_size(代理缓冲大小)
#proxy_buffers (代理缓冲)
#proxy_busy_buffers_size(高负荷下缓冲大小)
#proxy_ignore_client_abort(不允许代理端主动关闭连接)
2、slave服务器113的配置与主一样
3、节点服务器114和115配置一致
server {
listen 80;
server_name aaa.xftz.cn;
access_log /home/httpd/logs/nginx/aaa.xftz.cn-access.log main;
error_log /home/httpd/logs/nginx/aaa.xftz.cn-error.log debug ;
root /home/httpd/aaa.xftz.cn/;
index index.html index.php;
}
server {
listen 80;
server_name bbb.xftz.cn;
access_log /home/httpd/logs/nginx/bbb.xftz.cn-access.log main;
error_log /home/httpd/logs/nginx/bbb.xftz.cn-error.log debug ;
root /home/httpd/bbb.xftz.cn/;
index index.html index.php;
}
在后端节点服务器的nginx配置文件里添加日志格式
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" $proxy_add_x_forwarded_for "$http_x_forwarded_for"‘;
4、添加host文件测试访问
192.168.1.112aaa.xftz.cn
192.168.1.112bbb.xftz.cn
5、查看服务器日志
这里upstream的配置完成。
安装Keepalived
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure --sysconf=/etc
make && make install
修改配置文件/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx { //监控nginx脚本
script "/etc/init.d/nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
track_interface {
eth0
}
virtual_ipaddress {
192.168.1.200
}
}
virtual_server 192.168.1.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.1.114 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.115 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
备服务器的配置文件和主的差别在红色部分。
创建nginx监控文件
vim /etc/init.d/nginx.sh
#!/bin/bash
netstat -anpt | grep nginx
if [ $? != 0 ]
then
/home/webserver/nginx/sbin/nginx
sleep 3
netstat -anpt | grep nginx
if [ $? != 0 ]
then
/etc/init.d/keepalived stop
fi
fi
只在主配置即可
测试keepalived 关闭nginx
到此配置完成