阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx负载均衡实现

160次阅读
没有评论

共计 1938 个字符,预计需要花费 5 分钟才能阅读完成。

一、负载均衡方式
1、轮询

upstream test_up {
    server localhost:8080;   
            server localhost:9090;   
            server localhost:9090;
}

server {
      listen 80;
      server_name test;

      location /test.html {
            proxy_pass http://test_up;
      }}

以上代码当在浏览器地址栏中输入 http://test/test.html 时,nginx 会按 test_up 中配置的服务器顺序依次访问后端服务器

2、权重分配

upstream test_up {
    server localhost:8080 weight=1;
    server localhost:9090 weight=2;
}

server {
      listen 80;
      server_name test;

      location /test.html {
            proxy_pass http://test_up;
      }
}

以上代码 NGINX 会根据权重值的分布,动态将前端请求分配给后端服务器

3、IP HASH 分配

upstream test_up {
    server localhost:8080 weight=1;
    server localhost:9090 weight=2;
    ip_hash;
}

server {
    listen 80;
    server_name test;

    location /test.html {
        proxy_pass http://test_up
    }
}

IP HASH 方式是根据客户端IP进行 HASH 后,将客户端请求分配给后端服务器

特别注意:上面代码中虽然在每个 server 后面配置了权重,但采用了 IP HASH 方式后,实际上权重并不会生效

二、对 SERVER 进行控制
1.down 表示单前的 server 暂时不参与负载
2.weight 默认为 1.weight 越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为 1. 当超过最大次数时,返回 proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails 次失败后,暂停的时间。
5.backup:其它所有的非 backup 机器 down 或者忙的时候,请求 backup 机器。所以这台机器压力会最轻。

nginx 支持同时设置多组的负载均衡,用来给不用的 server 来使用。
client_body_in_file_only 设置为 On 可以讲 client post 过来的数据记录到文件中用来做 debug
client_body_temp_path 设置记录文件的目录 可以设置最多 3 层目录
location 对 URL 进行匹配. 可以进行重定向或者进行新的代理 负载均衡

————————————– 分割线 ————————————–

Nginx 反向代理搭建配置及搭建过程一些思考  http://www.linuxidc.com/Linux/2015-01/111702.htm

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计1938字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中