{"id":277,"date":"2019-08-18T18:39:09","date_gmt":"2019-08-18T10:39:09","guid":{"rendered":"http:\/\/www.leohe.net.cn\/?p=277"},"modified":"2019-10-18T23:34:12","modified_gmt":"2019-10-18T15:34:12","slug":"nginx-%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1","status":"publish","type":"post","link":"https:\/\/china-abc.leohe.net.cn\/?p=277","title":{"rendered":"nginx-\u8d1f\u8f7d\u5747\u8861"},"content":{"rendered":"\n<p>upstream testserver {<br>     # server 127.0.0.1:8080 weight=2;<br>     # server 127.0.0.1:8081 weight=1; #\u591a\u52a0\u4e86\u6b64\u53f0\u670d\u52a1\u5668 weight\u6743\u9650\u7ea7\u522b<br>     server 127.0.0.1:8080;<br>     server 127.0.0.1:8081; #\u591a\u52a0\u4e86\u6b64\u53f0\u670d\u52a1\u5668<br>     ip_hash;<br> }<br> upstream testserver1 {<br>     server 127.0.0.1:8081;<br> }<\/p>\n\n\n\n<p>\u4ee5\u4e0a testserver \u548c   testserver1 \u914d\u7f6e\u9879\u4e3a\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e\u9879\u548c\u5747\u8861\u670d\u52a1\u5668\u5217\u8868<\/p>\n\n\n\n<p>server {<br>     listen 80;<br>     server_name nginx-proxy.cn;<br>     location \/ {<br>         proxy_pass http:\/\/testserver;<br>         index index.html index.htm index.php l.php;<br>     }<br> }<\/p>\n\n\n\n<p>proxy_pass http:\/\/testserver  \u91cc\u7684testserver\u4e3a\u4e0a\u8fb9\u914d\u7f6e\u9879\u76ee\u540d\u79f0<\/p>\n\n\n\n<p style=\"text-align:center\"><strong> \u8d1f\u8f7d\u5747\u8861\u7b56\u7565 <\/strong><\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>\u65b9\u6cd5<\/strong><\/td><td><strong>\u8bf4\u660e<\/strong><\/td><td><strong>\u914d\u7f6e\u4e3e\u4f8b<\/strong><\/td><td><strong>\u6ce8\u610f<\/strong><\/td><\/tr><tr><td>\u8f6e\u8be2<\/td><td><strong>\u9ed8\u8ba4\u65b9\u5f0f<\/strong> \uff0c \u6700\u57fa\u672c\u7684\u914d\u7f6e\u65b9\u6cd5\uff0c<br>\u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u8f6e\u8be2\u7684\u65b9\u5f0f\uff0c<br>\u5b83\u662fupstream\u6a21\u5757\u9ed8\u8ba4\u7684\u8d1f<br>\u8f7d\u5747\u8861\u9ed8\u8ba4\u7b56\u7565\u3002\u6bcf\u4e2a\u8bf7\u6c42\u4f1a<br>\u6309\u65f6\u95f4\u987a\u5e8f\u9010\u4e00\u5206\u914d\u5230\u4e0d\u540c\u7684<br>\u540e\u7aef\u670d\u52a1\u5668\u3002 <\/td><td><\/td><td><\/td><\/tr><tr><td>weight<\/td><td><strong>\u6743\u91cd\u65b9\u5f0f<\/strong>  \uff0c\u5728\u8f6e\u8be2\u7b56\u7565\u7684\u57fa<br>\u7840\u4e0a\u6307\u5b9a\u8f6e\u8be2\u7684\u51e0\u7387 <\/td><td>upstream bend {<br>     server 192.168.0.14 weight=10;<br>     server 192.168.0.15 weight=10;<br> }<\/td><td> \u6743\u91cd\u8d8a\u9ad8\u5206\u914d\u5230\u9700\u8981\u5904\u7406\u7684\u8bf7\u6c42\u8d8a\u591a\u3002<br>\u6b64\u7b56\u7565\u53ef\u4ee5\u4e0eleast_conn\u548cip_hash\u7ed3\u5408\u4f7f\u7528\u3002<br>\u6b64\u7b56\u7565\u6bd4\u8f83\u9002\u5408\u670d\u52a1\u5668\u7684\u786c\u4ef6\u914d\u7f6e\u5dee\u522b\u6bd4\u8f83\u5927\u7684\u60c5\u51b5\u3002 <\/td><\/tr><tr><td>ip_hash<\/td><td><strong>\u4f9d\u636eip\u5206\u914d\u65b9\u5f0f<\/strong>\uff0c \u6307\u5b9a\u8d1f\u8f7d<br>\u5747\u8861\u5668\u6309\u7167\u57fa\u4e8e\u5ba2\u6237\u7aefIP\u7684<br>\u5206\u914d\u65b9\u5f0f\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u786e\u4fdd\u4e86<br>\u76f8\u540c\u7684\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4e00\u76f4\u53d1<br>\u9001\u5230\u76f8\u540c\u7684\u670d\u52a1\u5668\uff0c\u4ee5\u4fdd\u8bc1<br>session\u4f1a\u8bdd\u3002\u8fd9\u6837\u6bcf\u4e2a\u8bbf<br>\u5ba2\u90fd\u56fa\u5b9a\u8bbf\u95ee\u4e00\u4e2a\u540e\u7aef\u670d<br>\u52a1\u5668\uff0c\u53ef\u4ee5\u89e3\u51b3session\u4e0d<br>\u80fd\u8de8\u670d\u52a1\u5668\u7684\u95ee\u9898\u3002 <\/td><td>upstream dynamic {<br>     ip_hash;    #\u8bbf\u5ba2\u56fa\u5b9a\u8bbf\u95ee\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\u5668<br>     server localhost:8080   weight=2;<br>     server localhost:8081;<br>     server localhost:8082;<br>     server localhost:8083   max_fails=3 fail_timeout=20s;<br> }<\/td><td>ip_hash\u4e0d\u80fd\u4e0ebackup\u540c\u65f6\u4f7f\u7528\u3002<br> \u6b64\u7b56\u7565\u9002\u5408\u6709\u72b6\u6001\u670d\u52a1\uff0c\u6bd4\u5982session\u3002<br> \u5f53\u6709\u670d\u52a1\u5668\u9700\u8981\u5254\u9664\uff0c\u5fc5\u987b\u624b\u52a8down\u6389\u3002<\/td><\/tr><tr><td>least_conn<\/td><td><strong>\u6700\u5c11\u8fde\u63a5\u65b9\u5f0f<\/strong>\uff0c \u628a\u8bf7\u6c42\u8f6c<br>\u53d1\u7ed9\u8fde\u63a5\u6570\u8f83\u5c11\u7684\u540e\u7aef\u670d<br>\u52a1\u5668\u3002\u8f6e\u8be2\u7b97\u6cd5\u662f\u628a\u8bf7\u6c42<br>\u5e73\u5747\u7684\u8f6c\u53d1\u7ed9\u5404\u4e2a\u540e\u7aef\uff0c<br>\u4f7f\u5b83\u4eec\u7684\u8d1f\u8f7d\u5927\u81f4\u76f8\u540c\uff1b<br>\u4f46\u662f\uff0c\u6709\u4e9b\u8bf7\u6c42\u5360\u7528\u7684<br>\u65f6\u95f4\u5f88\u957f\uff0c\u4f1a\u5bfc\u81f4\u5176\u6240<br>\u5728\u7684\u540e\u7aef\u8d1f\u8f7d\u8f83\u9ad8\u3002\u8fd9<br>\u79cd\u60c5\u51b5\u4e0b\uff0cleast_conn\u8fd9\u79cd\u65b9\u5f0f\u5c31\u53ef\u4ee5\u8fbe\u5230\u66f4\u597d\u7684\u8d1f\u8f7d\u5747\u8861\u6548\u679c\u3002 <\/td><td>upstream dynamic {<br>     least_conn;    #\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u8fde\u63a5\u6570\u8f83\u5c11\u7684\u670d\u52a1\u5668<br>     server localhost:8080   weight=2;<br>     server localhost:8081;<br>     server localhost:8082 backup;<br>     server localhost:8083   max_fails=3 fail_timeout=20s;<br> }<\/td><td> \u6b64\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u9002\u5408\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u957f\u77ed\u4e0d\u4e00\u9020\u6210\u670d\u52a1\u5668\u8fc7\u8f7d\u7684\u60c5\u51b5\u3002 <\/td><\/tr><tr><td>fair\uff08\u7b2c\u4e09\u65b9\uff09<\/td><td>\u54cd\u5e94\u65f6\u95f4\u65b9\u5f0f\uff0c  \u6309\u7167\u670d\u52a1<br>\u5668\u7aef\u7684\u54cd\u5e94\u65f6\u95f4\u6765\u5206\u914d\u8bf7<br>\u6c42\uff0c\u54cd\u5e94\u65f6\u95f4\u77ed\u7684\u4f18\u5148\u5206\u914d <\/td><td> upstream dynamic {<br>     server localhost:8080;  #tomcat 7.0<br>     server localhost:8081;  #tomcat 8.0<br>     server localhost:8082;  #tomcat 8.5<br>     server localhost:8083;  #tomcat 9.0<br>     fair;    #\u5b9e\u73b0\u54cd\u5e94\u65f6\u95f4\u77ed\u7684\u4f18\u5148\u5206\u914d<br> }<\/td><td><\/td><\/tr><tr><td>url_hash\uff08\u7b2c\u4e09\u65b9\uff09<\/td><td>\u4f9d\u636eURL\u5206\u914d\u65b9\u5f0f\uff0c\u6309\u8bbf<br>\u95eeurl\u7684hash\u7ed3\u679c\u6765\u5206\u914d\u8bf7<br>\u6c42\uff0c\u4f7f\u6bcf\u4e2aurl\u5b9a\u5411\u5230\u540c\u4e00<br>\u4e2a\u540e\u7aef\u670d\u52a1\u5668\uff0c\u8981\u914d\u5408\u7f13\u5b58<br>\u547d\u4e2d\u6765\u4f7f\u7528\u3002\u540c\u4e00\u4e2a\u8d44\u6e90<br>\u591a\u6b21\u8bf7\u6c42\uff0c\u53ef\u80fd\u4f1a\u5230\u8fbe\u4e0d<br>\u540c\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u5bfc\u81f4\u4e0d\u5fc5<br>\u8981\u7684\u591a\u6b21\u4e0b\u8f7d\uff0c\u7f13\u5b58\u547d<br>\u4e2d\u7387\u4e0d\u9ad8\uff0c\u4ee5\u53ca\u4e00\u4e9b\u8d44<br>\u6e90\u65f6\u95f4\u7684\u6d6a\u8d39\u3002\u800c\u4f7f\u7528<br>url_hash\uff0c\u53ef\u4ee5\u4f7f\u5f97<br>\u540c\u4e00\u4e2aurl\uff08\u4e5f\u5c31\u662f\u540c<br>\u4e00\u4e2a\u8d44\u6e90\u8bf7\u6c42\uff09\u4f1a\u5230<br>\u8fbe\u540c\u4e00\u53f0\u670d\u52a1\u5668\uff0c<br>\u4e00\u65e6\u7f13\u5b58\u4f4f\u4e86\u8d44\u6e90\uff0c\u518d\u6b64\u6536\u5230\u8bf7\u6c42\uff0c<br>\u5c31\u53ef\u4ee5\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\u3002\u3000<\/td><td>upstream dynamic {<br>     hash $request_uri;    #\u5b9e\u73b0\u6bcf\u4e2aurl\u5b9a\u5411\u5230\u540c\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\u5668<br>     server localhost:8080;  #tomcat 7.0<br>     server localhost:8081;  #tomcat 8.0<br>     server localhost:8082;  #tomcat 8.5<br>     server localhost:8083;  #tomcat 9.0<br> }<\/td><td><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p style=\"text-align:center\"><strong>\u914d\u7f6e\u9879\u8bf4\u660e<\/strong><\/p>\n\n\n\n<p>1.down \u8868\u793a\u5355\u524d\u7684server\u6682\u65f6\u4e0d\u53c2\u4e0e\u8d1f\u8f7d<br>2.weight \u9ed8\u8ba4\u4e3a1.weight\u8d8a\u5927\uff0c\u8d1f\u8f7d\u7684\u6743\u91cd\u5c31\u8d8a\u5927\u3002<br>3.max_fails \uff1a\u5141\u8bb8\u8bf7\u6c42\u5931\u8d25\u7684\u6b21\u6570\u9ed8\u8ba4\u4e3a1.\u5f53\u8d85\u8fc7\u6700\u5927\u6b21\u6570\u65f6\uff0c\u8fd4\u56deproxy_next_upstream \u6a21\u5757\u5b9a\u4e49\u7684\u9519\u8bef<br>4.fail_timeout:max_fails\u6b21\u5931\u8d25\u540e\uff0c\u6682\u505c\u7684\u65f6\u95f4\u3002<br>5.backup\uff1a \u5176\u5b83\u6240\u6709\u7684\u975ebackup\u673a\u5668down\u6216\u8005\u5fd9\u7684\u65f6\u5019\uff0c\u8bf7\u6c42backup\u673a\u5668\u3002\u6240\u4ee5\u8fd9\u53f0\u673a\u5668\u538b\u529b\u4f1a\u6700\u8f7b\u3002<br> nginx\u652f\u6301\u540c\u65f6\u8bbe\u7f6e\u591a\u7ec4\u7684\u8d1f\u8f7d\u5747\u8861\uff0c\u7528\u6765\u7ed9\u4e0d\u7528\u7684server\u6765\u4f7f\u7528\u3002<br> client_body_in_file_only \u8bbe\u7f6e\u4e3aOn \u53ef\u4ee5\u8bb2client post\u8fc7\u6765\u7684\u6570\u636e\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\u7528\u6765\u505adebug<br> client_body_temp_path \u8bbe\u7f6e\u8bb0\u5f55\u6587\u4ef6\u7684\u76ee\u5f55 \u53ef\u4ee5\u8bbe\u7f6e\u6700\u591a3\u5c42\u76ee\u5f55<br> location \u5bf9URL\u8fdb\u884c\u5339\u914d.\u53ef\u4ee5\u8fdb\u884c\u91cd\u5b9a\u5411\u6216\u8005\u8fdb\u884c\u65b0\u7684\u4ee3\u7406 \u8d1f\u8f7d\u5747\u8861<\/p>\n","protected":false},"excerpt":{"rendered":"<p>upstream testserver { # server 127.0.0.1:8080 weight=2; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,12,5],"tags":[],"_links":{"self":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/277"}],"collection":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=277"}],"version-history":[{"count":6,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/277\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}