{"id":1108,"date":"2018-12-19T10:17:20","date_gmt":"2018-12-19T02:17:20","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=1108"},"modified":"2018-12-19T10:17:20","modified_gmt":"2018-12-19T02:17:20","slug":"nginx%e9%80%9a%e8%bf%87ip-hash%e7%ae%97%e6%b3%95%e8%b4%9f%e8%bd%bd%e4%b8%8d%e5%9d%87%e6%88%96%e4%b8%8d%e8%b5%b7%e6%95%88%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=1108","title":{"rendered":"nginx\u901a\u8fc7ip-hash\u7b97\u6cd5\u8d1f\u8f7d\u4e0d\u5747\u6216\u4e0d\u8d77\u6548\u95ee\u9898"},"content":{"rendered":"<p>\u73b0\u8c61\uff1aNginx\u548c\u5ba2\u6237\u7aef\u5904\u4e8e\u540c\u4e00\u4e2a\u5c40\u57df\u7f51\uff0c\u4f7f\u7528ip_hash\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u4e0d\u540c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u5168\u90e8\u90fd\u5206\u53d1\u5230\u540c\u4e00\u4e2a\u540e\u53f0\u670d\u52a1\u5668<\/p>\n<p>&nbsp;<\/p>\n<p>\u539f\u56e0\uff1a<\/p>\n<p>1\u3001\u8bf7\u770b\u5b98\u65b9\u89e3\u91ca\uff1a<br \/>\nThis directive causes requests to be distributed between upstreams based on the IP-address of the client.<br \/>\nThe key for the hash is the class-C network address or the entire IPv6-address of the client. IPv6 is supported for ip_hash since 1.3.2 or 1.2.2. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server. \uff08\u7b80\u8bd1\uff1a\u5c06\u5ba2\u6237\u7aefip\u8f6c\u5316\u6210C\u7c7b\u7f51\u7edc\u5730\u5740\uff0c\u7136\u540e\u5c06\u8be5\u7f51\u7edc\u5730\u5740\u5f53\u4f5chash\u5173\u952e\u5b57\uff0c\u6765\u4fdd\u8bc1\u8fd9\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\u603b\u662f\u88ab\u8f6c\u53d1\u5230\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>2\u3001\u8bf7\u770bNginx\u7684ip hash\u7b97\u6cd5<\/p>\n<p>&nbsp;<\/p>\n<p>for ( ;; ) {<\/p>\n<p>&nbsp;<\/p>\n<p>for (i = 0; i &lt; 3; i++) {<\/p>\n<p>hash = (hash * 113 + iphp-&gt;addr[i]) % 6271; \/\/iphp-&gt;addr[i]\u4e3aip\u7684\u70b9\u5206\u5341\u8fdb\u5236\u6cd5\u7684\u7b2ci\u6bb5<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>p = hash % iphp-&gt;rrp.peers-&gt;number;<\/p>\n<p>&nbsp;<\/p>\n<p>n = p \/ (8 * sizeof(uintptr_t));<\/p>\n<p>m = (uintptr_t) 1 &lt;&lt; p % (8 * sizeof(uintptr_t));<\/p>\n<p>&nbsp;<\/p>\n<p>if (!(iphp-&gt;rrp.tried[n] &amp; m)) {<\/p>\n<p>&nbsp;<\/p>\n<p>ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc-&gt;log, 0,<\/p>\n<p>&#8220;get ip hash peer, hash: %ui %04XA&#8221;, p, m);<\/p>\n<p>&nbsp;<\/p>\n<p>peer = &amp;iphp-&gt;rrp.peers-&gt;peer[p];<\/p>\n<p>&nbsp;<\/p>\n<p>\/* ngx_lock_mutex(iphp-&gt;rrp.peers-&gt;mutex); *\/<\/p>\n<p>&nbsp;<\/p>\n<p>if (!peer-&gt;down) {<\/p>\n<p>&nbsp;<\/p>\n<p>if (peer-&gt;max_fails == 0 || peer-&gt;fails &lt; peer-&gt;max_fails) {<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>if (now &#8211; peer-&gt;accessed &gt; peer-&gt;fail_timeout) {<\/p>\n<p>peer-&gt;fails = 0;<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>iphp-&gt;rrp.tried[n] |= m;<\/p>\n<p>&nbsp;<\/p>\n<p>\/* ngx_unlock_mutex(iphp-&gt;rrp.peers-&gt;mutex); *\/<\/p>\n<p>&nbsp;<\/p>\n<p>pc-&gt;tries&#8211;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>if (++iphp-&gt;tries &gt;= 20) {<\/p>\n<p>return iphp-&gt;get_rr_peer(pc, &amp;iphp-&gt;rrp);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e3b\u8981\u4ee3\u7801<\/p>\n<p>&nbsp;<\/p>\n<p>for(i = 0; i &lt; 3; i++) {<\/p>\n<p>hash = (hash * 113+ iphp-&gt;addr[i]) % 6271;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>for\u5faa\u73afi\u53d6 012\u4e09\u4e2a\u503c\uff0c\u800cip\u7684\u70b9\u5206\u5341\u8fdb\u5236\u8868\u793a\u65b9\u6cd5\u5c06ip\u5206\u6210\u56db\u6bb5\uff08\u5982\uff1a192.168.1.1\uff09\uff0c\u4f46\u662f\u8fd9\u91cc\u5faa\u73af\u65f6\u53ea\u662f\u5c06ip\u7684\u524d\u4e09\u4e2a\u6bb5\u4f5c\u4e3a\u53c2\u6570\u52a0\u5165hash\u51fd\u6570\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\u4fdd\u8bc1ip\u5730\u5740\u524d\u4e09\u4f4d\u76f8\u540c\u7684\u7528\u6237\u7ecf\u8fc7hash\u8ba1\u7b97\u5c06\u5206\u914d\u5230\u76f8\u540c\u7684\u540e\u7aefserver\u3002<\/p>\n<p>\u4f5c\u8005\u7684\u8fd9\u4e2a\u8003\u8651\u662f\u6781\u4e3a\u53ef\u53d6\u7684\uff0c\u56e0\u6b64ip\u5730\u5740\u524d\u4e09\u4f4d\u76f8\u540c\u901a\u5e38\u610f\u5473\u7740\u6765\u7740\u540c\u4e00\u4e2a\u5c40\u57df\u7f51\u6216\u8005\u76f8\u90bb\u533a\u57df\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u540e\u7aef\u670d\u52a1\u8ba9nginx\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u66f4\u5177\u6709\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u89e3\u91ca\uff0c\u5df2\u7ecf\u57fa\u672c\u5224\u65ad\u51fa\u95ee\u9898\u6240\u5728\u4e86\u3002\u3002<br \/>\n\u4e3b\u8981\u539f\u56e0\u5c31\u662f\uff0c\u516c\u53f8\u4f7f\u7528\u7684\u5c40\u57df\u7f51\u524d\u4e09\u6bb5ip\u76f8\u540c\uff0c\u8fd9\u6837Nginx\u5728ip_hash\uff08for\u5faa\u73af\u540e\u4e09\u4e2a\u53c2\u6570\u7edf\u4e00\u8ba1\u5165hash\u503c\uff09\u7684\u65f6\u5019\uff0c\u5c31\u5c06\u8be5\u7c7b\u6240\u6709ip\u90fd\u8f6c\u53d1\u5230\u4e00\u4e2a\u540e\u7aef\u4e86\u3002<\/p>\n<p>\u53e6\uff0c\u4e0d\u8bbaA\u7c7bB\u7c7bC\u7c7b\u7b49\u7f51\u7edc\u5730\u5740\uff0cNginx\u7684ip_hash\u7b97\u6cd5\u90fd\u5c06\u4e00\u4e2aip\u5730\u5740\u7684\u524d\u4e09\u6bb5\u4f5c\u4e3ahash\u7684\u5173\u952e\u5b57\u3002<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u8c61\uff1aNginx\u548c\u5ba2\u6237\u7aef\u5904\u4e8e\u540c\u4e00\u4e2a\u5c40\u57df\u7f51\uff0c\u4f7f\u7528ip_hash\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u4e0d\u540c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u5168\u90e8\u90fd\u5206\u53d1\u5230\u540c\u4e00\u4e2a\u540e\u53f0\u670d\u52a1\u5668 &nbsp; \u539f\u56e0\uff1a 1\u3001\u8bf7\u770b\u5b98\u65b9\u89e3\u91ca\uff1a This directive causes requests to be distributed between upstreams based on the IP-address of the client. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1108","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1108"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1108\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}