{"id":1335,"date":"2020-04-02T02:12:32","date_gmt":"2020-04-02T02:12:32","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1335"},"modified":"2020-12-02T02:37:37","modified_gmt":"2020-12-02T02:37:37","slug":"nginx-ip-limit","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2020\/04\/nginx-ip-limit\/","title":{"rendered":"Nginx\u914d\u7f6e-\u975e\u7279\u5b9aIP\u9700\u8981\u8f93\u5165\u5bc6\u7801\u624d\u80fd\u8bbf\u95ee"},"content":{"rendered":"
<\/div>\n

Nginx\u914d\u7f6e\uff0c\u975e\u7279\u5b9aIP\u9700\u8981\u8f93\u5165\u5bc6\u7801\u624d\u80fd\u8bbf\u95ee.md<\/p>\n

\u9700\u6c42<\/h2>\n

\u6709\u4e00\u4e2a\u540e\u53f0\u7ad9\u70b9\uff0c\u4ec5\u5141\u8bb8\u516c\u53f8\u7684\u5916\u7f51IP\u53ef\u4ee5\u8bbf\u95ee\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5728nginx\u4e2d\u4f7f\u7528allow\u3001deny\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5982\u679c\u524d\u9762\u8fd8\u6709CDN\u6216\u8005\u8d1f\u8f7d\u5747\u8861\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528$http_x_forwarded_for\u53c2\u6570\u6765\u8fdb\u884c\u5224\u65ad\uff1b
\n\u4eca\u5929\u53c8\u6765\u4e86\u4e00\u4e2a\u65b0\u9700\u6c42\uff0c\u5047\u5982\u7528\u6237\u4e0d\u662f\u5728\u516c\u53f8\uff0c\u800c\u662f\u5728\u5bb6\u91cc\u4e5f\u60f3\u8bbf\u95ee\uff0c\u4f46\u53c8\u4e0d\u60f3\u62e8VPN\uff0c\u548b\u529e\uff1f
\n\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u7528\u6237\u8f93\u5165\u4e00\u5c42\u5bc6\u7801\u6765\u8fdb\u884c\u4fdd\u62a4\u3002
\n\u603b\u7ed3\u8d77\u6765\u5c31\u662f\uff0c\u6307\u5b9aIP(\u516c\u53f8\u5916\u7f51IP)\u80fd\u76f4\u63a5\u6253\u5f00\uff0c\u975e\u6307\u5b9aIP\u9700\u8981\u8f93\u5165\u5bc6\u7801\u624d\u80fd\u6253\u5f00\u3002<\/p>\n

\u5b9e\u73b0<\/h2>\n
\n
    location \/ {\n        proxy_pass http:\/\/test;\n\n        #auth while from internet\n        set $auth_basic off;\n\n         if ( $http_x_forwarded_for !~ \"223.21.230.186|112.134.186.(25[0-5]|2[0-4][0-9]|1[0-9]\\{2\\}|[1-9][0-9]|[0-9])\") {       \n            set $auth_basic Restricted;   \n        }\n\n        auth_basic $auth_basic;\n        auth_basic_user_file htpasswd;\n\n    }\n<\/code><\/pre>\n<\/div>\n

\u4e0d\u80fd\u76f4\u63a5\u5728if\u4e2d\u5199auth_basic\uff0c\u4f1a\u62a5\u9519\"nginx: [emerg] \"auth_basic\" directive is not allowed here\"<\/p>\n

\n
 if ( $http_x_forwarded_for !~ \"223.21.230.186|104.30.235.2\") {\n        auth_basic \"abcdef\";\n        auth_basic_user_file htpasswd;\n}<\/code><\/pre>\n<\/div>\n
\u6765\u81ea\u4e3a\u77e5\u7b14\u8bb0(Wiz)<\/a><\/div>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Nginx\u914d\u7f6e-\u975e\u7279\u5b9aIP\u9700\u8981\u8f93\u5165\u5bc6\u7801\u624d\u80fd\u8bbf\u95ee<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Nginx\u914d\u7f6e\uff0c\u975e\u7279\u5b9aIP\u9700\u8981\u8f93\u5165\u5bc6\u7801\u624d\u80fd\u8bbf\u95ee.md \u9700\u6c42 \u6709\u4e00\u4e2a\u540e\u53f0\u7ad9\u70b9\uff0c\u4ec5\u5141\u8bb8\u516c\u53f8\u7684\u5916\u7f51IP\u53ef\u4ee5\u8bbf\u95ee\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5728nginx\u4e2d\u4f7f\u7528allow\u3001deny\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5982\u679c\u524d\u9762\u8fd8\u6709CDN\u6216\u8005\u8d1f\u8f7d\u5747\u8861\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528$http_x_forwarded_for\u53c2\u6570\u6765\u8fdb\u884c\u5224\u65ad\uff1b \u4eca\u5929\u53c8\u6765\u4e86\u4e00\u4e2a\u65b0\u9700\u6c42\uff0c\u5047\u5982\u7528\u6237\u4e0d\u662f\u5728\u516c\u53f8\uff0c\u800c\u662f\u5728\u5bb6\u91cc\u4e5f\u60f3\u8bbf\u95ee\uff0c\u4f46\u53c8\u4e0d\u60f3\u62e8VPN\uff0c\u548b\u529e\uff1f \u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u7528\u6237\u8f93\u5165\u4e00\u5c42\u5bc6\u7801\u6765\u8fdb\u884c\u4fdd\u62a4\u3002 \u603b\u7ed3\u8d77\u6765\u5c31\u662f\uff0c\u6307\u5b9aIP(\u516c\u53f8\u5916\u7f51IP)\u80fd\u76f4\u63a5\u6253\u5f00\uff0c\u975e\u6307\u5b9aIP\u9700\u8981\u8f93\u5165\u5bc6\u7801\u624d\u80fd\u6253\u5f00\u3002 \u5b9e\u73b0 location \/ { proxy_pass http:\/\/test; #auth while from internet set $auth_basic off; if ( $http_x_forwarded_for !~ “223.21.230.186|112.134.186.(25[0-5]|2[0-4][0-9]|1[0-9]\\{2\\}|[1-9][0-9]|[0-9])”) { set $auth_basic Restricted; } auth_basic $auth_basic; auth_basic_user_file htpasswd; } \u4e0d\u80fd\u76f4\u63a5\u5728if\u4e2d\u5199auth_basic\uff0c\u4f1a\u62a5\u9519”nginx: [emerg] “auth_basic” directive is not allowed here” if ( $http_x_forwarded_for !~ “223.21.230.186|104.30.235.2”) { auth_basic “abcdef”; auth_basic_user_file htpasswd; } \u6765\u81ea\u4e3a\u77e5\u7b14\u8bb0(Wiz) \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[14],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1335"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1335"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1335\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1335\/revisions\/1336"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}