nginx负载均衡配置

1.nginx负载均衡配置

upstream lbs {
    server 192.168.1.12:8080;
    server 192.168.1.12:8081;
}

server {
    listen       80;
    server_name  localhost a.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    location /api/ {
        proxy_pass http://lbs;
        proxy_redirect default;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

2.nginx常见的负载均衡策略

  • 节点轮询(默认)

    • 简介:每个请求按顺序分配到不同的后端服务器
    • 场景:会造成可靠性低和负载分配不均衡,适合静态文件服务器
  • weight权重配置

    • 简介:weight和访问比率成正比,数字越大,分配得到的流量越高

    • 场景:服务器性能差异大的情况使用

      upstream lbs {
          server 192.168.1.12:8080 weight=1;
          server 192.168.1.12:8081 weight=5;
      }
      
  • ip_hash(固定分发)

    • 简介:根据请求按访问IP的hash结果分配,这样每个用户就可以固定访问一个后端服务器

    • 场景:服务器业务分区、业务缓存、session需要单点的情况

      upstream lbs {
          ip_hash;
          server 192.168.1.12:8080;
          server 192.168.1.12:8081;
      }
      
  • upstream还可以为每个节点设置状态值

    • down:当前的server暂时不参与负载;server 192.168.1.12:8080 down;
    • backup:其它所有的非backup机器down的时候,会请求backup机器,这台机器压力会最轻,配置也会相对低;server 192.168.1.12:8080 backup;

3.nginx后端节点可用性探测和配置

  • 如果某个应用挂了,请求不应该继续分发过去

    • max_fails:允许请求失败的次数,默认为1,当超过最大次数时就不会请求

    • fail_timeout:max_fails次失败后暂停的时间,默认为10s

    • 参数解释

      • max_fails=N 设定nginx与后端节点通信的尝试失败的次数
      • 在fail_timeout参数定义的时间内,如果失败的次数达到此值,nginx这个节点不可用
      • 在下一个fail_timeout时间段到来前,服务器不会再被尝试
      • 失败的尝试次数默认是1,如果设置为0就会停止统计尝试次数,认为服务器是一直可用的,一般不会如此配置
    • 具体什么是nginx认为的失败呢

      • 可以通过指令proxy_next_upstream来配置什么是失败的尝试
      • 注意默认配置时,http_404状态不被认为是失败的尝试
  • 配置实操

    upstream lbs {
        server 192.168.1.12:8080 max_fails=2 fail_timeout=60s;
        server 192.168.1.12:8081 max_fails=2 fail_timeout=60s;
    }
    
    server {
        listen       80;
        server_name  localhost a.com;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        location /api/ {
            proxy_pass http://lbs;
            proxy_redirect default;
            proxy_next_upstream error timeout http_500 http_503 http_404;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水宝的滚动歌词

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

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

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

打赏作者

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

抵扣说明:

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

余额充值