LB之Nginx

2024-03-14 08:18
文章标签 nginx lb

本文主要是介绍LB之Nginx,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!




Nginx实现七层的负载均衡

调度到不同组后端服务器
1. 动静分离
2. 网站进行分区
=================================================================================

拓扑结构

         [vip: 20.20.20.20]

       [LB1 Nginx]   [LB2 Nginx]
       192.168.1.2   192.168.1.3

  [index]   [milis]  [videos]  [images]       [news]
  1.11    1.21   1.31    1.41    1.51
  1.12    1.22   1.32    1.42    1.52
  1.13    1.23   1.33    1.43    1.53
  ...     ...    ...     ...     ...
  /web             /web/milis    /web/videos    /web/images   /web/news
  index.html      index.html   index.html                           index.html


一、实施过程
方案一 根据站点分区进行调度
http {
    upstream index {
        server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
    upstream milis {
        server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
     upstream videos {
        server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
     upstream images {
        server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
      upstream news {
        server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
     server {
           location / {
        proxy_pass
http://index;
        }
        
        location  /news {
        proxy_pass
http://news;
        }
        
        location /milis {
        proxy_pass
http://milis;
        }
        
        location ~* \.(wmv|mp4|rmvb)$ {
        proxy_pass
http://videos;
        }
        
        location ~* \.(png|gif|jpg)$ {
        proxy_pass
http://images;
        }
}
 

方案二 根据动静分离进行调度
http {
     upstream htmlservers {
        server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.2:80 weight=2 max_fails=2 fail_timeout=2;
         }
        
  upstream phpservers {
        server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
         }
        
      server {
        location ~* \.html$ {
        proxy_pass
http://htmlservers;
        }
        
        location ~* \.php$ {
        proxy_pass
http://phpservers;
        }
      }
 }


二、Keepalived实现调度器HA
注:主/备调度器均能够实现正常调度
1. 主/备调度器安装软件
[root@master ~]# yum -y install keepalived
[root@backup ~]# yum -y install keepalived

2. Keepalived
BACKUP1
[root@uplook ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id director1   //辅助改为director2
}

vrrp_instance VI_1 {
    state BACKUP
    nopreempt       
    interface eth0    //心跳接口,尽量单独连接心跳
    virtual_router_id 80  //整个集群的调度器一致
    priority 100     //辅助改为50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        20.20.20.20
    }
}

BACKUP2


3. 启动KeepAlived(主备均启动)
[root@uplook ~]# chkconfig keepalived on
[root@uplook ~]# service keepalived start
[root@uplook ~]# ip addr

到此:
可以解决心跳故障keepalived
不能解决Nginx服务故障

4. 扩展对调度器Nginx健康检查(可选)
思路:
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived
a. script
[root@master ~]# cat /etc/keepalived/check_nginx_status.sh
#!/bin/bash            
/usr/bin/curl -I
http://localhost &>/dev/null 
if [ $? -ne 0 ];then          
 /etc/init.d/keepalived stop      
fi                
[root@master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh

b. keepalived使用script
! Configuration File for keepalived

global_defs {
   router_id director1
}

vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx_status.sh"
   interval 5
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    nopreempt
    virtual_router_id 90
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass uplook
    }
   
    virtual_ipaddress {
        192.168.1.80
    }

    track_script {
        check_nginx
    }
}

注:必须先启动nginx,再启动keepalived





这篇关于LB之Nginx的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/807775

相关文章

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

Nginx中配置HTTP/2协议的详细指南

《Nginx中配置HTTP/2协议的详细指南》HTTP/2是HTTP协议的下一代版本,旨在提高性能、减少延迟并优化现代网络环境中的通信效率,本文将为大家介绍Nginx配置HTTP/2协议想详细步骤,需... 目录一、HTTP/2 协议概述1.HTTP/22. HTTP/2 的核心特性3. HTTP/2 的优

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Nginx指令add_header和proxy_set_header的区别及说明

《Nginx指令add_header和proxy_set_header的区别及说明》:本文主要介绍Nginx指令add_header和proxy_set_header的区别及说明,具有很好的参考价... 目录Nginx指令add_header和proxy_set_header区别如何理解反向代理?proxy

Nginx配置location+rewrite实现隐性域名配置

《Nginx配置location+rewrite实现隐性域名配置》本文主要介绍了Nginx配置location+rewrite实现隐性域名配置,包括基于根目录、条件和反向代理+rewrite配置的隐性... 目录1、配置基于根目录的隐性域名(就是nginx反向代理)2、配置基于条件的隐性域名2.1、基于条件

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4

Nginx如何进行流量按比例转发

《Nginx如何进行流量按比例转发》Nginx可以借助split_clients指令或通过weight参数以及Lua脚本实现流量按比例转发,下面小编就为大家介绍一下两种方式具体的操作步骤吧... 目录方式一:借助split_clients指令1. 配置split_clients2. 配置后端服务器组3. 配