{"id":1106,"date":"2018-12-19T10:16:37","date_gmt":"2018-12-19T02:16:37","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=1106"},"modified":"2018-12-19T10:16:37","modified_gmt":"2018-12-19T02:16:37","slug":"%e9%97%ae%e9%a2%98%e6%8e%92%e6%9f%a5-nginx-ip_hash%e5%b0%86%e6%89%80%e6%9c%89%e8%af%b7%e6%b1%82%e8%bd%ac%e5%8f%91%e5%88%b0%e4%b8%80%e5%8f%b0%e6%9c%ba%e5%99%a8","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=1106","title":{"rendered":"[\u95ee\u9898\u6392\u67e5] nginx ip_hash\u5c06\u6240\u6709\u8bf7\u6c42\u8f6c\u53d1\u5230\u4e00\u53f0\u673a\u5668"},"content":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0\uff1a\u963f\u91cc\u4e91nginx ip_hash\u5c06\u6240\u6709\u8bf7\u6c42\u8f6c\u53d1\u5230\u4e00\u53f0\u673a\u5668<br \/>\n\u6392\u67e5\uff1a\u67e5\u770bnginx\u7684error.log, \u53d1\u73b0\u5927\u90e8\u5206\u7684client ip\u662f\u6765\u81ea\u4e8e10.159.95\u7f51\u6bb5<\/p>\n<div>\n<div class=\"codeheads\">\n<p>\u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00<\/p>\n<\/div>\n<div id=\"code257\" class=\"codeText\">\n<ol class=\"dp-css\" start=\"1\">\n<li>2015\/10\/25 12:01:03 [warn] 21580#0: *12412231 an upstream response is buffered to a temporary file \/home\/work\/nginx\/proxy_temp\/2\/55\/0000075552 while reading upstream,\u00a0client:\u00a010.159.95.***, server: ***.***.***, request: &#8220;POST\u00a0***\u00a0HTTP\/1.0&#8221;, upstream: &#8220;http:\/\/***&#8221;, host: &#8220;***.***.***&#8221;<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>\u6000\u7591nginx\u672a\u83b7\u53d6\u5230\u771f\u5b9e\u7684\u7528\u6237ip\uff0c\u800c\u83b7\u53d6\u5230\u4e0a\u6e38SLB\u96c6\u7fa4\u7684IP\u3002<\/p>\n<p>nginx\u5b98\u65b9\u7f51\u7ad9\u4e2d\u5173\u4e8eip_hash\u7684\u8bf4\u660e<br \/>\n<em>Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses.\u00a0<\/em><em>The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key.\u00a0<\/em><em>The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.<\/em><br \/>\n\u98d8\u7ea2\u5b57\u6bb5\u7684\u610f\u601d\u662f\uff0c\u5bf9\u4e8eIPv4\uff0cip_hash\u4f1a\u4f7f\u7528IP\u7684\u524d3\u90e8\u5206\u4f5c\u4e3a\u54c8\u5e0c\u7684Key\uff0c\u4e00\u4e2aSLB\u96c6\u7fa4\u5bf9\u5e94\u7684\u7f51\u6bb5\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u5bfc\u81f4ip_hash\u4e4b\u540e\u7684\u8bf7\u6c42\u5168\u90e8\u843d\u5230\u4e00\u53f0upstream\u673a\u5668\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a\u8ba9nginx\u83b7\u53d6\u5230\u7528\u6237\u7684\u771f\u5b9eIP\uff0c\u4f7f\u7528real client ip\u505aip_hash<br \/>\n1. \u91cd\u65b0\u7f16\u8bd1nginx, \u589e\u52a0&#8211;with-http_realip_module\u6a21\u5757<br \/>\n2. \u5728nginx.conf\u4e2dhttp{}\u4e2d\uff0c\u589e\u52a0<\/p>\n<div>\n<div class=\"codeheads\">\n<p>\u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00<\/p>\n<\/div>\n<div id=\"code769\" class=\"codeText\">\n<ol class=\"dp-css\" start=\"1\">\n<li>real_ip_header X-Forwarded-For;<\/li>\n<li>set_real_ip_from 0.0.0.0\/0;<\/li>\n<li>real_ip_recursive on;<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>3. \u91cd\u542fnginx\uff0c\u67e5\u770berror.log\u68c0\u67e5client: \u5bf9\u5e94\u65e5\u5fd7\uff0c\u5df2\u7ecf\u53d8\u6210\u4e86\u7528\u6237\u771f\u5b9e\u7684\u5916\u7f51\u51fa\u53e3IP<br \/>\n4. \u68c0\u67e5upstream\u7684\u673a\u5668\u6d41\u91cf\uff0c\u5df2\u8d8b\u4e8e\u5747\u8861<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0\uff1a\u963f\u91cc\u4e91nginx ip_hash\u5c06\u6240\u6709\u8bf7\u6c42\u8f6c\u53d1\u5230\u4e00\u53f0\u673a\u5668 \u6392\u67e5\uff1a\u67e5\u770bnginx\u7684error.log, \u53d1\u73b0\u5927\u90e8\u5206\u7684client ip\u662f\u6765\u81ea\u4e8e10.159.95\u7f51\u6bb5 \u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00 2015\/10\/25 12:01:03 [warn] 21580#0: *12412231 an upstream response is buffered to a temporary file [&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-1106","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1106","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=1106"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1106\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}