• nginx实现负载均衡


    proxy_pass、upstream与resolver

    upstream

    upstream将创建一个上游服务配置项,用于交给proxy_pass 转发ip.

    1
    2
    3
        upstream x.cn {
            server 192.168.192.134:80;
        }

    只有当proxy_passs调用时,upstream才会生效:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    upstream x.cn {
         server 192.168.192.134:80;
    }
     
    server
    {
        listen 80;
        server_name 1.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/1.cn;
         
         location / {
                proxy_pass http://x.cn;
         }
    }

    当访问服务器1.cn时,读取 proxy_pass代理配置,获取到upstream ip,将请求转发到 192.168.192.134

    host为x.cn

    proxy_pass反向代理

    通过proxy_pass,可将请求代理到指定服务中,例如:

    1
    2
    3
    4
    5
    6
    7
    8
     server {
            listen       80;
            server_name  localhost;
     
            location /proxy {
                proxy_pass http://x.cn;
            }
         }

    当访问localhost/proxy时,nginx将会自动解析x.cn ip,将请求代理到 解析后的ip中.

    当x.cn无法解析或未设置dns服务器时,将会报错

    1
    <span style="color: rgb(255, 0, 0);">nginx: [emerg] host not found in upstream "x.cn" in /www/server/panel/vhost/nginx/1.cn.conf:9<br></span>

    当没有设置upstream时,proxy_pass将通过dns服务器解析ip,默认添加一个upstream ip,用于实现转发请求.

    反向代理配置项:

          #代理配置参数
            proxy_connect_timeout 180;
            proxy_send_timeout 180;
            proxy_read_timeout 180;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarder-For $remote_addr;

    resolve

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    upstream 1.cn {
         server 192.168.192.134:80;
    }
     
    server
    {
        listen 80;
        server_name 1.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/1.cn;
        resolver 114.114.114.114;
        location / {
               set $testCn 1.cn;
                proxy_pass http://$testCn;
         }
    }

    当设置resolve后, nginx将会忽略本身设置的dns,本机的hosts,直接通过resolve的dns服务器动态获取ip,用于转发

    只有通过变量设置域名,resolve的dns解析才会生效

    upstream负载均衡

    在上面,我们已经了解到了upstream可以设置代理服务器的ip,转发请求,这个代理ip,是可以设置多个的,例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    upstream 1.cn {
         server 192.168.192.134:80;
         server 127.0.0.1:80;
         server 192.168.192.139:80;
    }
     
    server
    {
        listen 80;
        server_name x.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/1.cn;
        location / {
               set $testCn 1.cn;
                proxy_pass http://$testCn;
         }
    }

    我们同时在3台服务器,新增网站1.cn,并且配置不一样的数据显示,用于区分.

    注意: 如果nginx代理本机,请不要访问同一个域名,例如,本机访问1.cn,会造成转发到本机的1.cn->再转发到本机的1.cn,导致出错

    所以我在这边的配置项改为了访问主服务器的x.cn,代理到主服务器,以及其他2台副服务器的1.cn中.

    仙士可博客

    仙士可博客

    仙士可博客

    这样就实现了nginx负载均衡

    upstream权重负载

    通过设置 weight,即可设置权重区分负载均衡.例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    upstream 1.cn {
         server 192.168.192.134:80 weight=2;
         server 127.0.0.1:80 weight=5;
         server 192.168.192.139:80;
    }
     
    server
    {
        listen 80;
        server_name x.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/x.cn;
        location / {
               set $testCn 1.cn;
                proxy_pass http://$testCn;
         }
    }

    设置权重后,nginx在代理时将尽可能平均的前提下通过权重大小进行分配代理服务器.例如a=5,b=3,c=2

    请求代理顺序可能为 abacabacaba,在尽量平均请求的前提下增加节点的请求数.

    upstream ip hash取模分组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    upstream 1.cn {
         ip_hash;
         server 192.168.192.134:80 weight=2;
         server 127.0.0.1:80 weight=5;
         server 192.168.192.139:80;
    }
     
    server
    {
        listen 80;
        server_name x.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/x.cn;
        location / {
               set $testCn 1.cn;
                proxy_pass http://$testCn;
         }
    }

    增加ip_hash配置项后,weight将失效,nginx将通过请求ip进行取模,同一ip的请求将分配到固定的一台服务器上

    upstream backup

    当其他上游节点全部出现异常时,nginx才会将请求转发到backup:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    upstream 1.cn {
         server 192.168.192.134:80;
         server 127.0.0.1:80 backup;
         server 192.168.192.139:80;
    }
     
    server
    {
        listen 80;
        server_name x.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/x.cn;
        location / {
               set $testCn 1.cn;
                proxy_pass http://$testCn;
         }
    }
  • 相关阅读:
    JSON序列化选项
    JOSN的stringify()和parse()方法
    html5增强元素--续
    html5页面增强元素
    js继承模式
    js常用设计模式
    js跨浏览器事件处理
    前端兴趣浓厚,后端提不起来兴趣
    padding的讲究
    margin的讲究
  • 原文地址:https://www.cnblogs.com/myJuly/p/12973675.html
Copyright © 2020-2023  润新知