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;
        }
    }
相关推荐
Avan_菜菜10 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
ping某14 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
難釋懷16 天前
Nginx反向代理中的容错机制
运维·nginx
bloglin9999916 天前
Nginx高危漏洞CVE-2021-23017及配置样例
运维·nginx
进阶的小名16 天前
Spring Boot SSE + Nginx 配置:解决 EventSource 不实时返回、连接超时、流式响应被缓冲问题
spring boot·后端·nginx
難釋懷16 天前
Nginx获取客户端真实IP
服务器·前端·nginx
qq_谁赞成_谁反对16 天前
甲方IT的成长之路--nginx实战--2604
服务器·数据库·nginx
图灵追慕者16 天前
Nginx安裝以及配置顯示本地服務器文件夾
运维·nginx
rabbit_pro16 天前
Nginx配置维护模式
运维·nginx
楠目17 天前
Nginx 解析漏洞利用总结
nginx·网络安全