加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

[日常] nginx与负载均衡策略

发布时间:2020-12-13 21:42:59 所属栏目:Nginx 来源:网络整理
导读:div class="cnblogs_code" #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。 server /spanspan style="color: #800080"gt;192.168/span.span style="color: #800080"gt;80.121/span:span st

<div class="cnblogs_code">

    #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;80.121</span>:<span style="color: #800080"&gt;80</span> weight=<span style="color: #800080"&gt;3</span><span style="color: #000000"&gt;;
    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;80.122</span>:<span style="color: #800080"&gt;80</span> weight=<span style="color: #800080"&gt;2</span><span style="color: #000000"&gt;;
    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;80.123</span>:<span style="color: #800080"&gt;80</span> weight=<span style="color: #800080"&gt;3</span><span style="color: #000000"&gt;;

    #nginx的upstream目前支持4种方式的分配
    #</span><span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;、轮询(默认)
    #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
    #</span><span style="color: #800080"&gt;2</span><span style="color: #000000"&gt;、weight
    #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    #例如:
    #upstream bakend {
    #    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;0.14</span> weight=<span style="color: #800080"&gt;10</span><span style="color: #000000"&gt;;
    #    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;0.15</span> weight=<span style="color: #800080"&gt;10</span><span style="color: #000000"&gt;;
    #}
    #</span><span style="color: #800080"&gt;2</span><span style="color: #000000"&gt;、ip_hash
    #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    #例如:
    #upstream bakend {
    #    ip_hash;
    #    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;0.14</span>:<span style="color: #800080"&gt;88</span><span style="color: #000000"&gt;;
    #    server </span><span style="color: #800080"&gt;192.168</span>.<span style="color: #800080"&gt;0.15</span>:<span style="color: #800080"&gt;80</span><span style="color: #000000"&gt;;
    #}
    #</span><span style="color: #800080"&gt;3</span><span style="color: #000000"&gt;、fair(第三方)
    #按后端服务器的响应时间来分配请求,响应时间短的优先分配。
    #upstream backend {
    #    server server1;
    #    server server2;
    #    fair;
    #}
    #</span><span style="color: #800080"&gt;4</span><span style="color: #000000"&gt;、url_hash(第三方)
    #按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
    #例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
    #upstream backend {
    #    server squid1:</span><span style="color: #800080"&gt;3128</span><span style="color: #000000"&gt;;
    #    server squid2:</span><span style="color: #800080"&gt;3128</span><span style="color: #000000"&gt;;
    #    hash $request_uri;
    #    hash_method crc32;
    #}

    #tips:
    #upstream bakend{#定义负载均衡设备的Ip及设备状态}{
    #    ip_hash;
    #    server </span><span style="color: #800080"&gt;127.0</span>.<span style="color: #800080"&gt;0.1</span>:<span style="color: #800080"&gt;9090</span><span style="color: #000000"&gt; down;
    #    server </span><span style="color: #800080"&gt;127.0</span>.<span style="color: #800080"&gt;0.1</span>:<span style="color: #800080"&gt;8080</span> weight=<span style="color: #800080"&gt;2</span><span style="color: #000000"&gt;;
    #    server </span><span style="color: #800080"&gt;127.0</span>.<span style="color: #800080"&gt;0.1</span>:<span style="color: #800080"&gt;6060</span><span style="color: #000000"&gt;;
    #    server </span><span style="color: #800080"&gt;127.0</span>.<span style="color: #800080"&gt;0.1</span>:<span style="color: #800080"&gt;7070</span><span style="color: #000000"&gt; backup;
    #}
    #在需要使用负载均衡的server中增加 proxy_pass http:</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;bakend/;</span>

<span style="color: #000000">

每个设备的状态设置为:

    #</span><span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;.down表示单前的server暂时不参与负载
    #</span><span style="color: #800080"&gt;2</span><span style="color: #000000"&gt;.weight为weight越大,负载的权重就越大。
    #</span><span style="color: #800080"&gt;3</span><span style="color: #000000"&gt;.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
    #</span><span style="color: #800080"&gt;4</span><span style="color: #000000"&gt;.fail_timeout:max_fails次失败后,暂停的时间。
    #</span><span style="color: #800080"&gt;5</span><span style="color: #000000"&gt;.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读