{"id":59,"date":"2024-05-30T17:54:45","date_gmt":"2024-05-30T09:54:45","guid":{"rendered":"http:\/\/zhaoyanqi.cn\/?p=59"},"modified":"2024-05-30T18:18:18","modified_gmt":"2024-05-30T10:18:18","slug":"59","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=59","title":{"rendered":"nginx\u8d1f\u8f7d\u5747\u8861\u5c42\u8be6\u89e3"},"content":{"rendered":"\n<p>\u53c2\u8003\u6765\u81ea<a href=\"https:\/\/egonlin.com\/?p=9256\">\u4e8c\u3001Nginx\u56db\u5c42\u4e0e\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861 \u2013 Egon\u6797\u6d77\u5cf0 (egonlin.com)<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"MsyCU\">\u4e00\u3001\u4ec0\u4e48\u662f\u56db\u5c42\u4e0e\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861<\/h1>\n\n\n\n<p id=\"u78137035\">\u5224\u5b9a\u6807\u51c6\uff1aosi\u4e03\u5c42\u534f\u8bae\uff0c\u80fd\u89e3\u6790\u5230\u54ea\u4e00\u5c42\u4f60\u5c31\u54ea\u4e00\u5c42\u7684\u8bbe\u5907<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/zhaoyanqi.cn\/wp-content\/uploads\/2024\/05\/image-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1017\" height=\"545\" data-id=\"70\" data-original=\"https:\/\/zhaoyanqi.cn\/wp-content\/uploads\/2024\/05\/image-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-70\"  sizes=\"auto, (max-width: 1017px) 100vw, 1017px\" \/><\/div><\/figure>\n<\/figure>\n\n\n\n<p id=\"ua4eebe23\">\u4e0b\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<p id=\"u714abeff\">1\u3001\u5ba2\u6237\u7aef\u4ea7\u751f\u4e00\u4e2a\u6570\u636e\u5305\uff0c\u5c31\u597d\u6bd4\u662f\u4e00\u4e2a\u5feb\u9012\u5305\uff0c\u5c42\u5c42\u5305\u88f9\u4e03\u5c42<\/p>\n\n\n\n<p id=\"u91cb4444\">2\u3001\u56db\u5c42\uff1a\u89e3\u6790\u5230\u56db\u5c42\uff0c\u770b\u5230\u7684\u662ftcp\u6216udp\u534f\u8bae\uff0c\u7136\u540e\u57fa\u4e8eip+port\u8fdb\u884c\u8f6c\u53d1<\/p>\n\n\n\n<p id=\"u7c5a1ba8\">3\u3001\u4e03\u5c42\uff1a\u89e3\u6790\u5230\u4e03\u5c42\uff0c\u770b\u5230\u7684\u662f\u4e03\u5c42\u534f\u8bae\uff0c\u4f8b\u5982http\u534f\u8bae\uff0c\u7136\u540eurl\u5730\u5740\u8fdb\u884c\u8f6c\u53d1<\/p>\n\n\n\n<p id=\"uc280f015\">\u5f3a\u8c03\uff1a<\/p>\n\n\n\n<p id=\"ubd749521\">1\u3001\u4e00\u4e2a\u5b8c\u6574\u7684\u5305\uff0c\u5305\u542b\u4e86\u4e03\u5c42\uff0c\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5feb\u9012\u88ab\u5c42\u5c42\u5305\u88f9\u4e86\u4e03\u5c42<\/p>\n\n\n\n<p id=\"u812b0e87\">2\u3001\u4e3a\u4f55\u8981\u5305\u8fd9\u4e48\u591a\u5c42\uff1f<\/p>\n\n\n\n<p id=\"ub61b6a44\">\u4e3a\u4e86\u8ba9\u6cbf\u9014\u5e2e\u5fd9\u8f6c\u53d1\u7684\u8bbe\u5907\u90fd\u80fd\u77e5\u9053\u4e0b\u4e00\u6b65\u8be5\u8f6c\u53d1\u7ed9\u8c01<\/p>\n\n\n\n<p id=\"u75d92ee0\">3\u3001\u7f51\u7edc\u8bbe\u5907\u5728\u89e3\u6790\u5305\u7684\u65f6\u5019\uff0c\u4f1a\u8bfb\u53d6\u67d0\u4e00\u5c42\u7684\u5185\u5bb9\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4fee\u6539<\/p>\n\n\n\n<p id=\"u654b3714\">\u4f46\u662f\u8be5\u6709\u51e0\u5c42\u8fd8\u662f\u51e0\u5c42<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"izHk0\">\u4e8c\u3001\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u7684\u5e94\u7528\u573a\u666f<\/h1>\n\n\n\n<p id=\"u867217e6\">\u7528\u5728\u8981\u57fa\u4e8eip+port\u8f6c\u53d1\u7684\u573a\u666f<\/p>\n\n\n\n<p id=\"ua9ab257b\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p id=\"uac3e7a5e\">1\u3001\u4ee3\u7406MySQL\u3001redis<\/p>\n\n\n\n<p id=\"u7778cf16\">2\u3001\u56db\u5c42\u4ee3\u7406\u4e03\u5c42\u6765\u653e\u5927\u5e76\u53d1<\/p>\n\n\n\n<p id=\"u14c2628d\">\u8865\u5145\uff1a<\/p>\n\n\n\n<p id=\"ua155eff1\">tcp\uff1a\u6d41\u5f0f\u534f\u8bae&#8211;\u300bstream<\/p>\n\n\n\n<p id=\"u43b8082a\">udp\uff1a\u6570\u636e\u62a5\u534f\u8bae<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"Zwxhs\">\u4e09\u3001\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u7684\u5e94\u7528\u573a\u666f<\/h1>\n\n\n\n<p id=\"u8b1d0c68\">\u7528\u5728\u8981\u57fa\u4e8e\u4e03\u5c42\u534f\u8bae\u8f6c\u53d1\u7684\u573a\u666f<\/p>\n\n\n\n<p id=\"u2bea7025\">\u4f8b\u5982BS\u67b6\u6784\u7684\u8f6f\u4ef6\uff0c\u5e94\u7528\u5c42\u90fd\u662fhttp\u534f\u8bae<\/p>\n\n\n\n<p id=\"u85f0a897\">\u90a3\u4ee3\u7406\u591a\u53f0web\u670d\u52a1\u5668\u5c31\u9700\u8981\u7528\u5230\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"NTkmG\">\u56db\u3001\u8d1f\u8f7d\u5747\u8861\u7684\u8f6f\u786c\u4ecb\u7ecd<\/h1>\n\n\n\n<p id=\"u0417f9a5\">\u56db\u5c42\u53ef\u9009\u65b9\u6848<\/p>\n\n\n\n<p id=\"ubccd7413\">1\u3001F5\uff08\u652f\u63014\u5c42\u30017\u5c42\uff09<\/p>\n\n\n\n<p id=\"ue1a12399\">2\u3001LVS\uff08\u652f\u63014\u5c42\uff0c\u582a\u6bd4F5\uff09<\/p>\n\n\n\n<p id=\"u3adc852e\">\u4e03\u5c42\u53ef\u9009\u65b9\u6848<\/p>\n\n\n\n<p id=\"u25df9621\">3\u3001haproxy\uff084\u5c42\u30017\u5c42\uff09<\/p>\n\n\n\n<p id=\"u759a75aa\">4\u3001nginx\uff084\u5c42\u30017\u5c42\u3001web\u670d\u52a1\uff09<\/p>\n\n\n\n<p id=\"u7899eb6d\">.\/configure &#8211;prefix=\/usr\/local\/nginx &#8211;with-openssl &#8211;with-stream # \u542f\u7528\/\u652f\u6301stream\u6a21\u5757<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"uaRA3\">\u5b9e\u9a8c1\uff1a\u56db\u5c42\u4ee3\u7406\u6570\u636e\u5e93\uff08\u4e0d\u5b8c\u6574\u7684\u65b9\u6848\uff09<\/h1>\n\n\n\n<p id=\"u2c24a10d\">\u4e00\u3001\u51c6\u5907\u673a\u5668+\u73af\u5883<\/p>\n\n\n\n<p id=\"u3b9f7885\">\u4e09\u53f0\u673a\u5668<\/p>\n\n\n\n<p id=\"udba8dbaa\">\u5173\u95ed\u9632\u706b\u5899\u3001selinux<\/p>\n\n\n\n<p id=\"u8a52a063\">\u914d\u7f6e\u9759\u6001ip<\/p>\n\n\n\n<p id=\"u398ba69d\">\u65f6\u95f4\u540c\u6b65<\/p>\n\n\n\n<p id=\"u5a80212a\">\u4e8c\u3001\u673a\u5668\u89c4\u5212<\/p>\n\n\n\n<p id=\"u8e9ec205\">\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\uff1a192.168.71.112 13306<\/p>\n\n\n\n<p id=\"ua0ffbf12\">\u6570\u636e\u5e931\uff1a 192.168.71.113 3306<\/p>\n\n\n\n<p id=\"u6cbfc6de\">\u6570\u636e\u5e932\uff1a 192.168.71.116 3306<\/p>\n\n\n\n<p id=\"u503ecff6\">\u4e09\u3001\u5148\u90e8\u7f72\u597d\u6570\u636e\u5e93<\/p>\n\n\n\n<p id=\"u9c9ab22a\">\u51c6\u5907\u597d\u6570\u636e\u5e931\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install mariadb* -y\n\nsystemctl start mariadb\nsystemctl enable mariadb\n\n\u521d\u59cb\u5316\u6570\u636e\uff1axxx.sql\n# \u793a\u4f8b\uff1a\u975e\u4ea4\u4e92\u65b9\u5f0f\u6267\u884csql\n# mysql -uroot --password='' -e \"show databases;\"\n\n# \u5bfc\u5165sql\n# mysql -uroot --password='' &lt; xxx.sql\ncreate database testDB;\ncreate table testDB.messages(id int(11),text varchar(15));\nselect * from testDB.messages;\n\n\u51c6\u5907\u4e00\u4e2a\u8fdc\u7a0b\u8d26\u53f7\ngrant all on testDB.* to 'egon'@'%' identified by '123';\nflush privileges;\n\n\u51c6\u5907\u6570\u636e\u5e932\uff1a\n\u540c\u4e0a\u4e00\u6a21\u4e00\u6837\u7684\u64cd\u4f5c<\/code><\/pre>\n\n\n\n<p>\u56db\u3001\u51c6\u5907\u56db\u5c42\u8d1f\u8f7d\u5747\u8861<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install nginx -y\n\n# nginx -V \u9a8c\u8bc1\u662f\u5426\u5f00\u542f\u4e86stream\u652f\u6301\uff0c--with-stream=dyamic\n\nyum install -y nginx-mod-stream \n\n# vim \/etc\/nginx\/nginx.conf\nworker_processes 4;\nworker_rlimit_nofile 40000;\ninclude \/usr\/share\/nginx\/modules\/*.conf;\n\n\nevents {\nworker_connections 8192;\n}\n\nstream {\nupstream my_servers {\nserver 192.168.10.11:8080 max_fails=3 fail_timeout=5s;\nserver 192.168.10.12:8080 max_fails=3 fail_timeout=5s;\nserver 192.168.10.13:8080 max_fails=3 fail_timeout=5s;\n}\nserver {\nlisten 80;\n# proxy_pass\u540e\u8fd8\u662f\u5efa\u8bae\u8ddf\u4e00\u4e2aupstream\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u53ea\u8ddf\u4e00\u4e2aip:port\u53ea\u4ee3\u7406\u4e00\u53f0\u673a\u5668\uff08\u5982proxy_pass 192.168.10.14:8080 \u8fd9\u5c31\u53ea\u4ee3\u7406\u4e00\u4e2aip+port\u4e86\uff0c\u5931\u53bb\u4e86\u8d1f\u8f7d\u5747\u8861\u6548\u679c\uff0c\u8d1f\u8f7d\u5747\u8861\u8fd8\u662f\u8981\u642d\u914dupstream\u4e00\u8d77\u7528\u624d\u5bf9\uff09\nproxy_pass my_servers; \n}\n}\n\nsystemctl restart nginx\nsystemctl enable nginx\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"gfs0p\">\u5b9e\u9a8c2\uff1a\u56db\u5c42\u4ee3\u7406\u4e03\u5c42&#8211;\u300b\u653e\u5927\u5e76\u53d1<\/h1>\n\n\n\n<p id=\"u70c2009b\">\u65b9\u6848\uff1a<\/p>\n\n\n\n<p id=\"ucf54e986\">\u56db\u5c42&#8211;\u300bnginx<\/p>\n\n\n\n<p id=\"u5c503899\">\u4e03\u5c42&#8211;\u300bnginx<\/p>\n\n\n\n<p id=\"u04496c69\">\u56db\u5c42\uff1a192.168.71.115 9999<\/p>\n\n\n\n<p id=\"uf25d4c9b\">\u4e03\u5c42\u8d1f\u8f7d\u5747\u886101\uff1a 192.168.71.116 8081<\/p>\n\n\n\n<p id=\"uef7cfee3\">\u4e03\u5c42\u8d1f\u8f7d\u5747\u886102\uff1a 192.168.71.111 8081<\/p>\n\n\n\n<p id=\"u61763588\">web01\uff1a 192.168.71.112 8080<\/p>\n\n\n\n<p id=\"ub26876d8\">web02\uff1a 192.168.71.113 8080<\/p>\n\n\n\n<p id=\"u8f9f3477\">web03\uff1a 192.168.71.114 8080<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Nascj\">\u4e00\u3001\u521d\u59cb\u5316\u73af\u5883<\/h2>\n\n\n\n<p id=\"u5ab2ed30\">\u6240\u6709\u673a\u5668<\/p>\n\n\n\n<p id=\"ue030be46\">\u5173\u95ed\u9632\u706b\u5899\u3001selinux<\/p>\n\n\n\n<p id=\"u2954fceb\">\u914d\u7f6e\u9759\u6001ip<\/p>\n\n\n\n<p id=\"u887393dd\">\u65f6\u95f4\u540c\u6b65<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"j4JBZ\">\u4e8c\u3001\u51c6\u5907web\u670d\u52a1\u5668<\/h2>\n\n\n\n<p id=\"u9301d546\">web\u670d\u52a1 + web\u5e94\u7528\uff08\u524d\u7aef+\u540e\u7aef\uff09<\/p>\n\n\n\n<p id=\"u9d3a1993\">web\u670d\u52a1&#8212;-\u300bnginx<\/p>\n\n\n\n<p id=\"u75b1684e\">web\u5e94\u7528&#8212;-\u300b\u524d\u7aef\uff1a1.txt<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>web03:<\/p>\n\n\n\n<p>yum install nginx -y<br>vim \/etc\/nginx\/nginx.conf # \u6539listen 8080<br>echo &#8220;================web03&#8221; &gt; \/usr\/share\/nginx\/html\/1.txt<br>systemctl restart nginx<br>systemctl enable nginx<\/p>\n\n\n\n<p>web02:<\/p>\n\n\n\n<pre id=\"wKiwO\" class=\"wp-block-code\"><code>yum install nginx -y\nvim \/etc\/nginx\/nginx.conf # \u6539listen 8080\necho \"================web02\" &gt; \/usr\/share\/nginx\/html\/1.txt\nsystemctl restart nginx\nsystemctl enable nginx<\/code><\/pre>\n\n\n\n<p>web01:<\/p>\n\n\n\n<p>yum install nginx -y<br>vim \/etc\/nginx\/nginx.conf # \u6539listen 8080<br>echo &#8220;================web01&#8221; &gt; \/usr\/share\/nginx\/html\/1.txt<br>systemctl restart nginx<br>systemctl enable nginx<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"NErUh\">\u4e09\u3001\u914d\u7f6e\u4e03\u5c42\u4ee3\u7406web\u670d\u52a1\u5668<\/h2>\n\n\n\n<p id=\"u5da4ecc8\">\u4e03\u5c42\u8d1f\u8f7d\u5747\u886102<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install nginx -y\n\n&#91;root@lb01 ~]# vim \/etc\/nginx\/nginx.conf\nevents {\nworker_connections 1024;\n}\nhttp {\nupstream web_app_servers {\nserver 192.168.1.103:8080 weight=1;\nserver 192.168.1.104:8080 weight=1;\nserver 192.168.1.105:8080 weight=1;\n}\nserver {\nlisten 8081;\nlocation \/ {\nproxy_pass http:\/\/web_app_servers;\n}\n}\n}\n\nsystemctl restart nginx &amp;&amp; systemctl enable nginx\n\n<\/code><\/pre>\n\n\n\n<p id=\"u5577573b\">\u4e03\u5c42\u8d1f\u8f7d\u5747\u886101<\/p>\n\n\n\n<p id=\"u96cf28f2\">\u4e0e\u4e0a\u9762\u5b8c\u5168\u4e00\u81f4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"HQp0Z\">\u56db\u3001\u914d\u7f6e\u56db\u5c42\u4ee3\u7406<\/h2>\n\n\n\n<pre id=\"kSdCU\" class=\"wp-block-code\"><code>yum install nginx -y\n# nginx -V \u9a8c\u8bc1\u662f\u5426\u5f00\u542f\u4e86stream\u652f\u6301\uff0c--with-stream=dyamic\nyum install -y nginx-mod-stream \n# vim \/etc\/nginx\/nginx.conf\nworker_processes 4;\nworker_rlimit_nofile 40000;\ninclude \/usr\/share\/nginx\/modules\/*.conf;\n\nevents {\nworker_connections 8192;\n}\n\nstream {\nupstream my_servers {\nserver 192.168.10.11:8080 max_fails=3 fail_timeout=5s;\nserver 192.168.10.12:8080 max_fails=3 fail_timeout=5s;\nserver 192.168.10.13:8080 max_fails=3 fail_timeout=5s;\n}\nserver {\nlisten 80;\n# proxy_pass\u540e\u8fd8\u662f\u5efa\u8bae\u8ddf\u4e00\u4e2aupstream\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u53ea\u8ddf\u4e00\u4e2aip:port\u53ea\u4ee3\u7406\u4e00\u53f0\u673a\u5668\uff08\u5982proxy_pass 192.168.10.14:8080 \u8fd9\u5c31\u53ea\u4ee3\u7406\u4e00\u4e2aip+port\u4e86\uff0c\u5931\u53bb\u4e86\u8d1f\u8f7d\u5747\u8861\u6548\u679c\uff0c\u8d1f\u8f7d\u5747\u8861\u8fd8\u662f\u8981\u642d\u914dupstream\u4e00\u8d77\u7528\u624d\u5bf9\uff09\nproxy_pass my_servers; \n}\n}\n\nsystemctl restart nginx\nsystemctl enable nginx\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ZR2ds\">\u4e94\u3001\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u6dfb\u52a0\u65e5\u5fd7<\/h2>\n\n\n\n<p id=\"u85c26c3f\">\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u662f\u6ca1\u6709access\u7684\u65e5\u5fd7\u7684\uff0c\u56e0\u4e3a\u5728nginx.conf\u7684\u914d\u7f6e\u4e2d\uff0caccess\u7684\u65e5\u5fd7\u683c\u5f0f\u662f\u914d\u7f6e\u5728http\u4e0b\u7684\uff0c\u800c\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e\u662f\u5728http\u4ee5\u5916\u7684\uff1b\u5982\u679c\u9700\u8981\u65e5\u5fd7\u5219\u9700\u8981\u914d\u7f6e\u5728stream\u4e0b\u9762\u3002<\/p>\n\n\n\n<p id=\"ufe9551b0\">Nginx \u7684\u6d41\u6a21\u5757\u5728\u5904\u7406 TCP \u6d41\u91cf\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5728\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u5199\u5165\u4e00\u6761\u65e5\u5fd7\u3002\u76f8\u53cd\uff0c\u5b83\u5728 TCP \u8fde\u63a5\u7ed3\u675f\u540e\uff08\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u8fde\u63a5\u5173\u95ed\u65f6\uff09\u624d\u4f1a\u8bb0\u5f55\u4e00\u6761\u65e5\u5fd7\u3002<\/p>\n\n\n\n<p id=\"u0c34ea6a\">\u8fd9\u4e0e HTTP \u4e0d\u540c\uff0c\u56e0\u4e3aTCP \u662f\u4e00\u79cd\u57fa\u4e8e\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u6bcf\u4e2a\u8fde\u63a5\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u8bf7\u6c42\u548c\u54cd\u5e94\u3002\u7531\u4e8e Nginx \u7684 stream \u6a21\u5757\u5728\u66f4\u4f4e\u7684\u7f51\u7edc\u5c42\u5904\u7406\u8fd9\u4e9b\u8fde\u63a5\uff0c\u5b83\u5e76\u6ca1\u6709\u4e00\u4e2a\u660e\u786e\u5b9a\u4e49\u7684 &#8220;\u8bf7\u6c42&#8221;\u3002\u6240\u4ee5\uff0c\u5728 TCP \u8fde\u63a5\u751f\u547d\u5468\u671f\u4e2d\uff0c\u53ea\u6709\u5f53\u8fde\u63a5\u5173\u95ed\u65f6\uff0cNginx \u624d\u4f1a\u8bb0\u5f55\u8fde\u63a5\u7684\u603b\u4f53\u4fe1\u606f\u3002\u56e0\u6b64\uff0c\u4f60\u5982\u679c\u4f60\u5728\u8bbf\u95ee\u670d\u52a1\u5668\u65f6\u770b\u4e0d\u5230\u7acb\u5373\u5199\u5165\u65e5\u5fd7\uff0c\u90a3\u662f\u56e0\u4e3a\u4f60\u7684 TCP \u8fde\u63a5\u8fd8\u6ca1\u5173\u95ed\u3002<\/p>\n\n\n\n<p id=\"ud4c14491\">\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6d4f\u89c8\u5668\u53ef\u80fd\u4f1a\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u590d\u7528\u4e00\u4e2a TCP \u8fde\u63a5\uff0c\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\uff0c\u76f4\u5230\u8fde\u63a5\u8d85\u65f6\u6216\u8005\u4e3b\u52a8\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n\n\n\n<p id=\"ub8b0c42a\">\u56e0\u6b64\uff0c\u4f60\u53ef\u80fd\u53ea\u6709\u5728\u8fd9\u4e2a TCP \u8fde\u63a5\u5173\u95ed\u65f6\u624d\u4f1a\u5728\u65e5\u5fd7\u4e2d\u770b\u5230\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p id=\"ua66e848f\">\u5982\u679c\u4f60\u5e0c\u671b\u7acb\u5373\u770b\u5230\u65e5\u5fd7\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5728\u53d1\u9001\u8bf7\u6c42\u540e\u7acb\u5373\u5173\u95ed TCP \u8fde\u63a5\u3002<\/p>\n\n\n\n<p id=\"u7733d089\">\u5982\u679c\u4f60\u4f7f\u7528 curl \u6216\u8005 telnet \u5de5\u5177\u53d1\u9001\u8bf7\u6c42\u7684\u8bdd\uff0c\u4ed6\u4eec\u901a\u5e38\u5728\u8bf7\u6c42\u7ed3\u675f\u540e\u5c31\u4f1a\u7acb\u5373\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@lb ~]# cat \/etc\/nginx\/nginx.conf\nuser nginx;\nworker_processes auto;\nerror_log \/var\/log\/nginx\/error.log;\npid \/run\/nginx.pid;\ninclude \/usr\/share\/nginx\/modules\/*.conf;\nevents {\n    worker_connections 1024;\n}\nstream {\n    log_format  proxy1 '$remote_addr $remote_port - &#91;$time_local] $status $protocol '\n                  '\"$upstream_addr\" \"$upstream_bytes_sent\" \"$upstream_connect_time\"';\n    access_log \/var\/log\/nginx\/proxy1.log proxy1;\n    upstream lb_servers {\n        server 192.168.71.12:9090 max_fails=3 fail_timeout=5s;\n        server 192.168.71.13:9090 max_fails=3 fail_timeout=5s;\n    }\n    server {\n        listen 80;\n        proxy_pass lb_servers;\n    }\n}\n\n#\u6ce8\u610f\u7ed9\u6743\u9650\uff0c\u6211\u4eec\u7684\u542f\u52a8user\u5c31\u662fnginx\nchown -R nginx.nginx \/var\/log\/nginx\/\n\n#\u67e5\u770b\u65e5\u5fd7\ntail -f \/var\/log\/nginx\/proxy1.log \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Nii50\">\u516d\u3001\u540e\u7aef\u8bbf\u95ee\u771f\u5b9eIP\uff0c7\u5c42\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e<\/h2>\n\n\n\n<p id=\"udb8e70f4\">\u4e3a\u4f55\u8ba9\u5ba2\u6237\u7aefweb\u670d\u52a1\u62ff\u5230\u8bbf\u95ee\u8005\u7684\u771f\u5b9eip<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/zhaoyanqi.cn\/wp-content\/uploads\/2024\/05\/image-2-1024x251.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"251\" data-original=\"https:\/\/zhaoyanqi.cn\/wp-content\/uploads\/2024\/05\/image-2-1024x251.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-73\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p id=\"ufb3ca476\">\u9488\u5bf9\u83b7\u53d6\u8bbf\u95ee\u8005\u771f\u5b9eip\u5730\u5740\u8fd9\u4ef6\u4e8b\uff0c\u9700\u8981\u601d\u8003\u4e24\u4ef6\u4e8b<\/p>\n\n\n\n<p id=\"u883bc557\">1\u3001\u5982\u4f55\u624d\u80fd\u628a\u8bbf\u95ee\u8005\u7684\u771f\u5b9eip\u5730\u5740\u900f\u4f20\u7ed9\u540e\u7aefweb\u670d\u52a1<\/p>\n\n\n\n<p id=\"uaf63b594\">\u6700\u8f7b\u91cf\u7ea7\u7684\u65b9\u6848\uff1a\u5c31\u662f\u5728\u534f\u8bae\u91cc\u52a0\uff0c\u5177\u4f53\u70b9\u8bf4\u5982\u679c\u662fhttp\u534f\u8bae\u90a3\u5c31\u53ef\u4ee5\u52a0\u5230\u8bf7\u6c42\u5934\u91cc\u3002\u8fd9\u79cd\u628a\u5185\u5bb9\u653e\u5230\u534f\u8bae\u91cc\u6765\u5b9e\u73b0\u900f\u4f20\u4fe1\u606f\u7684\u601d\u60f3\u5728\u540e\u7eed\u4f60\u505a\u66f4\u9ad8\u7ea7\u7684\u4e00\u4e9b\u67b6\u6784\u6784\u601d\u65f6\u4e5f\u4f1a\u7ecf\u5e38\u7528\u5230<\/p>\n\n\n\n<p id=\"u1eb33861\">2\u3001\u5728\u54ea\u91cc\u914d\u7f6e<\/p>\n\n\n\n<p id=\"u106f83b4\">web\u670d\u52a1\u5668\u4e0a\u4e0d\u9700\u8981\u914d\u7f6e\uff0c\u56e0\u4e3a\u5b83\u662f\u4eab\u53d7\u6210\u679c\u7684\u4eba\uff0c\u9700\u8981\u914d\u7f6e\u7684\u662f\u90a3\u4e9b\u6cbf\u9014\u8f6c\u53d1\u7684\u4ee3\u7406\u4eec\uff08\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u3001\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\uff09\u9700\u8981\u7528\u5230\u2014\u4e2a\u5173\u952e\u53c2\u6570X-Forwarded-For\uff0c<\/p>\n\n\n\n<p id=\"ubfd6dbdb\">\u4e3a\u4e86\u5feb\u901f\u4e86\u89e3X-Forwarded-For\uff0c\u6211\u4eec\u5148\u5f53\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u4e0d\u5b58\u5728\uff0c\u6211\u4eec\u5c31\u8ba9\u7528\u6237\u76f4\u63a5\u8bbf\u95ee\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\uff0c\u90a3\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5982\u4f55\u900f\u4f20\u5ba2\u6237\u7aef\u7684\u771f\u5b9eip\u5462\uff1f\u00b7\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\uff1a\u4f1a\u89e3\u5f00http\u534f\u8bae\u5934\uff0c\u7136\u540e\u91cd\u65b0\u5c01\u5305\u518d\u5411\u540e\u7aef\u8f6c\u53d1http\u534f\u8bae\u7684\u8bf7\u6c42\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u771f\u5b9e\u8bbf\u95ee\u8005ip\u5730\u5740\u5c31\u88ab\u4e22\u5f03\u6389\u4e86\uff0c\u56e0\u4e3a\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u91cd\u65b0\u5c01\u88c5\u4e86\u5305\uff0c\u4e8e\u662f\u4e4e\uff0c\u5305\u9001\u5230web\u670d\u52a1\u5668\u4e0a\u65f6\uff0cweb\u670d\u52a1\u5668\u4e0a\u65e5\u5fd7$remote_addr\u83b7\u53d6\u7684\u5c31\u662f\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u7684ip\u5730\u5740\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u7684\u8bbf\u95ee\u8005\u771f\u5b9eip\u5730\u5740\uff0c\u8fd9\u6574\u5957\u5904\u7406\u903b\u8f91\u5fc5\u7136\u662f\u65e0\u6cd5\u66f4\u6539\u7684\uff0c\u9664\u975e\u4f60\u66f4\u6539\u4e0a\u4e0b\u6e38\u7684\u6e90\u4ee3\u7801\uff0c\u90a3\u6837\u5b50\u5c31\u4fb5\u5165\u592a\u5927\u4e86\u3002\u5982\u4f55\u624d\u80fd\u4e0d\u6539\u53d8\u3001\u4e0d\u7834\u574f\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u62c6\u89e3\u5305\u8f6c\u53d1\u903b\u8f91\u7684\u6e90\u4ee3\u7801\uff0c\u8fd8\u80fd\u5b9e\u73b0\u628a\u8bbf\u95ee\u8005\u7684\u771f\u5b9eip\u5730\u5740\u900f\u4f20\u7ed9\u540e\u7aef\u7684web\u670d\u52a1\u5668\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u505a\uff0c\u5f80\u8bf7\u6c42\u5934\u91cc\u52a0\u4e00\u70b9\u4e1c\u897f\u5c31\u884c\uff1a\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5728\u6536\u5230http\u5305\u91cc\u80af\u5b9a\u6709\u5ba2\u6237\u7aefip\uff0c\u522b\u6254\u3001\u628a\u5b83\u8d4b\u503c\u7ed9\u53e6\u5916\u4e00\u4e2akey\u540d\uff0c\u7136\u540e\u9644\u5728\u8bf7\u6c42\u5934\u91cc\uff0c\u9001\u7ed9\u5ba2\u6237\u7aef\u5c31\u884c\uff0c\u5ba2\u6237\u7aef\u518d\u4ece\u8fd9\u4e2a\u72ec\u7279\u7684key\u540d\u53d6\u51fa\u6765\u5c31\u80fd\u62ff\u5230\u771f\u5b9e\u7684\u8bbf\u95ee\u8005ip\u5730\u5740\u4e86\uff0c\u5982\u6b64\uff0c\u6211\u4eec\u4fbf\u5728\u6ca1\u6709\u6539\u53d8\u4efb\u4f55\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5c31\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u529f\u80fd\u7684\u589e\u52a0\uff0c\u8fd9\u662f\u4e00\u79cd\u601d\u60f3\uff0c\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u9ad8\u7ea7\u601d\u60f3\uff0c\u6211\u613f\u610f\u5c06\u5176\u79f0\u4e4b\u4e3a\u5c4e\u4e0a\u96d5\u82b1\uff0c\u8fd9\u5e76\u975e\u5f00\u73a9\u7b11\uff0c\u4f60\u5e73\u65f6\u5de5\u4f5c\u4e2d\u5c31\u662f\u4f1a\u78b0\u5230\u5f88\u591a\u5f88\u591a\u7684\u5c4e\uff0c\u8fd9\u4e9b\u5c4e\u4e0d\u53ef\u80fd\u63a8\u7ffb\u91cd\u6765\uff0c\u800c\u4e14\u6e90\u4ee3\u7801\u90fd\u4e0d\u80fd\u6539\uff0c\u4e00\u6539\u5c31\u662f\u8fde\u9501\u53cd\u5e94\u7684\u524d\u6e83\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u5c31\u662f\u539f\u6765\u7684\u5c4e\u5c31\u4e0d\u52a8\u4e86\uff0c\u8fd8\u80fd\u5f80\u91cc\u52a0\u70b9\u65b0\u529f\u80fd\u5c31\u662f\u6700\u68d2\u7684\u3002\u5177\u4f53\u914d\u7f6e\u5982\u4e0b<\/p>\n\n\n\n<p id=\"uf91e132c\">7\u5c42\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; \/etc\/nginx\/nginx.conf &lt;&lt; EOF\nuser nginx;\nworker_processes auto;\nerror_log \/var\/log\/nginx\/error.log;\npid \/run\/nginx.pid;\ninclude \/usr\/share\/nginx\/modules\/*.conf;\nevents {\nworker_connections 1024;\n}\nhttp {\nupstream webserver {\n  server 192.168.71.14:8080;\n  server 192.168.71.15:8080;\n  server 192.168.71.16:8080;\n}\nserver {\n    listen       9090;\n    location \/ {\n       proxy_pass  http:\/\/webserver;\n \n       # -------------------\u300b\u53ea\u52a0\u4e0a\u4e0b\u9762\u8fd9\u4e00\u6bb5\u5373\u53ef\n       proxy_set_header X-Real-IP $remote_addr;\n       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n       proxy_set_header Host $http_host;\n       proxy_set_header X-Forwarded-Proto $scheme;\n \n    }\n}\n}\nEOF\n===\n\u89e3\u91ca\uff1a\n1\u3001X-Forwarded-For \u5934\u5b57\u6bb5\uff0c\u5b83\u662f\u4e00\u4e2a\u88ab\u9010\u7ea7\u4ee3\u7406\u670d\u52a1\u5668\u6dfb\u52a0\u7684\u3001\u5305\u542b\u4e86\u6240\u6709\u4e2d\u95f4 IP \u7684\u987a\u5e8f\u5217\u8868\uff0c\n\u5217\u8868\u7684\u6700\u5de6\u4fa7\u662f\u539f\u59cb\u5ba2\u6237\u7aef\u7684 IP\u3002\u5982\u679c\u8bf7\u6c42\u6ca1\u6709\u7ecf\u8fc7\u4efb\u4f55\u4ee3\u7406\uff0c\u8fd9\u4e2a\u53d8\u91cf\u4e3a\u7a7a\u3002\u5982\u679c\u7ecf\u8fc7\u591a\u7ea7\u4ee3\u7406\uff0c\n\u8fd9\u4e2a\u53d8\u91cf\u4f1a\u4f9d\u6b21\u6dfb\u52a0\u4ee3\u7406\u670d\u52a1\u5668\u7684 IP\uff0c\u56e0\u6b64\u8981\u83b7\u53d6\u771f\u5b9e\u7684\u5ba2\u6237\u7aef IP\uff0c\u5e94\u53d6\u8fd9\u4e2a\u5217\u8868\u7684\u7b2c\u4e00\u4e2a IP\u3002\n2\u3001 HTTP \u7684 X-Real-IP \u5934\u5b57\u6bb5\uff0c\u5b83\u88ab\u8bbe\u8ba1\u4e3a\u5355\u4e2a IP \u5730\u5740\uff0c\u4ee3\u8868\u539f\u59cb\u5ba2\u6237\u7aef\u7684 IP\u3002\n\u5f53\u8bf7\u6c42\u901a\u8fc7\u4ee3\u7406\u65f6\uff0c\u4ee3\u7406\u670d\u52a1\u5668\u901a\u5e38\u4f1a\u6dfb\u52a0\u6216\u8005\u8986\u76d6\u8fd9\u4e2a\u5b57\u6bb5\uff1b\n\u4f46\u8bf7\u6ce8\u610f\uff0c\u5e76\u975e\u6240\u6709\u7684\u4ee3\u7406\u670d\u52a1\u5668\u90fd\u4f1a\u8bbe\u7f6e\u8fd9\u4e2a\u5b57\u6bb5\u3002\n\u56e0\u6b64\uff0c\u8981\u6839\u636e\u4f60\u6240\u4f7f\u7528\u7684\u8d1f\u8f7d\u5747\u8861\u5668\u6216\u4ee3\u7406\u670d\u52a1\u5668\u7684\u884c\u4e3a\u6765\u6b63\u786e\u83b7\u53d6\u5ba2\u6237\u7aef\u7684 IP\u3002<\/code><\/pre>\n\n\n\n<p>web\u670d\u52a1\u7aef\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># $http\u4ee3\u8868\u4ecehttp\u8bf7\u6c42\u5934\u91cc\u53d6\n$http_x_forwarded_for  # \u83b7\u53d6X-Forwarded-For\u7684\u503c\n$http_x_real_ip        # \u83b7\u53d6X-Real-IP\u7684\u503c\n=================================\n\u65e5\u5fd7\u914d\u7f6e\u5982\u4e0b\nhttp { # $remote_addr\u83b7\u53d6\u7684\u8fd8\u662f\u8d1f\u8f7d\u5747\u8861\u7684ip\u5730\u5740\u8fd9\u4e00\u70b9\u4e0d\u4f1a\u6539\u53d8\uff0c\u6211\u4eec\u9ed8\u8ba4\u589e\u52a0\u4e86\u4e24\u4e2a\"$http_x_forwarded_for\" \"$http_x_real_ip\"\uff0c\u8fd9\u4e24\u4e2a\u90fd\u53ef\u4ee5\u62ff\u5230\u8bbf\u95ee\u8005\u7684\u771f\u5b9eip\nlog_format  main1  '$remote_addr - $remote_user &#91;$time_local] \"$request\" '\n                  '$status $body_bytes_sent \"$http_referer\" '\n                  '\"$http_user_agent\" \"$http_x_forwarded_for\" \"$http_x_real_ip\"' ;\n}\nserver {\naccess_log \/var\/log\/nginx\/access.log main1;\n\u3002\u3002\u3002\u5176\u4ed6\u90e8\u5206\u90fd\u4e0d\u52a8\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"IuG5y\">\u4e03\u3001\u56db\u5c42\u8d1f\u8f7d\u5747\u8861+\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u900f\u4f20\u771f\u5b9eIP<\/h2>\n\n\n\n<p id=\"u27efc81c\">\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\uff08192.168.71.11\uff09\u2014-&gt;\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\uff08192.168.71.12\uff09\u2014\u4e00&gt;web\u670d\u52a1\u5668\uff08192.168.71.14\u3001192.168.71.15)<\/p>\n\n\n\n<p id=\"uc9950420\">\u8981\u60f3\u4ece\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u900f\u4f20\u771f\u5b9eIP\u5230\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\uff0c\u6211\u4eec\u5148\u8981\u660e\u767d\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u5de5\u4f5c\u5728\u4f20\u8f93\u5c42\uff0c\u53ea\u6709TCP\/UDP\u534f\u8bae\uff0c\u8fd9\u4e24\u79cd\u534f\u8bae\u4e2d\u53ea\u5305\u542b\u6e90IP\u3001\u76ee\u6807IP\u3001\u6e90\u7aef\u53e3\u53f7\u53ca\u76ee\u7684\u7aef\u53e3\u53f7\uff0c\u5e76\u4e0d\u4f1a\u643a\u5e26http\u5934\u4fe1\u606f\uff0c\u8fd9\u6837\u7684\u8bdd\u6211\u4eec\u5c31\u65e0\u6cd5\u5b9a\u4e49X-Forwarded-For\u5b57\u6bb5\u4f20\u9012\u5ba2\u6237\u7aef\u771f\u5b9eIP\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u7528\u6237\u7684\u771f\u5b9eIP\u5c31\u4f1a\u4e22\u5931\uff0c\u6700\u540e\u5bfc\u81f4\u6211\u4eec\u540e\u7aefweb\u8282\u70b9\u83b7\u53d6\u5230\u53ea\u4f1a\u662f\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u7684IP\u3002<\/p>\n\n\n\n<p id=\"ubef714f3\">\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u5728\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u4e0a\u542f\u7528proxy\u534f\u8bae\uff0c\u8be5\u534f\u8bae\u901a\u8fc7\u4e3atcp\u6dfb\u52a0\u4e00\u4e2a\u5f88\u5c0f\u7684\u5934\u4fe1\u606f\uff0c\u6765\u65b9\u4fbf\u7684\u4f20\u9012\u5ba2\u6237\u7aef\u4fe1\u606f\uff08\u534f\u8bae\u6808\u3001\u6e90IP\u3001\u76ee\u7684IP\u3001\u6e90\u7aef\u53e3\u3001\u76ee\u7684\u7aef\u53e3\u7b49\uff09\uff0c\u5176\u672c\u8d28\u662f\u5728\u4e09\u6b21\u63e1\u624b\u7ed3\u675f\u540e\u7531\u4ee3\u7406\u5728\u4f20\u8f93\u4e2d\u63d2\u5165\u4e86\u4e00\u4e2a\u643a\u5e26\u4e86\u539f\u59cb\u8fde\u63a5\u5143\u7ec4\u4fe1\u606f\u7684\u6570\u636e\u5305\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"CQVLB\">4\u5c42\u8d1f\u8f7d\u5747\u8861<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; \/etc\/nginx\/nginx.conf &lt;&lt; EOF\nuser nginx;\nworker_processes auto;\nerror_log \/var\/log\/nginx\/error.log;\npid \/run\/nginx.pid;\ninclude \/usr\/share\/nginx\/modules\/*.conf;\nevents {\nworker_connections 1024;\n}\nstream {\nupstream lb_servers {\n    server 192.168.71.12:9090 max_fails=3 fail_timeout=5s;\n    server 192.168.71.13:9090 max_fails=3 fail_timeout=5s;\n}\nserver {\n    listen 80;\n    proxy_pass lb_servers;\n    proxy_protocol on; # ---------\u300b\u65b0\u589e\u8fd9\u4e00\u6761\uff1a\u4ee3\u8868\u5f00\u542fproxy protocol \u534f\u8bae\n}\n}\nEOF\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>7\u5c42\u8d1f\u8f7d\u5747\u8861<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@lb01 \/etc\/nginx]# cat \/etc\/nginx\/nginx.conf\nevents {\n    worker_connections 1024;\n}\nhttp {\n    upstream web_app_servers {\n        server 192.168.71.112:80 weight=1;\n        server 192.168.71.113:80 weight=1;\n    }\n    server {\n        listen 80 proxy_protocol;   # \u56e0\u4e3a\u4e0a\u6e38\u7684\u56db\u5c42\u5f00\u542f\u4e86proxy\u534f\u8bae\uff0c\u6240\u4ee5\u6b64\u5904\u9700\u8981\u914d\u5408\u542f\u7528proxy_protocol\u7684\u76d1\u542c\n        set_real_ip_from 0.0.0.0\/0; # \u4fe1\u4efb\u6240\u6709\u4e0a\u6e38\u6e90\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\n        real_ip_header proxy_protocol; # \u5c06proxy_protocol\u83b7\u53d6\u7684IP\u66ff\u6362\u539f$remote_addr\u503c\n \n        location \/ {\n            proxy_pass http:\/\/web_app_servers;\n   \n            # \u589e\u52a0\u4e0b\u9762\u4e00\u6bb5\n            proxy_set_header X-Real-IP $remote_addr;\n            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n            proxy_set_header Host $http_host;\n            proxy_set_header X-Forwarded-Proto $scheme;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>web\u670d\u52a1\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http {\n    log_format  main  '\u65e5\u5fd7\u683c\u5f0f\u672b\u5c3e\u589e\u52a0\u4e24\u4e2a\u6bb5 \"$http_x_forwarded_for\" \"$http_x_real_ip\"';\n \n    access_log  \/var\/log\/nginx\/access.log  main;\n \n    server {\n        # \u65b0\u589e\u4e0b\u9762\u4e00\u6bb5\n        set_real_ip_from 0.0.0.0\/0; # \u4fe1\u4efb\u6240\u6709\u4e0a\u6e38\u6e90\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\n        real_ip_header X-Forwarded-For;   # \u5c06X-Forwarded-For\u5b57\u6bb5\u5305\u542b\u7684IP\u66ff\u6362\u539f$remote_addr\u503c\n        #real_ip_header X-Real-IP;        # \u5c06X-Real-IP\u5b57\u6bb5\u5305\u542b\u7684IP\u66ff\u6362\u539f$remote_addr\u503c\n        real_ip_recursive on; \n        \n        ......\n        \n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u6765\u81ea\u4e8c\u3001Nginx\u56db\u5c42\u4e0e\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861 \u2013 Egon\u6797\u6d77\u5cf0 (egonlin.com) \u4e00\u3001\u4ec0\u4e48\u662f\u56db\u5c42\u4e0e\u4e03\u5c42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-6"],"_links":{"self":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":5,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/76"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}