Nginx+Keepalived
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+Keepalived相关的知识,希望对你有一定的参考价值。
?????????file format ?????? col ?????? sha openssl ?????? and
????????????????????????????????????????????????????????????nginx???HA????????????????????????Nginx?????????loadbalance?????????????????????
nginx???????????????Master+Slave(worker)?????????????????????????????????????????????????????????????????????
???Master????????????????????????Master???????????????????????????????????????????????????????????????
????????????Master??????????????????????????????Slave(worker)???????????????????????????????????????????????????
Slave(worker)????????????????????????????????????Master??????????????????????????????????????????
Keepalived???Linux????????????VRRP ???????????????????????????????????????
??????Keepalived??????????????????????????????????????????????????????????????????????????????IP?????????????????????
???????????????????????????
1???Master????????????Master??????vip???nginx?????????Master???
2???Master????????????backup??????vip??????backup?????????nginx??????
3?????????master???????????????nginx??????????????????vip???????????????backup????????????
4??????????????????????????????????????? ??????????????????ip???????????????1????????????vip?????????
?????????????????????????????????
???????????????2???nginx???2???web???1????????????(???linux????????????)
ip_?????? ???2???nginx 176.16.1.10(eth1??????)???192.168.1.254(eth0??????)
2???web?????????: 192.168.1.100/200(eth0??????)
--------------------------------------------------------------------------------------
????????????????????????Nngix????????????
1???Ningxi+keepalived???????????????(eth0??????, eth1??????)
[[email protected]_master ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:74:C2:B2
TYPE=Ethernet
UUID=8308dd10-28a8-468f-80e4-5a9771fdf9a0
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=202.103.24.68
[[email protected]_master ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
HWADDR=00:0C:29:74:C2:BC
TYPE=Ethernet
UUID=48322db4-8feb-4e5b-a1e0-4e01f64345a3
ONBOOT=yes
NM_CONTROLLED=no
USERCTL=no
?????????????????????????????????????????????????????????
-------------------------------------------------------------------------------------------------------------------
2?????????keepalived.conf
[[email protected]_master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id Nginx_master
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
176.16.1.10/26 dev eth1 //????????????ip
}
virtual_routes {
default via 176.16.1.1 scope global //????????????
}
}
???????????????????????????????????????????????????????????????
[[email protected]_master ~]# services networkd restart //????????????
[[email protected]_master ~]# services keepalived restart //????????????
-------------------------------------------------------------------------------------------------------------------
3????????????/ip????????????????????????????????????????????????eth1???????????????
-------------------------------------------------------------------------------------------------------------------
????????????Nginx???????????????????????????/????????????
???????????????????????????http upstream ??????
[[email protected]_master ~]# cat /usr/local/nginx/conf/nginx.conf
......
......
http {
include mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/extra/wang.conf; //?????????????????????????????????web??????
#log_format main ???$remote_addr - $remote_user [$time_local] "$request" ???
#???$status $body_bytes_sent "$http_referer" ???
#???"$http_user_agent" "$http_x_forwarded_for"???;
......
[[email protected]_master ~]# mkdir -p /etc/nginx/conf.d/extra //????????????????????????
[[email protected]_master ~]# cat /etc/nginx/conf.d/extra/wang.conf //????????????????????????
upstream wang.com { //?????????????????????
server 192.168.1.100 weight=1; //????????????web?????????,???????????????
server 192.168.1.200 weight=1; //????????????web?????????,???????????????
}
server { //??????????????????server
listen 80; //??????????????????80
server_name www.wang.com; //???????????????,?????????localhost
location / {
root html; //?????????????????????
index web.html index.html; //??????????????????
proxy_pass http://wang.com; //??????????????????
proxy_redirect default;
}
}
[[email protected]_master ~]# /usr/local/nginx/sbin/nginx -s reload //????????????????????????
[[email protected]_master ~]# /usr/local/nginx/sbin/nginx //????????????
?????????????????????????????????????????????????????????
-------------------------------------------------------------------------------------------------------------------
??????????????????????????????????????????nginx/keepalived/httpd
#useradd -s /sbin/ -M nginx //???????????????????????????????????????????????????
#yum install gcc gcc-c++ pcre-devel openssl-devel zlib-devel popt-devel kernel-devel //?????????????????????
#./configure --prefix=/usr/local/nginx
--user=nginx --group=nginx --with-http_ssl_module
#make && make install //???????????????
#yum keepaliced httpd //??????keepalived???apache
#echo this is web1 > /var/www/html/a.html //?????????web???????????????????????????
#echo this is web2 > /var/www/html/b.html
#services httpd restart
???????????????????????????http://176.16.1.10??????????????????????????????
-------------------------------------------------------------------------------------------------------------------
?????????
upstream sergrp {
#ip_hash; //??????????????????????????????
#server 192.168.8.5:80 weight=2; //????????????2
server 192.168.8.5:80 down; //???????????????
server 192.168.8.4:80;
server 192.168.8.6:80 backup; //???????????????
server 192.168.8.3:80 max_fails=2 fail_timeout=30; //??????2???????????????,?????????30???
}
以上是关于Nginx+Keepalived的主要内容,如果未能解决你的问题,请参考以下文章