实验场景:
负载均衡:
LB主:192.168.1.1
LB从;192.168.1.2
VIP:192.168.1.3
web服务器:
web1:192.168.1.4
web2:192.168.1.5
一, 安装nginx依赖: 查看是否已经安装
rpm -qa |grep gcc openssl-devel pcre-devel zlib-devel
二, 分别在主从服务器上安装nginx
#tar -zxf nginx-1.1.2.tar.gz
#./configure
#make && make install
修改# vim /usr/local/nginx/conf/nginx.conf
worker_processes 32; (32是你物理机内存的二倍)
在http { } 里添加
upstreammyserver{
server 192.168.1.4; //web1服务器地址
server 192.168.1.5; //web2服务器地址
}
server {
listen 80;
server_name myserver;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_passhttp://myserver;
proxy_next_upstream http_500 http_503error timeout invalid_header;
include /usr/local/nginx/conf/proxy.conf;(这个文件要自己创建)
#root html;
#index index.html index.htm;
}
创建 touch /usr/local/nginx/conf/proxy.conf
[root@localhost conf]# cat proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
三、验证效果
判断配置文件是否正确
#/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx:configuration file /usr/local/nginx/conf/nginx.conf test is successful
更改配置重启nginx
kill -HUP 主进程号或进程号文件路径
或者使用
cd /usr/local/nginx/sbin
./nginx -s reload
分别在两台web服务器上安装nginx
Web1服务器上 echo service1 > /usr/local/nginx/html/index.html
Web2服务器上echo web2 > /usr/local/nginx/html/index.html
四 、问题总结:
问题 : [error] 33249#0: *46 no live upstreams whileconnecting to upstream, client: 10.16.21.118, server: myserver, request:"GET /favicon.ico HTTP/1.1", upstream: "http://myserver/favicon.ico",host: "10.16.21.20", referrer: "http://10.16.21.20/"
解决方法
1. 查看nginx错误日志,检查nginx.conf 配置文件中配置是否正确
2. Iptables –L关闭防火墙 setenforce 0
分别在主从服务器上安装keepalive
//本文参照http://www.open-open.com/lib/view/open1453698551808.html
#tar -zxvfkeepalived-1.2.7.tar.gz
# yum -y install kernel-headers kernel-devel
#./configure --prefix=/usr/local/keepalived --sysconfdir=/etc/
--with-kernel-dir=/usr/src/kernels/2.6.32-573.12.1.el6.x86_64
#make && make install
#ln -s /usr/local/sbin/keepalived /sbin/
#chkconfig keepalived on
#vim /etc/keepalived/keepalived.conf (将原来的配置文件备份后修改)
1. global_defs {
2. router_id NodeB
3. }
4. vrrp_instance VI_1 {
5. state Master #设置为主服务器
6. interface eth0 #监测网络接口
7. virtual_router_id 51 #主、备必须一样
8. priority 90 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
9. advert_int 1 #VRRP Multicast广播周期秒数
10. authentication {
11. auth_type PASS #VRRP认证方式,主备必须一致
12. auth_pass 1111 #(密码)
13. }
14.virtual_ipaddress {
15. 192.168.1.3/24 #VRRP HA虚拟地址
16.}
启动keepalived服务
# service keepalived start 或者keepalived -D -f/usr/local/etc/keepalived/keepalived.conf
#ip a
eth0:
link/ether 00:0c:29:a8:ff:bb brd ff:ff:ff:ff:ff:ff
inet 192.168.1.1/24 brd 10.16.21.255 scope global eth0
inet 192.168.1.3/24 scope global secondary eth0 ///vip地址
inet6 fe80::20c:29ff:fea8:ffbb/64 scope link
valid_lft forever preferred_lft forever