{"id":3719,"date":"2018-11-18T16:12:58","date_gmt":"2018-11-18T08:12:58","guid":{"rendered":"http:\/\/switch.linesno.com\/?p=3719"},"modified":"2018-11-18T16:15:37","modified_gmt":"2018-11-18T08:15:37","slug":"nginx%e9%99%90%e5%88%b6ip%e6%81%b6%e6%84%8f%e8%b0%83%e7%94%a8%e7%9f%ad%e4%bf%a1%e6%8e%a5%e5%8f%a3%e5%a4%84%e7%90%86%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/switch.linesno.com\/?p=3719","title":{"rendered":"nginx\u9650\u5236IP\u6076\u610f\u8c03\u7528\u77ed\u4fe1\u63a5\u53e3\u5904\u7406\u65b9\u6cd5"},"content":{"rendered":"<p>\u771f\u5b9e\u6848\u4f8b\uff1a<\/p>\n<p>\u67e5\u770bnginx\u65e5\u5fd7\uff0c\u53d1\u73b0\u522b\u6709\u7528\u5fc3\u7684\u4eba\u6076\u610f\u8c03\u7528API\u63a5\u53e3\u5237\u77ed\u4fe1\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>30966487 115.213.229.38 \"-\" [05\/Jun\/2018:14:37:29 +0800] 0.003 xxxxxx.com \"POST \/xxx\/sendCheckCode HTTP\/1.1\" 401 200 46 xx.xx.xx.xx:0000 0.003 200 \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko\/20100101 Firefox\/27.0\" \"https:\/\/xxxxxx\/sendCheckCode\"\r\n30963985 60.181.111.140 \"-\" [05\/Jun\/2018:14:37:29 +0800] 0.004 xxxxxx.com \"POST \/xxx\/sendCheckCode HTTP\/1.1\" 401 200 46 xx.xx.xx.xx:0000 0.004 200 \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko\/20100101 Firefox\/27.0\" \"https:\/\/xxxxxx\/sendCheckCode\"\r\n30959954 220.190.18.25 \"-\" [05\/Jun\/2018:14:37:29 +0800] 0.003 xxxxxx.com \"POST \/xxx\/sendCheckCode HTTP\/1.1\" 401 200 46 xx.xx.xx.xx:0000 0.003 200 \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko\/20100101 Firefox\/27.0\" \"https:\/\/xxxxxx\/sendCheckCode\"<\/pre>\n<\/div>\n<p>\u601d\u8003\u4e86\u51e0\u79cd\u65b9\u6848\uff0c\u6700\u7ec8\u8003\u8651\u4f7f\u7528ip\u9ed1\u540d\u5355\u7684\u65b9\u5f0f\uff1a<\/p>\n<p>\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<p>\u4e00\u3001nginx\u9ed1\u540d\u5355\u65b9\u5f0f\uff1a<\/p>\n<p>1\u3001\u8fc7\u6ee4\u65e5\u5fd7\u8bbf\u95eeAPI\u63a5\u53e3\u7684IP\uff0c\u7edf\u8ba1\u6bcf10\u5206\u949f\u8c03\u7528\u8d85\u8fc7100\u6b21\u7684IP\uff0c\u76f4\u63a5\u4e22\u8fdbnginx\u7684\u8bbf\u95ee\u9ed1\u540d\u5355<\/p>\n<p>2\u3001\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<p>\u7f16\u5199shell\u811a\u672c\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>vim \/shell\/nginx_cutaccesslog.sh\r\n#!\/bin\/bash\r\nlog_path=\/xxx\/nginx\/logs\r\ndate=`date -d \"10 min ago\" +%Y%m%d-%H:%M:%S`\r\nnginxpid=`cat ${log_path}\/nginx.pid`\r\ncd ${log_path}\r\n#\u8fc7\u6ee4access.log\u4e2d\u6b63\u5e38\u8bbf\u95eeAPI\u63a5\u53e3\u5e76\u572810\u5206\u949f\uff08\u4e0b\u9762\u662f\u65e5\u5fd7\u5207\u5272\uff0c\u518d\u505a\u4e2a\u5b9a\u65f6\u4efb\u52a1\u6bcf10\u5206\u949f\u6267\u884c\u4e00\u6b21\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4e86\uff09\u5185\u8bbf\u95ee\u91cf\u6700\u9ad8\u768430\u4e2aIP\uff0c\u53d6\u503c\u5982\u679c\u6b64IP\u8bbf\u95ee\u91cf\u5927\u4e8e100\u6b21\uff0c\u5219\u628a\u6b64IP\u653e\u5165\u9ed1\u540d\u5355\r\ncat access.log | grep  sendCheckCode  | grep -v 403 | awk '{print $2}'|sort|uniq -c | sort -k1 -n | tail -30 | awk '{if($1&gt;100) print \"deny \"$2\";\"}' &gt; ..\/conf\/denyip.conf\r\n#\u65e5\u5fd7\u5207\u5272\uff0c\u505a\u5b9a\u65f6\u4efb\u52a1\uff0c\u6bcf10\u5206\u949f\u6267\u884c\u4e00\u6b21\r\nmv ${log_path}\/access.log ${log_path}\/accesslog.bak\/access_${date}.log\r\n..\/sbin\/nginx -s reload<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u53ef\u81ea\u5df1\u5b9a\u4e49\u65f6\u95f4\u95f4\u9694\u548c\u8bbf\u95ee\u91cf\uff0c\u4e5f\u53ef\u53d6\u6d88\u7b5b\u9009\u8bbf\u95ee\u91cf\u6700\u9ad8\u768430\u4e2a\uff0c\u76f4\u63a5\u53d6\u503c\u6bcf10\u5206\u949f\u8bbf\u95ee\u63a5\u53e3\u8d85\u8fc7100\u6b21\u7684<\/p>\n<p>\u5176\u4e2d\uff1a&#8221;grep -v 403&#8243; \u662f\u628a\u5df2\u7ecf\u7981\u6b62\u8bbf\u95ee\u7684IP\u7ed9\u8fc7\u6ee4\u6389\uff0c\u53ea\u7b5b\u9009\u6b63\u5e38\u8bbf\u95ee\u7684<\/p>\n<p>3\u3001\u4fee\u6539nginx.conf<\/p>\n<p>\u5728http\u6a21\u5757\u52a0\u5165\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>include       denyip.conf;<\/pre>\n<\/div>\n<p>\u91cd\u65b0\u52a0\u8f7dnginx\u751f\u6548\u3002<\/p>\n<p>4\u3001\u6dfb\u52a0\u8ba1\u5212\u4efb\u52a1\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>*\/10 * * * * \/bin\/bash \/shell\/nginx_cutaccesslog.sh &gt; \/dev\/null 2&gt;&amp;1<\/pre>\n<\/div>\n<p>5\u3001\u9a8c\u8bc1\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>[root@xxx logs]# ll accesslog.bak\/\r\n-rw-r--r-- 1 root   root    2663901 Jun  5 15:10 access_20180605-15:00:01.log\r\n-rw-r--r-- 1 root   root   13696947 Jun  5 15:20 access_20180605-15:10:01.log\r\n-rw-r--r-- 1 root   root   13265509 Jun  5 15:30 access_20180605-15:20:01.log\r\n-rw-r--r-- 1 root   root   13846297 Jun  5 15:40 access_20180605-15:30:01.log\r\n[root@xxx logs]# cat ..\/conf\/denyip.conf \r\n\u2026\u2026\u2026\u2026\r\n\u2026\u2026\u2026\u2026\r\ndeny 112.12.137.28;\r\ndeny 183.167.237.229;\r\ndeny 111.41.43.58;\r\ndeny 115.217.117.159;\r\ndeny 219.133.100.133;\r\ndeny 171.221.254.115;\r\ndeny 60.184.131.6;\r\n\u2026\u2026\u2026\u2026\r\n\u2026\u2026\u2026\u2026<\/pre>\n<\/div>\n<p>\u518d\u67e5\u770b\u5df2\u7ecf\u7981\u7528IP\u7684\u8bbf\u95ee\u65e5\u5fd7\uff0c\u5219\u4f1a\u8fd4\u56de403\u9519\u8bef\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>[root@xxx logs]# tail -f access.log | grep \"60.184.131.6\"\r\n31268622 60.184.131.6 \"-\" [05\/Jun\/2018:15:47:34 +0800] 0.000 xxxxxx.com \"POST \/xxxxxx\/sendCheckCode HTTP\/1.1\" 377 403 168 - - - \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko\/20100101 Firefox\/27.0\" \"https:\/\/xxxxxx\/sendCheckCode\"\r\n31268622 60.184.131.6 \"-\" [05\/Jun\/2018:15:47:35 +0800] 0.000 xxxxxx.com \"POST \/xxxxxx\/sendCheckCode HTTP\/1.1\" 377 403 168 - - - \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko\/20100101 Firefox\/27.0\" \"https:\/\/xxxxxx\/sendCheckCode\"\r\n31268622 60.184.131.6 \"-\" [05\/Jun\/2018:15:47:35 +0800] 0.000 xxxxxx.com \"POST \/xxxxxx\/sendCheckCode HTTP\/1.1\" 377 403 168 - - - \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko\/20100101 Firefox\/27.0\" \"https:\/\/xxxxxx\/sendCheckCode\"<\/pre>\n<\/div>\n<p>\u4e8c\u3001\u9650\u5236IP\u8bf7\u6c42\u6570\uff1a<\/p>\n<p>\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u7684\u65b9\u6cd5\u8fd8\u6709\u4e00\u79cd\u662f\u9650\u5236\u5355 IP \u5355\u4f4d\u65f6\u95f4\u7684\u8bf7\u6c42\u6570\uff0c\u4ee5\u53ca\u5355 IP \u7684\u5e76\u53d1\u8fde\u63a5\u6570<\/p>\n<p>\u6b64\u65b9\u6cd5\u6ca1\u6709\u5b9e\u9645\u8fd0\u7528\uff0c\u56e0\u4e3a\u611f\u89c9\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u8bef\u6740\u6b63\u5e38\u7684\u8bbf\u95ee\u7528\u6237<\/p>\n<p>\u5199\u4e00\u4e0b\u6b64\u65b9\u6cd5\u7684\u5927\u6982\u914d\u7f6e\uff0chttp\u6a21\u5757\u52a0\u5165\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>http {\r\n    limit_req_zone $binary_remote_addr zone=one:10m rate=8r\/s;\r\n    server {\r\n        location \/search\/ {\r\n            limit_req zone=one burst=5;\r\n        }<\/pre>\n<\/div>\n<p>\u5982\u4f55\u4f30\u7b97 limit_req_zone size\uff1a<\/p>\n<p>\u4e00\u5146\u5b57\u8282\u533a\u57df\u53ef\u4ee5\u4fdd\u6301\u5927\u7ea61\u4e076064\u5b57\u8282\u7684\u72b6\u6001\u3002<\/p>\n<p>\u90a3\u4e48 10M \u5c31\u53ef\u4ee5\u5b58\u50a8 16 \u4e07\u7684 IP \u7edf\u8ba1\u4fe1\u606f, \u8fd9\u4e2a\u5bf9\u666e\u901a\u5e94\u7528\u8db3\u591f\u4e86\uff0c16 \u4e07\u6bcf\u79d2\u7684 UV\uff0c\u5df2\u7ecf\u8d85\u7ea7\u5389\u5bb3\u4e86\u3002<\/p>\n<p>\u5982\u679c size \u7684\u5927\u5c0f\u5982\u679c\u8bbe\u7f6e\u5c0f\u4e86, \u4f8b\u5982\u8bbe\u7f6e\u6210 1M\uff0c\u90a3\u4e48\u5f53\u4e00\u79d2\u5185\u7684\u8bf7\u6c42 IP \u6570\u8d85\u8fc7 16000 \u7684\u65f6\u5019\uff0c\u8d85\u51fa\u7684 IP \u5bf9\u5e94\u7684\u7528\u6237\u770b\u5230\u7684\u5747\u4e3a 503 Service Temporarily Unavailable \u9875\u9762\u4e86\u3002\u53c2\u8003, \u6f0f\u6876\u7b97\u6cd5 Leaky Bucket\u3002 \u540c\u65f6\uff0crate \u7684\u5355\u4f4d\u7528 r\/s \u975e\u5e38\u5408\u9002\uff0c\u5982\u679c\u6362\u6210\u6309\u5929\uff0c\u6309\u5c0f\u65f6\u8ba1\u6570\uff0c10M \u7684\u5185\u5b58\u80af\u5b9a\u4e0d\u591f\u7528\u3002<\/p>\n<p>\u5982\u4f55\u4f30\u7b97 limit_req_zone rate\uff1a<\/p>\n<p>\u9996\u5148\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u666e\u901a\u6d4f\u89c8\u5668\u7684\u540c\u65f6\u5e76\u53d1\u6570\u91cf\u3002\u6309\u7167 Dropbox \u6280\u672f\u535a\u5ba2\u91cc\u6240\u8c08\u5230\u7684\uff0c\u76ee\u524d\u4e3b\u6d41\u6d4f\u89c8\u5668\u9650\u5236 AJAX \u5bf9\u540c\u4e00\u4e2a\u5b50\u57df\u540d\u7684\u5e76\u53d1\u8fde\u63a5\u6570\u662f6\u4e2a\u3002IE 6\uff0cIE 7 \u662f\u4e24\u4e2a\u3002<\/p>\n<p>\u5927\u591a\u6570\u6d4f\u89c8\u5668\u6bcf\u4e2a\u4e3b\u673a\u540d\u90fd\u67096\u4e2a\u5e76\u53d1\u8fde\u63a5\u7684\u9650\u5236\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u771f\u5b9e\u6848\u4f8b\uff1a \u67e5\u770bnginx\u65e5\u5fd7\uff0c\u53d1\u73b0\u522b\u6709\u7528\u5fc3\u7684\u4eba\u6076\u610f\u8c03\u7528API\u63a5\u53e3\u5237\u77ed\u4fe1\uff1a 30966487 115.213. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3720,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-day"],"_links":{"self":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3719"}],"version-history":[{"count":2,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3719\/revisions"}],"predecessor-version":[{"id":3727,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3719\/revisions\/3727"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/media\/3720"}],"wp:attachment":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3719"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}