{"id":205468,"date":"2020-11-25T08:38:26","date_gmt":"2020-11-25T00:38:26","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=205468"},"modified":"2020-11-16T10:39:09","modified_gmt":"2020-11-16T02:39:09","slug":"nginx-defense-ddos","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-defense-ddos.html","title":{"rendered":"NGINX\u9650\u5236\u8fde\u63a5\u7684\u5b9e\u8df5 (Defense DDOS)"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5206\u5e03\u5f0f\u62d2\u7edd\u670d\u52a1\u653b\u51fb(\u82f1\u6587\u610f\u601d\u662fDistributed Denial of Service\uff0c\u7b80\u79f0DDoS)\u662f\u6307\u5904\u4e8e\u4e0d\u540c\u4f4d\u7f6e\u7684\u591a\u4e2a\u653b\u51fb\u8005\u540c\u65f6\u5411\u4e00\u4e2a\u6216\u6570\u4e2a\u76ee\u6807\u53d1\u52a8\u653b\u51fb\uff0c\u6216\u8005\u4e00\u4e2a\u653b\u51fb\u8005\u63a7\u5236\u4e86\u4f4d\u4e8e\u4e0d\u540c\u4f4d\u7f6e\u7684\u591a\u53f0\u673a\u5668\u5e76\u5229\u7528\u8fd9\u4e9b\u673a\u5668\u5bf9\u53d7\u5bb3\u8005\u540c\u65f6\u5b9e\u65bd\u653b\u51fb\u3002\u7531\u4e8e\u653b\u51fb\u7684\u53d1\u51fa\u70b9\u662f\u5206\u5e03\u5728\u4e0d\u540c\u5730\u65b9\u7684\uff0c\u8fd9\u7c7b\u653b\u51fb\u79f0\u4e3a\u5206\u5e03\u5f0f\u62d2\u7edd\u670d\u52a1\u653b\u51fb\uff0c\u5176\u4e2d\u7684\u653b\u51fb\u8005\u53ef\u4ee5\u6709\u591a\u4e2a\u3002NGINX: \u9650\u5236\u8fde\u63a5\u7684\u5b9e\u8df5 (Defense DDOS) <\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5173\u4e8e\u9650\u5236\u7528\u6237\u8fde\u63a5\uff0cNginx \u63d0\u4f9b\u7684\u6a21\u5757\uff1a\u00a0[ ngx_http_limit_req_module ]<\/a>\u00a0[ ngx_http_limit_conn_module ]<\/a>\u00a0, \u8fd8\u6709 stream \u6a21\u5757\u4e5f\u5305\u542b\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c \u672c\u6587\u53ea\u8bf4\u660e ngx_http_limit_req_module \u7684\u4f7f\u7528.<\/p>\n

ngx_http_limit_req_module \u914d\u7f6e <\/strong><\/span><\/div>\n
http {\r\n    limit_req_zone $binary_remote_addr zone=one:10<\/span>m rate=10<\/span>r\/s;\r\n\r\n    ...\r\n\r\n    server {\r\n\r\n        ...\r\n        limit_req zone=one burst=5<\/span>;\r\n\r\n        location \/search\/ {\r\n            limit_req zone=one burst=5<\/span>;\r\n        }\r\n        ...\r\n}\r\n<\/code><\/pre>\n

 <\/p>\n

\u53c2\u6570\u8bf4\u660e<\/strong><\/p>\n

$binary_remote_addr<\/code>\u00a0: \u4e8c\u8fdb\u5236\u8fdc\u7a0b\u5730\u5740<\/p>\n

zone=one:10m<\/code>\u00a0: \u5b9a\u4e49zone\u540d\u5b57\u53ebone\uff0c\u5e76\u4e3a\u8fd9\u4e2azone\u5206\u914d10M\u5185\u5b58\uff0c\u7528\u6765\u5b58\u50a8\u4f1a\u8bdd\uff08\u4e8c\u8fdb\u5236\u8fdc\u7a0b\u5730\u5740\uff09\uff0c1m\u5185\u5b58\u53ef\u4ee5\u4fdd\u5b5816000\u4f1a\u8bdd<\/p>\n

rate=10r\/s<\/code>\u00a0: \u9650\u5236\u9891\u7387\u4e3a\u6bcf\u79d210\u4e2a\u8bf7\u6c42<\/p>\n

burst=5<\/code>\u00a0: \u5141\u8bb8\u8d85\u8fc7\u9891\u7387\u9650\u5236\u7684\u8bf7\u6c42\u6570\u4e0d\u591a\u4e8e5\u4e2a\uff0c\u5047\u8bbe1\u30012\u30013\u30014\u79d2\u8bf7\u6c42\u4e3a\u6bcf\u79d29\u4e2a\uff0c\u90a3\u4e48\u7b2c5\u79d2\u5185\u8bf7\u6c4215\u4e2a\u662f\u5141\u8bb8\u7684\uff0c\u53cd\u4e4b\uff0c\u5982\u679c\u7b2c\u4e00\u79d2\u5185\u8bf7\u6c4215\u4e2a\uff0c\u4f1a\u5c065\u4e2a\u8bf7\u6c42\u653e\u5230\u7b2c\u4e8c\u79d2\uff0c\u7b2c\u4e8c\u79d2\u5185\u8d85\u8fc710\u7684\u8bf7\u6c42\u76f4\u63a5503\uff0c\u7c7b\u4f3c\u591a\u79d2\u5185\u5e73\u5747\u901f\u7387\u9650\u5236\u3002<\/p>\n

\u9047\u5230\u7684\u95ee\u9898 <\/strong><\/span><\/div>\n

\u62df\u5b9a\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff1a<\/p>\n

\u4e00\u6b21\u6b63\u5e38\u8bf7\u6c42\u8fd4\u56de 50 \u4e2a HTML \u9875\u9762\uff0c\u603b\u5171\u7528\u65f610s\u3002 \u5982\u679c\u5728\u4e00\u4e2a500\u4eba\u7684\u573a\u6240\u4e2d\uff0c\u5355\u4e2a\u5ba2\u6237\u7aef IP \u53d1\u51fa\u7684\u6700\u5927\u8bf7\u6c42\u6570\u4f1a\u662f\uff1a 500\/10 * 500 = 2500.<\/p>\n

\u6240\u4ee5\uff0c\u4e3a\u4e86\u4e0d\u5f71\u54cd\u6b63\u5e38\u7528\u6237\u7684\u8bbf\u95ee\uff0c\u6211\u4f3c\u4e4e\u53ea\u80fd\u505a\u5982\u4e0b\u8bbe\u7f6e\uff1a rate=2500r\/s. \u663e\u7136\uff0c\u8fd9\u6837\u7684\u8bbe\u7f6e\u662f\u4e0d\u5408\u7406\u7684\u3002<\/p>\n

\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898 ?<\/strong><\/p>\n

\u5982\u679c\u4e0d\u662f\u4ee5 IP \u4f5c\u4e3a\u8bb0\u5f55\u7684\u6807\u51c6,\u800c\u662f\u8bf7\u6c42\u7684 URI, \u90a3\u4e48\u95ee\u9898\u5c31\u53ef\u4ee5\u89e3\u51b3!<\/p>\n

\u5728\u00a0limite_req_zone key zone=name:size rate=rate<\/code>\u00a0\u4e2d\u7684 key \u8bbe\u5b9a\u4e3a\uff1a\u00a0$remote_addr$uri<\/code>\u00a0, \u5982\u4e0b\uff1a<\/p>\n

limit_req_zone \"$remote_addr$uri\"<\/span> zone=one:10<\/span>m rate=1<\/span>r\/s;\r\n<\/code><\/pre>\n

 <\/p>\n

ab\u6d4b\u8bd5<\/strong><\/p>\n

ab -n 1000 -c 100 http:\/\/xx.xx.xx.xx\/uri\r\n<\/code><\/pre>\n

 <\/p>\n

ps: \u8fd9\u4e2a\u89e3\u51b3\u65b9\u5f0f\u662f\u4e00\u4e2a\u840c\u840c\u54d2\u7684\u540c\u4e8b\u60f3\u51fa\u6765\u7684.<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5173\u4e8e\u9650\u5236\u7528\u6237\u8fde\u63a5\uff0cNginx \u63d0\u4f9b\u7684\u6a21\u5757\uff1a\u00a0[ ngx_http_limit_req_module ]\u00a0[ n […]<\/p>\n","protected":false},"author":643,"featured_media":205471,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-205468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=205468"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205468\/revisions"}],"predecessor-version":[{"id":205470,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205468\/revisions\/205470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/205471"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=205468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=205468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=205468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}