{"id":51153,"date":"2024-10-08T16:28:21","date_gmt":"2024-10-08T08:28:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51153\/"},"modified":"2024-10-08T16:28:21","modified_gmt":"2024-10-08T08:28:21","slug":"nginx-%e5%a6%82%e4%bd%95%e5%81%9a%e9%98%b2-ddos-%e6%94%94%e6%88%aa","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51153\/","title":{"rendered":"nginx \u5982\u4f55\u505a\u9632 DDoS \u6514\u622a"},"content":{"rendered":"<h1 id=\"nginx-%e5%a6%82%e4%bd%95%e5%81%9a%e9%98%b2-ddos-%e6%94%94%e6%88%aa-yEdYlpjwtm\">nginx \u5982\u4f55\u505a\u9632 DDoS \u6514\u622a<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u5206\u6563\u5f0f\u62d2\u7d55\u670d\u52d9\u653b\u64ca\uff08DDoS\uff09\u5df2\u6210\u70ba\u4e00\u7a2e\u5e38\u898b\u7684\u5a01\u8105\u3002\u9019\u985e\u653b\u64ca\u901a\u904e\u5927\u91cf\u7684\u6d41\u91cf\u6df9\u6c92\u76ee\u6a19\u4f3a\u670d\u5668\uff0c\u5c0e\u81f4\u5176\u7121\u6cd5\u6b63\u5e38\u904b\u884c\u3002nginx \u4f5c\u70ba\u4e00\u6b3e\u9ad8\u6548\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u9632\u79a6 DDoS \u653b\u64ca\u7684\u529f\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528 nginx \u4f86\u52a0\u5f37\u4f3a\u670d\u5668\u7684\u5b89\u5168\u6027\uff0c\u62b5\u79a6 DDoS \u653b\u64ca\u3002<\/p>\n<h2 id=\"%e7%90%86%e8%a7%a3-ddos-%e6%94%bb%e6%93%8a-yEdYlpjwtm\">\u7406\u89e3 DDoS \u653b\u64ca<\/h2>\n<p>DDoS \u653b\u64ca\u7684\u57fa\u672c\u539f\u7406\u662f\u5229\u7528\u591a\u500b\u53d7\u611f\u67d3\u7684\u8a2d\u5099\uff08\u901a\u5e38\u7a31\u70ba\u300c\u6bad\u5c4d\u7db2\u7d61\u300d\uff09\u5411\u76ee\u6a19\u4f3a\u670d\u5668\u767c\u9001\u5927\u91cf\u8acb\u6c42\uff0c\u8d85\u904e\u5176\u8655\u7406\u80fd\u529b\u3002\u9019\u4e9b\u8acb\u6c42\u53ef\u80fd\u662f\u5408\u6cd5\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u60e1\u610f\u7684\uff0c\u76ee\u7684\u662f\u4f7f\u4f3a\u670d\u5668\u7121\u6cd5\u97ff\u61c9\u6b63\u5e38\u7528\u6236\u7684\u8acb\u6c42\u3002<\/p>\n<h2 id=\"nginx-%e7%9a%84%e5%9f%ba%e6%9c%ac%e9%98%b2%e7%a6%a6%e6%8e%aa%e6%96%bd-yEdYlpjwtm\">nginx \u7684\u57fa\u672c\u9632\u79a6\u63aa\u65bd<\/h2>\n<p>nginx \u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u914d\u7f6e\u9078\u9805\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6e1b\u5c11 DDoS \u653b\u64ca\u7684\u5f71\u97ff\uff1a<\/p>\n<ul>\n<li><strong>\u9650\u5236\u9023\u63a5\u6578\uff1a<\/strong>\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u6bcf\u500b IP \u5730\u5740\u7684\u6700\u5927\u9023\u63a5\u6578\u4f86\u9650\u5236\u540c\u6642\u9023\u63a5\u7684\u6578\u91cf\u3002<\/li>\n<li><strong>\u9650\u5236\u8acb\u6c42\u901f\u7387\uff1a<\/strong>\u901a\u904e\u8a2d\u7f6e\u6bcf\u500b IP \u5730\u5740\u7684\u8acb\u6c42\u901f\u7387\u4f86\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7de9\u5b58\uff1a<\/strong>nginx \u7684\u7de9\u5b58\u529f\u80fd\u53ef\u4ee5\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u5f9e\u800c\u63d0\u9ad8\u5176\u62b5\u6297 DDoS \u653b\u64ca\u7684\u80fd\u529b\u3002<\/li>\n<\/ul>\n<h3 id=\"%e9%99%90%e5%88%b6%e9%80%a3%e6%8e%a5%e6%95%b8-yEdYlpjwtm\">\u9650\u5236\u9023\u63a5\u6578<\/h3>\n<pre><code>http {\n    limit_conn_zone $binary_remote_addr zone=addr:10m;\n    \n    server {\n        location \/ {\n            limit_conn addr 10;  # \u6bcf\u500b IP \u5730\u5740\u6700\u591a 10 \u500b\u9023\u63a5\n        }\n    }\n}<\/code><\/pre>\n<h3 id=\"%e9%99%90%e5%88%b6%e8%ab%8b%e6%b1%82%e9%80%9f%e7%8e%87-yEdYlpjwtm\">\u9650\u5236\u8acb\u6c42\u901f\u7387<\/h3>\n<pre><code>http {\n    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=1r\/s;  # \u6bcf\u79d2\u6700\u591a 1 \u500b\u8acb\u6c42\n    \n    server {\n        location \/ {\n            limit_req zone=req_limit burst=5 nodelay;  # \u7a81\u767c\u8acb\u6c42\u6700\u591a 5 \u500b\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"%e9%80%b2%e9%9a%8e%e9%98%b2%e7%a6%a6%e6%8e%aa%e6%96%bd-yEdYlpjwtm\">\u9032\u968e\u9632\u79a6\u63aa\u65bd<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u9632\u79a6\u63aa\u65bd\uff0cnginx \u9084\u652f\u6301\u4e00\u4e9b\u9032\u968e\u7684\u9632\u79a6\u6280\u8853\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u9632\u706b\u7246\uff1a<\/strong>\u53ef\u4ee5\u7d50\u5408 iptables \u6216\u5176\u4ed6\u9632\u706b\u7246\u5de5\u5177\u4f86\u904e\u6ffe\u4e0d\u5fc5\u8981\u7684\u6d41\u91cf\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u6a21\u7d44\uff1a<\/strong>\u5982 ngx_http_limit_req_module \u548c ngx_http_limit_conn_module\uff0c\u9019\u4e9b\u6a21\u7d44\u53ef\u4ee5\u63d0\u4f9b\u66f4\u7d30\u7dfb\u7684\u6d41\u91cf\u63a7\u5236\u3002<\/li>\n<li><strong>\u8a2d\u7f6e\u9ed1\u540d\u55ae\u548c\u767d\u540d\u55ae\uff1a<\/strong>\u6839\u64da IP \u5730\u5740\u8a2d\u7f6e\u9ed1\u540d\u55ae\u548c\u767d\u540d\u55ae\uff0c\u963b\u6b62\u53ef\u7591\u7684\u6d41\u91cf\u3002<\/li>\n<\/ul>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e9%98%b2%e7%81%ab%e7%89%86-yEdYlpjwtm\">\u4f7f\u7528\u9632\u706b\u7246<\/h3>\n<p>\u5728\u4f3a\u670d\u5668\u4e0a\u914d\u7f6e iptables \u4f86\u904e\u6ffe\u6d41\u91cf\u662f\u4e00\u500b\u6709\u6548\u7684\u9632\u79a6\u63aa\u65bd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u898f\u5247\u4f86\u9650\u5236\u7279\u5b9a IP \u5730\u5740\u7684\u6d41\u91cf\uff1a<\/p>\n<pre><code>iptables -A INPUT -s 192.168.1.1 -j DROP  # \u963b\u6b62\u7279\u5b9a IP \u5730\u5740\u7684\u6d41\u91cf<\/code><\/pre>\n<h3 id=\"%e8%a8%ad%e7%bd%ae%e9%bb%91%e5%90%8d%e5%96%ae%e5%92%8c%e7%99%bd%e5%90%8d%e5%96%ae-yEdYlpjwtm\">\u8a2d\u7f6e\u9ed1\u540d\u55ae\u548c\u767d\u540d\u55ae<\/h3>\n<pre><code>http {\n    geo $block_ip {\n        default 0;\n        192.168.1.1 1;  # \u5c07\u6b64 IP \u5730\u5740\u52a0\u5165\u9ed1\u540d\u55ae\n    }\n    \n    server {\n        if ($block_ip) {\n            return 403;  # \u8fd4\u56de 403 \u7981\u6b62\u8a2a\u554f\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yEdYlpjwtm\">\u7e3d\u7d50<\/h2>\n<p>nginx \u63d0\u4f9b\u4e86\u591a\u7a2e\u6709\u6548\u7684\u9632\u79a6 DDoS \u653b\u64ca\u7684\u624b\u6bb5\uff0c\u5f9e\u57fa\u672c\u7684\u9023\u63a5\u6578\u548c\u8acb\u6c42\u901f\u7387\u9650\u5236\uff0c\u5230\u9032\u968e\u7684\u9632\u706b\u7246\u914d\u7f6e\u548c\u9ed1\u767d\u540d\u55ae\u8a2d\u7f6e\uff0c\u9019\u4e9b\u63aa\u65bd\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u4f3a\u670d\u5668\u7684\u5b89\u5168\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u7a69\u5b9a\u6027\u7684\u7db2\u7ad9\uff0c\u9078\u64c7\u5408\u9069\u7684\u9632\u79a6\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u4fdd\u8b77\u60a8\u7684\u4f3a\u670d\u5668\u514d\u53d7 DDoS \u653b\u64ca\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 Nginx \u5b9e\u73b0 DDoS \u6514\u622a\uff0c\u4fdd\u62a4\u7f51\u7ad9\u514d\u53d7\u6076\u610f\u6d41\u91cf\u653b\u51fb\uff0c\u786e\u4fdd\u7a33\u5b9a\u6027\u4e0e\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-51153","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=51153"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51153\/revisions"}],"predecessor-version":[{"id":51154,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51153\/revisions\/51154"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}