使用nginx配置负载均衡

1.下载nginx

  • 准备环境

    yum install gcc-c++
    yum install -y pcre pcre-devel
    yum install -y openssl openssl-devel
    
  • 准备nginx:
    下载:# wget http://nginx.org/download/nginx-1.8.1.tar.gz
    解压:# tar -zxvf nginx-1.8.1.tar.gz
    进入目录:# cd nginx-1.8.1

  • 配置nginx:# ./configure --prefix=/usr/src/nginx --with-http_stub_status_module --with-http_ssl_module

  • 编译安装:# make && make install

  • 查看nginx版本:
    进入安装目录:# cd /usr/src/nginx/sbin
    查看版本:# ./nginx -v

  • 设置开机启动
    编辑/etc/rc.local:# vim /etc/rc.local
    添加一行:/usr/src/nginx/sbin/nginx,之后,开机会自动启动nginx

  • nginx 命令
    进入nginx目录:# cd /usr/src/nginx/sbin
    启动服务:# ./nginx
    停止服务:# ./nginx -s stop
    重启服务:# ./nginx -s reopen
    重新载入配置文件:# ./nginx -s reload

2.配置负载均衡


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

	upstream minio{
	        server 172.28.8.65:9000;
	        server 172.28.8.61:9000;
	        server 172.28.8.62:9000;
	        server 172.28.8.63:9000;
	}

    server {
        listen 9001;
        server_name 172.28.8.111;
        location / {
                proxy_pass http://minio;
                proxy_set_header Host $http_host;
                client_max_body_size 1000m;
        }
	}

}

3.配置双主热备

! Configuration File for keepalived

global_defs {
   notification_email {
     guchang.pan@hand-china.com
     2660292608@qq.com
   }
   notification_email_from guchang.pan@hand-china.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MYCAT_HA
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 52   #路由器标识,MASTER和BACKUP必须是一致的
    priority 101           #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.28.8.111/24
    }
}

virtual_server 172.28.8.111 80 {
    delay_loop 6
    protocol TCP

    real_server 172.28.8.65 80 {
        notify_down "kill -9 $(cat /var/run/keepalived.pid)" 
        TCP_CHECK { 
            connect_port 80  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘顾昌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值