{"id":618,"date":"2024-08-20T14:00:24","date_gmt":"2024-08-20T06:00:24","guid":{"rendered":"https:\/\/thereisno.top:4430\/?p=618"},"modified":"2024-08-20T14:00:25","modified_gmt":"2024-08-20T06:00:25","slug":"nginx-streamtcp-udp%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=618","title":{"rendered":"Nginx\u00a0Stream(TCP\/UDP)\u8d1f\u8f7d\u5747\u8861"},"content":{"rendered":"\n<p>Nginx \u7684 TCP\/UDP \u8d1f\u8f7d\u5747\u8861\u662f\u5e94\u7528 Stream \u4ee3\u7406\u6a21\u5757\uff08ngx_stream_proxy_module\uff09\u548c Stream \u4e0a\u6e38\u6a21\u5757\uff08ngx_stream_upstream_module\uff09\u5b9e\u73b0\u7684\u3002Nginx \u7684 TCP \u8d1f\u8f7d\u5747\u8861\u4e0e LVS \u90fd\u662f\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u7684\u5e94\u7528\uff0c\u6240\u4e0d\u540c\u7684\u662f\uff0cLVS \u662f\u88ab\u7f6e\u4e8e Linux \u5185\u6838\u4e2d\u7684\uff0c\u800c Nginx \u662f\u8fd0\u884c\u4e8e\u7528\u6237\u5c42\u7684\uff0c\u57fa\u4e8e Nginx \u7684 TCP \u8d1f\u8f7d\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u7528\u6237\u8bbf\u95ee\u7ba1\u7406\u548c\u63a7\u5236\u3002<\/p>\n\n\n\n<p><strong>1<\/strong><strong>\u3001<\/strong><strong>TCP\/UDP <\/strong><strong>\u8d1f\u8f7d\u5747\u8861<\/strong><\/p>\n\n\n\n<p>Nginx \u7684 Stream \u4e0a\u6e38\u6a21\u5757\u652f\u6301\u4e0e Nginx HTTP \u4e0a\u6e38\u6a21\u5757\u4e00\u81f4\u7684\u8f6e\u8be2\uff08Round Robin\uff09\u3001\u54c8\u5e0c\uff08Hash\uff09\u53ca\u6700\u5c11\u8fde\u63a5\u6570\uff08least_conn\uff09\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u3002Nginx \u9ed8\u8ba4\u4f7f\u7528\u8f6e\u8be2\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u914d\u7f6e\u6837\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>stream {<br>&nbsp;&nbsp;&nbsp; upstream backend {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.145:389 weight=5;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.159:389 weight=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.109:389 weight=1;<br>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>server {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listen 389;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_pass backend;<br>&nbsp;&nbsp;&nbsp; }<br>}<\/p>\n\n\n\n<p>\u54c8\u5e0c\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u53ef\u4ee5\u901a\u8fc7\u5ba2\u6237\u7aef IP\uff08$remote_addr\uff09\u5b9e\u73b0\u7b80\u5355\u7684\u4f1a\u8bdd\u4fdd\u6301\uff0c\u5176\u53ef\u5c06\u540c\u4e00 IP \u5ba2\u6237\u7aef\u59cb\u7ec8\u8f6c\u53d1\u7ed9\u540c\u4e00\u53f0\u540e\u7aef\u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<p>\u914d\u7f6e\u6837\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>stream {<br>&nbsp;&nbsp;&nbsp; upstream backend {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hash $remote_addr;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.145:389 weight=5;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.159:389 weight=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.109:389 weight=1;<br>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>server {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listen 389;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_pass backend;<br>&nbsp;&nbsp;&nbsp; }<br>}<\/p>\n\n\n\n<p>\u54c8\u5e0c\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u901a\u8fc7\u6307\u4ee4\u53c2\u6570 consistent \u8bbe\u5b9a\u662f\u5426\u5f00\u542f\u4e00\u81f4\u6027\u54c8\u5e0c\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u3002Nginx \u7684\u4e00\u81f4\u6027\u54c8\u5e0c\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u662f\u91c7\u7528 Ketama \u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\uff0c\u5f53\u540e\u7aef\u670d\u52a1\u5668\u7ec4\u4e2d\u7684\u670d\u52a1\u5668\u6570\u91cf\u53d8\u5316\u65f6\uff0c\u53ea\u4f1a\u5f71\u54cd\u5c11\u90e8\u5206\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>\u914d\u7f6e\u6837\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>stream {<br>&nbsp;&nbsp;&nbsp; upstream backend {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hash $remote_addr consistent;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.145:389 weight=5;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.159:389 weight=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.109:389 weight=1;<br>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>server {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listen 389;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_pass backend;<br>&nbsp;&nbsp;&nbsp; }<br>}<\/p>\n\n\n\n<p>\u6700\u5c11\u8fde\u63a5\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u53ef\u4ee5\u5728\u540e\u7aef\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u6027\u80fd\u4e0d\u5747\u65f6\uff0c\u5728\u8003\u8651\u4e0a\u6e38\u670d\u52a1\u5668\u7ec4\u4e2d\u5404\u670d\u52a1\u5668\u6743\u91cd\u7684\u524d\u63d0\u4e0b\uff0c\u5c06\u5ba2\u6237\u7aef\u8fde\u63a5\u5206\u914d\u7ed9\u6d3b\u8dc3\u8fde\u63a5\u6700\u5c11\u7684\u88ab\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u4ece\u800c\u6709\u6548\u63d0\u9ad8\u5904\u7406\u6027\u80fd\u9ad8\u7684\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u7684\u4f7f\u7528\u7387\u3002<\/p>\n\n\n\n<p>\u914d\u7f6e\u6837\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>stream {<br>&nbsp;&nbsp;&nbsp; upstream backend {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; least_conn;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.145:389 weight=5;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.159:389 weight=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.109:389 weight=1;<br>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>server {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listen 389;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_pass backend;<br>&nbsp;&nbsp;&nbsp; }<br>}<\/p>\n\n\n\n<p><strong>2<\/strong><strong>\u3001<\/strong><strong>TCP\/UDP <\/strong><strong>\u8d1f\u8f7d\u5747\u8861\u7684\u5bb9\u9519\u673a\u5236<\/strong><\/p>\n\n\n\n<p>Nginx \u7684 TCP\/UDP \u8d1f\u8f7d\u5747\u8861\u5728\u8fde\u63a5\u5206\u914d\u65f6\u4e5f\u652f\u6301\u88ab\u52a8\u5065\u5eb7\u68c0\u6d4b\u6a21\u5f0f\uff0c\u5982\u679c\u4e0e\u540e\u7aef\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5\u5931\u8d25\uff0c\u5e76\u5728 fail_timeout \u53c2\u6570\u7684\u65f6\u95f4\u5185\u8fde\u7eed\u8d85\u8fc7 max_fails \u53c2\u6570\u8bbe\u7f6e\u7684\u6b21\u6570\uff0cNginx \u5c31\u4f1a\u5c06\u8be5\u670d\u52a1\u5668\u7f6e\u4e3a\u4e0d\u53ef\u7528\u72b6\u6001\uff0c\u5e76\u4e14\u5728 fail_timeout \u53c2\u6570\u7684\u65f6\u95f4\u5185\u4e0d\u518d\u7ed9\u8be5\u670d\u52a1\u5668\u5206\u914d\u8fde\u63a5\u3002\u5f53 fail_timeout \u53c2\u6570\u7684\u65f6\u95f4\u7ed3\u675f\u65f6\u5c06\u5c1d\u8bd5\u5206\u914d\u8fde\u63a5\u68c0\u6d4b\u8be5\u670d\u52a1\u5668\u662f\u5426\u6062\u590d\uff0c\u5982\u679c\u53ef\u4ee5\u5efa\u7acb\u8fde\u63a5\uff0c\u5219\u5224\u5b9a\u4e3a\u6062\u590d\u3002<\/p>\n\n\n\n<p>\u914d\u7f6e\u6837\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>stream {<br>&nbsp;&nbsp;&nbsp; upstream backend {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # 10s\u5185\u51fa\u73b03\u6b21\u9519\u8bef\uff0c\u8be5\u670d\u52a1\u5668\u5c06\u88ab\u7194\u65ad10s<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.154:8080 max_fails=3 fail_timeout=10s;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.109:8080 max_fails=3 fail_timeout=10s;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.108:8080 max_fails=3 fail_timeout=10s;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server 192.168.2.107:8080 max_fails=3 fail_timeout=10s;<br>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>server {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_connect_timeout 5s;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # \u4e0e\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5s<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_timeout 10s;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # \u83b7\u53d6\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u7684\u54cd\u5e94\u6700\u5927\u8d85\u65f6\u65f6\u95f4\u4e3a10s<\/p>\n\n\n\n<p># \u5f53\u88ab\u4ee3\u7406\u7684\u670d\u52a1\u5668\u8fd4\u56de\u9519\u8bef\u6216\u8d85\u65f6\u65f6\uff0c\u5c06\u672a\u8fd4\u56de\u54cd\u5e94\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42\u4f20\u9012\u7ed9upstream\u4e2d\u7684\u4e0b<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # \u4e00\u4e2a\u670d\u52a1\u5668<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_next_upstream on;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_next_upstream_tries 3;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # \u8f6c\u53d1\u5c1d\u8bd5\u8bf7\u6c42\u6700\u591a3\u6b21<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_next_upstream_timeout 10s;&nbsp;&nbsp;&nbsp; # \u603b\u5c1d\u8bd5\u8d85\u65f6\u65f6\u95f4\u4e3a10s<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_socket_keepalive on;&nbsp; # \u5f00\u542fSO_KEEPALIVE\u9009\u9879\u8fdb\u884c\u5fc3\u8df3\u68c0\u6d4b<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy_pass backend;<br>&nbsp;&nbsp;&nbsp; }<br>}<\/p>\n\n\n\n<p>\u5176\u4e2d\u7684\u53c2\u6570\u53ca\u6307\u4ee4\u8bf4\u660e\u5982\u4e0b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6307\u4ee4\u503c\u53c2\u6570 max_fails \u662f\u6307 10s \u5185 Nginx \u5206\u914d\u7ed9\u5f53\u524d\u670d\u52a1\u5668\u7684\u8fde\u63a5\u5931\u8d25\u6b21\u6570\u7d2f\u52a0\u503c\uff0c\u6bcf 10s \u4f1a\u91cd\u7f6e\u4e3a 0\uff1b<\/li>\n\n\n\n<li>\u6307\u4ee4\u503c\u53c2\u6570 fail_timeout \u65e2\u662f\u5931\u8d25\u8ba1\u6570\u7684\u6700\u5927\u65f6\u95f4\uff0c\u53c8\u662f\u670d\u52a1\u5668\u88ab\u7f6e\u4e3a\u5931\u8d25\u72b6\u6001\u7684\u7194\u65ad\u65f6\u95f4\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u5c06\u518d\u6b21\u88ab\u5206\u914d\u8fde\u63a5\uff1b<\/li>\n\n\n\n<li>\u6307\u4ee4 proxy_connect_timeout \u6216 proxy_timeout \u4e3a\u8d85\u65f6\u72b6\u6001\u65f6\uff0c\u90fd\u4f1a\u89e6\u53d1 proxy_next_upstream \u673a\u5236\uff1b<\/li>\n\n\n\n<li>proxy_next_upstream \u662f Nginx \u4e0b\u63d0\u9ad8\u8fde\u63a5\u6210\u529f\u7387\u7684\u673a\u5236\uff0c\u5f53\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u8fd4\u56de\u9519\u8bef\u6216\u8d85\u65f6\u65f6\uff0c\u5c06\u5c1d\u8bd5\u8f6c\u53d1\u7ed9\u4e0b\u4e00\u4e2a\u53ef\u7528\u7684\u88ab\u4ee3\u7406\u670d\u52a1\u5668\uff1b<\/li>\n\n\n\n<li>\u6307\u4ee4 proxy_next_upstream_tries \u7684\u6307\u4ee4\u503c\u6b21\u6570\u5305\u62ec\u7b2c\u4e00\u6b21\u8f6c\u53d1\u8bf7\u6c42\u7684\u6b21\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>TCP \u8fde\u63a5\u5728\u63a5\u6536\u5230\u5173\u95ed\u8fde\u63a5\u901a\u77e5\u524d\u5c06\u4e00\u76f4\u4fdd\u6301\u8fde\u63a5\uff0c\u5f53 Nginx \u4e0e\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u7684\u4e24\u4e2a\u8fde\u7eed\u6210\u529f\u7684\u8bfb\u6216\u5199\u64cd\u4f5c\u7684\u6700\u5927\u95f4\u9694\u65f6\u95f4\u8d85\u8fc7 proxy_timeout \u6307\u4ee4\u914d\u7f6e\u7684\u65f6\u95f4\u65f6\uff0c\u8fde\u63a5\u5c06\u4f1a\u88ab\u5173\u95ed\u3002\u5728 TCP \u957f\u8fde\u63a5\u7684\u573a\u666f\u4e2d\uff0c\u5e94\u9002\u5f53\u8c03\u6574 proxy_timeout \u7684\u8bbe\u7f6e\uff0c\u540c\u65f6\u5173\u6ce8\u7cfb\u7edf\u5185\u6838 SO_KEEPALIVE \u9009\u9879\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u9632\u6b62\u8fc7\u65e9\u5730\u65ad\u5f00\u8fde\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx \u7684 TCP\/UDP \u8d1f\u8f7d\u5747\u8861\u662f\u5e94\u7528 Stream \u4ee3\u7406\u6a21\u5757\uff08ngx_stream_proxy_mo &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thereisno.top\/?p=618\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cNginx\u00a0Stream(TCP\/UDP)\u8d1f\u8f7d\u5747\u8861\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[64],"class_list":["post-618","post","type-post","status-publish","format-standard","hentry","category-nginx","tag-nginx"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=618"}],"version-history":[{"count":1,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/618\/revisions"}],"predecessor-version":[{"id":619,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/618\/revisions\/619"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}