nginx负载均衡配置

1.nginx负载均衡配置

bash 复制代码
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和访问比率成正比,数字越大,分配得到的流量越高

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

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

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

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

      bash 复制代码
      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状态不被认为是失败的尝试
  • 配置实操

    bash 复制代码
    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;
        }
    }
相关推荐
计算机安禾10 小时前
【Linux从入门到精通】第48篇:Linux集群与负载均衡——LVS与Keepalived高可用
linux·负载均衡·lvs
2601_9498179210 小时前
nginx 代理 redis
运维·redis·nginx
java1234_小锋10 小时前
解释一下NGINX的反向代理和正向代理的区别?
运维·nginx
云飞云共享云桌面20 小时前
东莞智能装备工厂数字化实践—研发部门10名SolidWorks设计共享一台云主机流畅设计
服务器·自动化·汽车·负载均衡·制造
techdashen1 天前
Cloudflare 为何抛弃 NGINX,用 Rust 自研了一个代理
运维·nginx·rust
人生匆匆1 天前
通过nginx解决跨域问题
运维·nginx
子木HAPPY阳VIP1 天前
信创UOS,Docker 完整操作部署(Dockerfile部署方式)&排错整合
linux·运维·redis·nginx·docker·容器·tomcat
吹个口哨写代码1 天前
小程序图片不显示,直接访问显示,头部配置问题
javascript·css·nginx
phltxy1 天前
微服务多机部署与负载均衡实战:从手写轮询到 Spring Cloud LoadBalancer 落地
spring cloud·微服务·负载均衡
m0_631653311 天前
阿里云单机双网终极部署与运维白皮书:Nginx + PM2 + Prisma 踩坑实战
运维·nginx·阿里云·部署