Nginx   发布时间:2019-11-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[日常] nginx与负载均衡策略大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="cnblogs_code">

l.sina.net {
    #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>

大佬总结

以上是大佬教程为你收集整理的[日常] nginx与负载均衡策略全部内容,希望文章能够帮你解决[日常] nginx与负载均衡策略所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: