{"id":654,"date":"2022-03-02T08:12:10","date_gmt":"2022-03-02T00:12:10","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=654"},"modified":"2022-03-02T08:12:12","modified_gmt":"2022-03-02T00:12:12","slug":"10-nginx%e7%9a%84%e5%9b%9b%e5%b1%82%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=654","title":{"rendered":"10.Nginx\u7684\u56db\u5c42\u8d1f\u8f7d\u5747\u8861"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.Nginx\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u57fa\u672c\u6982\u8ff0<\/h2>\n\n\n\n<p><strong><em>1.\u4ec0\u4e48\u662f\u56db\u5c42\u8d1f\u8f7d\u5747\u8861<\/em><\/strong><\/p>\n\n\n\n<p>\u6240\u8c13\u56db\u5c42\u5c31\u662f\u57fa\u4e8eIP+\u7aef\u53e3\u7684\u8d1f\u8f7d\u5747\u8861\uff0c\u5b83\u901a\u8fc7\u7528\u6237\u8bf7\u6c42\u7684\u7aef\u53e3\u6765\u51b3\u5b9a\u5c06\u8bf7\u6c42\u8f6c\u53d1\u81f3\u54ea\u53f0\u670d\u52a1\u5668\u3002<br>\u5c31\u662f\u901a\u8fc7\u4e09\u5c42\u7684IP\u5730\u5740\u5e76\u52a0\u4e0a\u56db\u5c42\u7684\u7aef\u53e3\u53f7\uff0c\u6765\u51b3\u5b9a\u54ea\u4e9b\u6d41\u91cf\u9700\u8981\u505a\u8d1f\u8f7d\u5747\u8861\u3002\u5bf9\u9700\u8981\u8d1f\u8f7d\u5747\u8861\u7684\u6d41\u91cf\u8fdb\u884cNAT\u8f6c\u6362\uff0c\u7136\u540e\u8f6c\u53d1\u81f3\u540e\u7aef\u670d\u52a1\u5668\u8282\u70b9\uff0c\u5e76\u8bb0\u5f55\u8fd9\u4e2aTCP\u6216\u8005UDP\u7684\u6d41\u91cf\u662f\u7531\u54ea\u53f0\u540e\u7aef\u670d\u52a1\u5668\u5904\u7406\u7684\uff0c\u540e\u7eed\u8fd9\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u6d41\u91cf\u90fd\u540c\u6837\u8f6c\u53d1\u5230\u540c\u4e00\u53f0\u670d\u52a1\u5668\u5904\u7406\u3002<\/p>\n\n\n\n<p><strong><em>2.\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u5e94\u7528\u573a\u666f<\/em><\/strong><\/p>\n\n\n\n<p>\u573a\u666f\u4e00\uff1a\u7aef\u53e3\u8f6c\u53d1<br>\u9996\u5148http\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u534f\u8bae\uff0c\u4f46\u662f\u8fd8\u6709\u5f88\u591a\u975ehttp\u7684\u5e94\u7528\uff0c\u5982mysql\u3001redis\u3001ssh\u7b49\uff0c\u53ea\u80fd\u7528\u56db\u5c42\u4ee3\u7406<\/p>\n\n\n\n<p>\u573a\u666f\u4e8c\uff1a\u56db\u5c42\u8d1f\u8f7d\u5747\u8861+\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\uff0c\u5b9e\u73b0\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u3002<br>\u5176\u6b21\u4e03\u5c42\u4ee3\u7406\u9700\u8981CPU\u8fd0\u8f93\uff0c\u6240\u4ee5\u5355\u53f0\u673a\u5668\u5f88\u96be\u505a\u5230\u5f88\u9ad8\u7684\u5904\u7406\u80fd\u529b\uff0c\u56e0\u6b64\u9700\u8981\u5728\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u524d\u9762\u518d\u52a0\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u3002\uff08\u63d0\u9ad8\u7f51\u7ad9\u7684\u8bbf\u95ee\u6548\u7387\uff0c\u5e76\u4fdd\u8bc1\u4e86\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u7684\u9ad8\u53ef\u7528\u6027\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.Nginx\u56db\u5c42\u8d1f\u8f7d\u5747\u8861TCP\u914d\u7f6e<\/h2>\n\n\n\n<p><code>Nginx<\/code>\u56db\u5c42\u4ee3\u7406\u4ec5\u80fd\u5b58\u5728\u4e8e<code>main<\/code>\u6bb5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stream {\r\n        upstream ssh_proxy {\r\n                hash $remote_addr consistent;\r\n                server 192.168.56.103:22;\r\n        }\r\n        upstream mysql_proxy {\r\n                hash $remote_addr consistent;\r\n                server 192.168.56.103:3306;\r\n        }\r\n    server {\r\n        listen 6666;\r\n        proxy_connect_timeout 1s;\r\n        proxy_timeout 300s;\r\n        proxy_pass ssh_proxy;\r\n    }\r\n    server {\r\n        listen 5555;\r\n        proxy_connect_timeout 1s;\r\n        proxy_timeout 300s;\r\n        proxy_pass mysql_proxy;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">3.Nginx\u56db\u5c42\u8d1f\u8f7d\u5747\u8861+\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u573a\u666f\u914d\u7f6e<\/h2>\n\n\n\n<p>\u914d\u7f6e\u6587\u4ef6\u4ec5\u80fd\u5199\u5728main\u5c42\uff0c\u548chttp\u540c\u7ea7\u3002\u56e0\u662f\u56db\u5c42\u4ee3\u7406\uff0c\u89e3\u6790\u4e0d\u5230servername\u6240\u4ee5\u4e0d\u9700\u8981\u914d\u7f6e\u57df\u540d\u3002\u4e03\u5c42\u8d1f\u8f7d\u5b89\u88c5\u5e38\u89c4\u914d\u7f6e\u5373\u53ef\uff0c\u8fd9\u91cc\u4ec5\u5c55\u793a\u56db\u5c42\u8d1f\u8f7d\u7684\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stream {\r\n\tupstream lb {\r\n\t\tserver 10.0.0.6;\r\n\t\tserver 10.0.0.5;\r\n\t}\r\n\tlog_format proxy '$remote_addr &#91;$time_local] '\r\n\t\t\t\t\t'$protocol $status $bytes_sent $bytes_received '\r\n\t\t\t\t\t'$session_time \"$upstream_addr\" '\r\n\t\t\t\t\t'\"$upstream_bytes_sent\" \"upstream_bytes_received\" \"upstream_connect_time\"';\r\n\tserver {\r\n\t\tlisten 80;\r\n\t\tproxy_pass lb;\r\n\t\taccess_log \/var\/log\/nginxproxy.log proxy;\r\n\t}\r\n\r\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">4.Nginx\u56db\u5c42\u8d1f\u8f7d+\u4e03\u5c42\u8d1f\u8f7d\u900f\u4f20\u771f\u5b9eIP<\/h2>\n\n\n\n<p>\u56db\u5c42\u8d1f\u8f7d\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stream {\r\n\tupstream lb {\r\n\t\tserver 10.0.0.6;\r\n\t\tserver 10.0.0.5;\r\n\t}\r\n\tlog_format proxy '$remote_addr &#91;$time_local] '\r\n\t\t\t\t\t'$protocol $status $bytes_sent $bytes_received '\r\n\t\t\t\t\t'$session_time \"$upstream_addr\" '\r\n\t\t\t\t\t'\"$upstream_bytes_sent\" \"upstream_bytes_received\" \"upstream_connect_time\"';\r\n\tserver {\r\n\t\tlisten 80;\r\n\t\tproxy_pass lb;\r\n\t\tproxy_protocol on;###\u52a0\u4e0a\u8fd9\u4e2a\u53c2\u6570\uff0c\u5f00\u542f\u534f\u8bae\r\n\t\taccess_log \/var\/log\/nginxproxy.log proxy;\r\n\t}\r\n\r\n}<\/code><\/pre>\n\n\n\n<p>\u4e03\u5c42\u8d1f\u8f7d\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>###\u4ec5\u9700\u8981\u5728\u7b2c\u4e00\u7ea7\u4e03\u5c42\u4ee3\u7406\u4e0a\u914d\u7f6e\u5373\u53ef\nserver {\r\n    listen 80 proxy_protocol;###\u6dfb\u52a0\u8fd9\u4e2a\u53c2\u6570\uff0c\u652f\u6301\u534f\u8bae\uff0c\u5426\u5219\u4ec5\u5728\u56db\u5c42\u4e0a\u914d\u7f6e\u4f1a\u62a5400\u9519\u8bef\r\n    server_name ip.com;\r\n    set_real_ip_from 172.16.1.0\/24;###\u6dfb\u52a0\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u524d\u9762\u7ecf\u8fc7\u7684\u4ee3\u7406IP\u5730\u5740\n\r    real_ip_header proxy_protocol;###\u5c06proxy_protocol\u83b7\u53d6\u7684IP\u5730\u5740\u590d\u5236\u7ed9$remote_addr\n    location \/ {\r\n        proxy_pass http:\/\/10.0.0.6;\r\n        proxy_http_version 1.1;\r\n        proxy_set_header Host $http_host;\r\n        proxy_set_header X-Real-IP $remote_addr;\r\n        proxy_set_header X-Forwarded-For $proxy_protocol_addr;\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/654"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=654"}],"version-history":[{"count":1,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/654\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/654\/revisions\/655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}