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;
        }
    }
相关推荐
还是大剑师兰特1 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing1 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
PC2005-cloud1 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
PHP实战开发录2 天前
Nginx上传大文件为什么报413
nginx·php·开发
Lsetea2 天前
Nginx报No required SSL certificate was sent:mTLS客户端证书排查
运维·nginx·https·ssl·openssl
szephyr3 天前
HTTPS 证书申请与自动续期:acme.sh + Let‘s Encrypt 完整记录
nginx·https·acme.sh·证书续期
Lsetea4 天前
部署证书后提示“证书名称与输入不匹配”:Safari 报错的三层排查
nginx·https·safari·ssl证书·openssl
打工仔折腾 AI4 天前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
Linux运维技术栈4 天前
如何避免绕过CDN的攻击?基于 CDN 回源请求头鉴权的防御实操与安全性剖析
nginx·安全·cdn
Lsetea4 天前
OpenSSL快速生成域名证书:含SAN的自签命令、验证与排错
nginx·https·ssl证书·openssl·自签证书