centos7 Nginx+keepalived高可用+反向代理

Posted 惨惨惨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7 Nginx+keepalived高可用+反向代理相关的知识,希望对你有一定的参考价值。

centos7中没有自带的nginx安装包所有我在这里给大家一个yum源。

1、安装Nginx的yum源

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

本章主要就是Nginx,其他应用会在后续的梳理中,总结给大家。

(注意看服务的IP,作者这里服务IP有点乱,实在抱歉。)

实验环境

(Centos7)

Nginx1 + keepalived : 192.168.13.106

Nginx2 + keepalived : 192.168.13.108

client:192.168.13.109

Web1 : 192.168.13.110:80

Web2 : 192.168.13.111:80

关闭selinux

关闭防火墙

1安装Nginx服务

 yum install -y Nginx
2编辑网页内容

echo "Nginx1:192.168.13.110" > /usr/share/nginx/html/index.html

echo "Nginx2:192.168.13.111" > /usr/share/nginx/html/index.html

3启动web服务

systemctl start nginx

4编辑自身hosts文件

 cat /etc/hosts(四台相同)

192.168.13.110    web1

192.168.13.111    web2

Nginx配置信息

网站文件存放默认目录

/usr/share/nginx/html/

网站默认站点配置

/etc/nginx/conf.d/default.conf

自定义Nginx站点配置文件存放目录

/etc/nginx/conf.d/

Nginx全局配置

/etc/nginx/nginx.conf

在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。

配置Nginx反向代理

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

vim /etc/nginx/nginx.conf

user nginx

.......省略

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

#    include /etc/nginx/conf.d/*.conf;             ######注释掉这个

upstream test {                                                ###########开始
        server 192.168.1.110;
        server 192.168.1.111;
}
        server {
                listen 80;
                server_name localhost;
        location / {
                proxy_pass http://test;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Cookie $http_cookie;
                }
        }                                                              #########结束(注意格式)
}
systemctl restart nginx

在client上测试:

Firefox://192.168.1.106

可以发现110和111轮询状态。

配置keepalived

yum install -y keepalived

cp /etc/keepalived/keepalived.conf keepalived.conf.back

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
  smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.0.100.19
}
vrrp_instance VI_1 {
    state MASTER        BACKUP
    nopreempt
    interface ens33
    virtual_router_id 51
    priority 100                50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

 virtual_ipaddress {
        192.168.13.158  dev     ens33
    }
}
测试:

关闭keepalived

systemctl stop keepalived.service

对端的keepalived就会出现漂移IP

开启keepalived回归原来状态,不抢占MASTER。


以上是关于centos7 Nginx+keepalived高可用+反向代理的主要内容,如果未能解决你的问题,请参考以下文章

Centos7+nginx+keepalived高可用及双主模式

实践:在CentOS7.3部署 keepalived 高可用nginx(负载均衡)

centos7案例实战——nginx+keepalived高可用服务器集群搭建

nginx+keepalived实现高可用负载均衡

Centos7 安装keepalived实现高可用

keepalived+nginx实现高可用