{"id":63567,"date":"2025-04-29T10:20:00","date_gmt":"2025-04-29T02:20:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63567\/"},"modified":"2025-04-29T10:20:00","modified_gmt":"2025-04-29T02:20:00","slug":"nginx%e9%99%90%e6%b5%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63567\/","title":{"rendered":"nginx\u9650\u6d41\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  nginx \u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u65bd\u9650\u6d41\uff1a\u542f\u7528\u9650\u6d41\u6a21\u5757\uff1aload_module ngx_http_limit_conn_module.so;\u914d\u7f6e\u9650\u6d41\u89c4\u5219\uff1alimit_conn_zone $binary_remote_addr zone=mylimit:10m;\u8bbe\u7f6e\u901f\u7387\u9650\u5236\uff1alimit_conn mylimit 20;\u62d2\u7edd\u8d85\u51fa\u9650\u5236\u7684\u8bf7\u6c42\uff08\u9ed8\u8ba4 503 \u9519\u8bef\uff09\uff1b\u53ef\u9009\uff1a\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\uff1aerror_page 503 \/error-page.html;\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/27\/2024052721003786006.jpg\" class=\"aligncenter\" title=\"nginx\u9650\u6d41\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" alt=\"nginx\u9650\u6d41\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>Nginx \u9650\u6d41\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>Nginx \u662f\u4e00\u6b3e\u6d41\u884c\u7684 Web \u670d\u52a1\u5668\uff0c\u5b83\u63d0\u4f9b\u4e86\u9650\u6d41\u529f\u80fd\uff0c\u53ef\u4ee5\u9650\u5236\u5ba2\u6237\u7aef\u5bf9\u670d\u52a1\u5668\u7684\u8bf7\u6c42\u901f\u7387\uff0c\u9632\u6b62\u670d\u52a1\u5668\u8fc7\u8f7d\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u542f\u7528\u9650\u6d41\u6a21\u5757<\/strong><\/p>\n<p>\u5728 Nginx \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u6307\u4ee4\u542f\u7528\u9650\u6d41\u6a21\u5757\uff1a<\/p>\n<pre>load_module ngx_http_limit_conn_module.so;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u914d\u7f6e\u9650\u6d41\u89c4\u5219<\/strong><\/p>\n<p>\u5728\u670d\u52a1\u5668\u6216\u865a\u62df\u4e3b\u673a\u5757\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u6307\u4ee4\u914d\u7f6e\u9650\u6d41\u89c4\u5219\uff1a<\/p>\n<pre>limit_conn_zone $binary_remote_addr zone=mylimit:10m;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>$binary_remote_addr \u53d8\u91cf\u8868\u793a\u5ba2\u6237\u7aef\u7684\u4e8c\u8fdb\u5236 IP \u5730\u5740\u3002<\/li>\n<li>zone=mylimit \u6307\u5b9a\u9650\u6d41\u533a\u7684\u540d\u79f0\u3002<\/li>\n<li>10m \u6307\u5b9a\u9650\u6d41\u533a\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u4e3a\u5146\u5b57\u8282\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4e3a\u9650\u6d41\u533a\u8bbe\u7f6e\u901f\u7387\u9650\u5236<\/strong><\/p>\n<p>\u4e3a\u521b\u5efa\u7684\u9650\u6d41\u533a\u8bbe\u7f6e\u901f\u7387\u9650\u5236\uff1a<\/p>\n<pre>limit_conn mylimit 20;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>mylimit \u662f\u4e4b\u524d\u521b\u5efa\u7684\u9650\u6d41\u533a\u7684\u540d\u79f0\u3002<\/li>\n<li>20 \u6307\u5b9a\u6bcf\u79d2\u5141\u8bb8\u7684\u6700\u5927\u8fde\u63a5\u6570\u3002<\/li>\n<\/ul>\n<p><strong>4. \u62d2\u7edd\u8d85\u51fa\u9650\u5236\u7684\u8bf7\u6c42<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8d85\u8fc7\u901f\u7387\u9650\u5236\u7684\u8bf7\u6c42\u5c06\u8fd4\u56de HTTP 503\uff08\u670d\u52a1\u4e0d\u53ef\u7528\uff09\u9519\u8bef\u3002<\/p>\n<p><strong>5. \u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8d85\u51fa\u9650\u5236\u65f6\u663e\u793a\u7684\u9519\u8bef\u9875\u9762\uff1a<\/p>\n<pre>error_page 503 \/error-page.html;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u914d\u7f6e\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u914d\u7f6e\uff1a<\/p>\n<pre>load_module ngx_http_limit_conn_module.so;\n\nserver {\n    location \/ {\n        limit_conn_zone $binary_remote_addr zone=mylimit:10m;\n        limit_conn mylimit 20;\n        error_page 503 \/error-page.html;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9644\u6ce8\uff1a<\/strong><\/p>\n<ul>\n<li>\u9650\u6d41\u533a\u7684\u5927\u5c0f\u548c\u901f\u7387\u9650\u5236\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u6574\u3002<\/li>\n<li>Nginx \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u9650\u6d41\u6a21\u5757\uff0c\u5982 ngx_http_limit_req_module\uff0c\u5141\u8bb8\u5bf9\u8bf7\u6c42\u901f\u7387\u8fdb\u884c\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u9650\u6d41\u600e\u4e48\u4f7f\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nginx \u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u65bd\u9650\u6d41\uff1a\u542f\u7528\u9650\u6d41\u6a21\u5757\uff1aload_module ngx_http_limit_conn_module.so;\u914d\u7f6e\u9650\u6d41\u89c4\u5219\uff1alimit_conn_zone $binary_remote_addr zone=mylimit:10m;\u8bbe\u7f6e\u901f\u7387\u9650\u5236\uff1alimit_conn mylimit 20;\u62d2\u7edd\u8d85\u51fa\u9650\u5236\u7684\u8bf7\u6c42\uff08\u9ed8\u8ba4 503 \u9519\u8bef\uff09\uff1b\u53ef\u9009\uff1a\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\uff1aerror_page 503 \/error-page.html; Nginx \u9650\u6d41\u4f7f\u7528\u65b9\u6cd5 \u5f15\u8a00 Nginx \u662f\u4e00\u6b3e\u6d41\u884c\u7684 Web \u670d\u52a1\u5668\uff0c\u5b83\u63d0\u4f9b\u4e86\u9650\u6d41\u529f\u80fd\uff0c\u53ef\u4ee5\u9650\u5236\u5ba2\u6237\u7aef\u5bf9\u670d\u52a1\u5668\u7684\u8bf7\u6c42\u901f\u7387\uff0c\u9632\u6b62\u670d\u52a1\u5668\u8fc7\u8f7d\u3002 \u6b65\u9aa4\uff1a 1. \u542f\u7528\u9650\u6d41\u6a21\u5757 \u5728 Nginx \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u6307\u4ee4\u542f\u7528\u9650\u6d41\u6a21\u5757\uff1a load_module ngx_http_limit_conn_module.so; \u767b\u5f55\u540e\u590d\u5236 2. \u914d\u7f6e\u9650\u6d41\u89c4\u5219 \u5728\u670d\u52a1\u5668\u6216\u865a\u62df\u4e3b\u673a\u5757\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u6307\u4ee4\u914d\u7f6e\u9650\u6d41\u89c4\u5219\uff1a limit_conn_zone $binary_remote_addr zone=mylimit:10m; \u767b\u5f55\u540e\u590d\u5236 $binary_remote_addr \u53d8\u91cf\u8868\u793a\u5ba2\u6237\u7aef\u7684\u4e8c\u8fdb\u5236 IP \u5730\u5740\u3002 zone=mylimit \u6307\u5b9a\u9650\u6d41\u533a\u7684\u540d\u79f0\u3002 10m \u6307\u5b9a\u9650\u6d41\u533a\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u4e3a\u5146\u5b57\u8282\u3002 3. \u4e3a\u9650\u6d41\u533a\u8bbe\u7f6e\u901f\u7387\u9650\u5236 \u4e3a\u521b\u5efa\u7684\u9650\u6d41\u533a\u8bbe\u7f6e\u901f\u7387\u9650\u5236\uff1a limit_conn mylimit 20; \u767b\u5f55\u540e\u590d\u5236 mylimit \u662f\u4e4b\u524d\u521b\u5efa\u7684\u9650\u6d41\u533a\u7684\u540d\u79f0\u3002 20 \u6307\u5b9a\u6bcf\u79d2\u5141\u8bb8\u7684\u6700\u5927\u8fde\u63a5\u6570\u3002 4. \u62d2\u7edd\u8d85\u51fa\u9650\u5236\u7684\u8bf7\u6c42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-63567","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=63567"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63567\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}