{"id":18016,"date":"2024-11-18T13:10:21","date_gmt":"2024-11-18T05:10:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/18016\/"},"modified":"2024-11-18T13:10:21","modified_gmt":"2024-11-18T05:10:21","slug":"nginx%e7%99%bd%e5%90%8d%e5%8d%95%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/18016\/","title":{"rendered":"nginx\u767d\u540d\u5355\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  nginx \u767d\u540d\u5355\u9650\u5236\u8bbf\u95ee\u7279\u5b9a\u5185\u5bb9\uff0c\u4ec5\u5141\u8bb8\u6765\u81ea\u6388\u6743\u6765\u6e90\u7684\u8bf7\u6c42\u3002\u914d\u7f6e\u6b65\u9aa4\u5982\u4e0b\uff1a\u786e\u5b9a\u5141\u8bb8\u7684 ip \u5730\u5740\u6216\u57df\u540d\u3002\u521b\u5efa nginx \u670d\u52a1\u5668\u5757\u6765\u5904\u7406\u8bf7\u6c42\u3002\u6dfb\u52a0 allow \u6307\u4ee4\uff0c\u5141\u8bb8\u6307\u5b9a\u6765\u6e90\u8bbf\u95ee\u3002\u6dfb\u52a0 deny \u6307\u4ee4\uff08\u53ef\u9009\uff09\uff0c\u62d2\u7edd\u9664\u767d\u540d\u5355\u5916\u7684\u6240\u6709\u8bbf\u95ee\u3002\u4fdd\u5b58\u914d\u7f6e\u5e76\u91cd\u65b0\u52a0\u8f7d nginx\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/27\/2024052718212276799.jpg\" class=\"aligncenter\" title=\"nginx\u767d\u540d\u5355\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"nginx\u767d\u540d\u5355\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e  \u767d\u540d\u5355<\/strong><\/p>\n<p>\u5728 nginx \u4e2d\uff0c\u767d\u540d\u5355\u662f\u4e00\u79cd\u8bbf\u95ee\u63a7\u5236\u673a\u5236\uff0c\u7528\u4e8e\u9650\u5236\u5bf9\u7f51\u7ad9\u6216\u5e94\u7528\u7a0b\u5e8f\u7279\u5b9a\u90e8\u5206\u7684\u8bbf\u95ee\uff0c\u4ec5\u5141\u8bb8\u6765\u81ea\u6388\u6743\u6765\u6e90\u7684\u8bf7\u6c42\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u4e86\u5982\u4f55\u8bbe\u7f6e nginx \u767d\u540d\u5355\uff1a<\/p>\n<p><strong>1. \u786e\u5b9a\u5141\u8bb8\u7684 IP \u5730\u5740\u6216\u57df\u540d<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u786e\u5b9a\u8981\u5141\u8bb8\u8bbf\u95ee\u53d7\u4fdd\u62a4\u5185\u5bb9\u7684 IP \u5730\u5740\u6216\u57df\u540d\u3002\u8fd9\u4e9b\u53ef\u80fd\u662f\u4f60\u81ea\u5df1\u7684 IP \u5730\u5740\u3001\u53d7\u4fe1\u4efb\u7684\u5408\u4f5c\u4f19\u4f34\u6216\u5ba2\u6237\u7684 IP \u5730\u5740\u3002<\/p>\n<p><strong>2. \u521b\u5efa nginx \u914d\u7f6e\u5757<\/strong><\/p>\n<p>\u5728 nginx \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u670d\u52a1\u5668\u5757\u6765\u5904\u7406\u5c06\u5e94\u7528\u767d\u540d\u5355\u7684\u8bf7\u6c42\u3002<\/p>\n<p><strong>3. \u6dfb\u52a0 allow \u6307\u4ee4<\/strong><\/p>\n<p>\u5728\u670d\u52a1\u5668\u5757\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b allow \u6307\u4ee4\uff0c\u4ee5\u5141\u8bb8\u6765\u81ea\u6307\u5b9a IP \u5730\u5740\u6216\u57df\u540d\u7684\u8bbf\u95ee\uff1a<\/p>\n<pre>allow 192.168.0.1;\nallow example.com;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u6dfb\u52a0 deny \u6307\u4ee4\uff08\u53ef\u9009\uff09<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u62d2\u7edd\u9664\u767d\u540d\u5355\u4ee5\u5916\u7684\u6240\u6709\u8bbf\u95ee\uff0c\u5219\u53ef\u4ee5\u6dfb\u52a0 deny \u6307\u4ee4\uff1a<\/p>\n<pre>deny all;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4fdd\u5b58\u914d\u7f6e\u5e76\u91cd\u65b0\u52a0\u8f7d nginx<\/strong><\/p>\n<p>\u4fdd\u5b58 nginx \u914d\u7f6e\u6587\u4ef6\u5e76\u5c06\u66f4\u6539\u5e94\u7528\u4e8e\u670d\u52a1\u5668\uff1a<\/p>\n<pre>sudo systemctl reload nginx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u914d\u7f6e<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f nginx \u767d\u540d\u5355\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre>server {\n    listen 80;\n\n    location \/protected\/ {\n        allow 192.168.0.1;\n        allow example.com;\n        deny all;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u914d\u7f6e\u5c06\u5141\u8bb8\u6765\u81ea 192.168.0.1 IP \u5730\u5740\u548c example.com \u57df\u540d\u7684\u8bf7\u6c42\u8bbf\u95ee \/protected\/ \u76ee\u5f55\uff0c\u800c\u62d2\u7edd\u6240\u6709\u5176\u4ed6\u8bf7\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u767d\u540d\u5355\u600e\u4e48\u8bbe\u7f6e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nginx \u767d\u540d\u5355\u9650\u5236\u8bbf\u95ee\u7279\u5b9a\u5185\u5bb9\uff0c\u4ec5\u5141\u8bb8\u6765\u81ea\u6388\u6743\u6765\u6e90\u7684\u8bf7\u6c42\u3002\u914d\u7f6e\u6b65\u9aa4\u5982\u4e0b\uff1a\u786e\u5b9a\u5141\u8bb8\u7684 ip \u5730\u5740\u6216\u57df\u540d\u3002\u521b\u5efa nginx \u670d\u52a1\u5668\u5757\u6765\u5904\u7406\u8bf7\u6c42\u3002\u6dfb\u52a0 allow \u6307\u4ee4\uff0c\u5141\u8bb8\u6307\u5b9a\u6765\u6e90\u8bbf\u95ee\u3002\u6dfb\u52a0 deny \u6307\u4ee4\uff08\u53ef\u9009\uff09\uff0c\u62d2\u7edd\u9664\u767d\u540d\u5355\u5916\u7684\u6240\u6709\u8bbf\u95ee\u3002\u4fdd\u5b58\u914d\u7f6e\u5e76\u91cd\u65b0\u52a0\u8f7d nginx\u3002 \u5982\u4f55\u8bbe\u7f6e \u767d\u540d\u5355 \u5728 nginx \u4e2d\uff0c\u767d\u540d\u5355\u662f\u4e00\u79cd\u8bbf\u95ee\u63a7\u5236\u673a\u5236\uff0c\u7528\u4e8e\u9650\u5236\u5bf9\u7f51\u7ad9\u6216\u5e94\u7528\u7a0b\u5e8f\u7279\u5b9a\u90e8\u5206\u7684\u8bbf\u95ee\uff0c\u4ec5\u5141\u8bb8\u6765\u81ea\u6388\u6743\u6765\u6e90\u7684\u8bf7\u6c42\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u4e86\u5982\u4f55\u8bbe\u7f6e nginx \u767d\u540d\u5355\uff1a 1. \u786e\u5b9a\u5141\u8bb8\u7684 IP \u5730\u5740\u6216\u57df\u540d \u9996\u5148\uff0c\u786e\u5b9a\u8981\u5141\u8bb8\u8bbf\u95ee\u53d7\u4fdd\u62a4\u5185\u5bb9\u7684 IP \u5730\u5740\u6216\u57df\u540d\u3002\u8fd9\u4e9b\u53ef\u80fd\u662f\u4f60\u81ea\u5df1\u7684 IP \u5730\u5740\u3001\u53d7\u4fe1\u4efb\u7684\u5408\u4f5c\u4f19\u4f34\u6216\u5ba2\u6237\u7684 IP \u5730\u5740\u3002 2. \u521b\u5efa nginx \u914d\u7f6e\u5757 \u5728 nginx \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u670d\u52a1\u5668\u5757\u6765\u5904\u7406\u5c06\u5e94\u7528\u767d\u540d\u5355\u7684\u8bf7\u6c42\u3002 3. \u6dfb\u52a0 allow \u6307\u4ee4 \u5728\u670d\u52a1\u5668\u5757\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b allow \u6307\u4ee4\uff0c\u4ee5\u5141\u8bb8\u6765\u81ea\u6307\u5b9a IP \u5730\u5740\u6216\u57df\u540d\u7684\u8bbf\u95ee\uff1a allow 192.168.0.1; allow example.com; \u767b\u5f55\u540e\u590d\u5236 4. \u6dfb\u52a0 deny \u6307\u4ee4\uff08\u53ef\u9009\uff09 \u5982\u679c\u4f60\u5e0c\u671b\u62d2\u7edd\u9664\u767d\u540d\u5355\u4ee5\u5916\u7684\u6240\u6709\u8bbf\u95ee\uff0c\u5219\u53ef\u4ee5\u6dfb\u52a0 [&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-18016","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18016","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=18016"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18016\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=18016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=18016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=18016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}