{"id":18326,"date":"2024-11-18T09:07:10","date_gmt":"2024-11-18T01:07:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/18326\/"},"modified":"2024-11-18T09:07:10","modified_gmt":"2024-11-18T01:07:10","slug":"nginx%e4%b8%adlocation%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/18326\/","title":{"rendered":"nginx\u4e2dlocation\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  nginx \u7684 location \u6307\u4ee4\u7528\u4e8e\u5b9a\u4e49\u5904\u7406\u8bf7\u6c42\u7684\u89c4\u5219\u96c6\uff0c\u901a\u8fc7\u5339\u914d uri\u3001http \u65b9\u6cd5\u7b49\u6761\u4ef6\uff0c\u6307\u5b9a\u8bf7\u6c42\u5904\u7406\u65b9\u5f0f\u3002\u8bed\u6cd5\u5305\u62ec\uff1a\u7cbe\u786e\u5339\u914d\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3001\u524d\u7f00\u5339\u914d\u7b49\u3002location \u6307\u4ee4\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f18\u5148\u7ea7\u7531\u5339\u914d\u987a\u5e8f\u51b3\u5b9a\uff0c\u540e\u7eed\u8bf7\u6c42\u53ef\u80fd\u53d7\u5176\u5f71\u54cd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/06\/2024060615032077250.jpg\" class=\"aligncenter\" title=\"nginx\u4e2dlocation\u7528\u6cd5\u63d2\u56fe\" alt=\"nginx\u4e2dlocation\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong> \u4e2d location \u7528\u6cd5<\/strong><\/p>\n<p>location \u6307\u4ee4\u7528\u4e8e\u5b9a\u4e49 nginx \u4e2d\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\u7684\u89c4\u5219\u96c6\u3002\u5b83\u7528\u4e8e\u5339\u914d\u8bf7\u6c42\u7684\u67d0\u4e9b\u65b9\u9762\uff0c\u4f8b\u5982 URI\u3001HTTP \u65b9\u6cd5\u6216\u5176\u4ed6\u6807\u5934\uff0c\u5e76\u6307\u5b9a\u5e94\u5982\u4f55\u5904\u7406\u8bf7\u6c42\u3002<\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<pre>location [= | ~ | ^~] [uri] {\n  # \u6307\u4ee4\u5757\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>=\uff1a\u7cbe\u786e\u5339\u914d URI<\/li>\n<li>~\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d URI<\/li>\n<li>^~\uff1a\u524d\u7f00\u5339\u914d URI<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>location \u6307\u4ee4\u53ef\u4ee5\u5305\u542b\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>alias<\/strong>\uff1a\u6307\u5b9a\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u522b\u540d<\/li>\n<li> <strong>auth_basic<\/strong>\uff1a\u542f\u7528\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1<\/li>\n<li> <strong>auth_basic_user_file<\/strong>\uff1a\u6307\u5b9a\u5305\u542b\u7528\u6237\u51ed\u636e\u7684\u6587\u4ef6<\/li>\n<li> <strong>charset<\/strong>\uff1a\u8bbe\u7f6e\u5b57\u7b26\u96c6<\/li>\n<li> <strong>deny<\/strong>\uff1a\u62d2\u7edd\u5339\u914d\u8bf7\u6c42<\/li>\n<li> <strong>error_page<\/strong>\uff1a\u6307\u5b9a\u9519\u8bef\u9875\u9762<\/li>\n<li> <strong>if<\/strong>\uff1a\u57fa\u4e8e\u6761\u4ef6\u6267\u884c\u6307\u4ee4\u5757<\/li>\n<li> <strong>include<\/strong>\uff1a\u4ece\u5916\u90e8\u6587\u4ef6\u5305\u542b\u6307\u4ee4<\/li>\n<li> <strong>limit_except<\/strong>\uff1a\u6839\u636e\u8bf7\u6c42 URI \u6216 IP \u5730\u5740\u6392\u9664\u8bf7\u6c42\u9650\u5236<\/li>\n<li> <strong>limit_req<\/strong>\uff1a\u9650\u5236\u6bcf\u79d2\u5904\u7406\u7684\u8bf7\u6c42\u6570<\/li>\n<li> <strong>location<\/strong>\uff1a\u5d4c\u5957 location \u6307\u4ee4<\/li>\n<li> <strong>proxy_pass<\/strong>\uff1a\u4ee3\u7406\u8bf7\u6c42\u5230\u540e\u7aef\u670d\u52a1\u5668<\/li>\n<li> <strong>rewrite<\/strong>\uff1a\u91cd\u5199 URI<\/li>\n<li> <strong>return<\/strong>\uff1a\u663e\u5f0f\u8fd4\u56de\u8bf7\u6c42<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5339\u914d\/hello URI \u7684\u8bf7\u6c42\u5e76\u8fd4\u56de\u9759\u6001\u6587\u4ef6\uff1a<\/p>\n<pre>location = \/hello {\n  alias \/var\/www\/html\/hello.html;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5339\u914d\u4ee5\/api\/\u5f00\u5934\u7684 URI \u7684\u8bf7\u6c42\u5e76\u5c06\u5176\u4ee3\u7406\u5230\u540e\u7aef\u670d\u52a1\u5668\uff1a<\/p>\n<pre>location ^~ \/api\/ {\n  proxy_pass http:\/\/backend_server;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u5148\u7ea7<\/strong><\/p>\n<p>location \u6307\u4ee4\u7684\u4f18\u5148\u7ea7\u7531\u5339\u914d\u987a\u5e8f\u51b3\u5b9a\u3002\u7b2c\u4e00\u4e2a\u5339\u914d\u7684 location \u6307\u4ee4\u5c06\u5904\u7406\u8bf7\u6c42\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>location \u6307\u4ee4\u53ef\u4ee5\u5d4c\u5957\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u590d\u6742\u7684\u5339\u914d\u89c4\u5219\u3002<\/li>\n<li>location \u6307\u4ee4\u4e2d\u7684\u6307\u4ee4\u53ef\u4ee5\u5f71\u54cd\u540e\u7eed\u8bf7\u6c42\u3002\u4f8b\u5982\uff0creturn \u6307\u4ee4\u5c06\u7acb\u5373\u505c\u6b62\u8bf7\u6c42\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2dlocation\u7528\u6cd5\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 \u7684 location \u6307\u4ee4\u7528\u4e8e\u5b9a\u4e49\u5904\u7406\u8bf7\u6c42\u7684\u89c4\u5219\u96c6\uff0c\u901a\u8fc7\u5339\u914d uri\u3001http \u65b9\u6cd5\u7b49\u6761\u4ef6\uff0c\u6307\u5b9a\u8bf7\u6c42\u5904\u7406\u65b9\u5f0f\u3002\u8bed\u6cd5\u5305\u62ec\uff1a\u7cbe\u786e\u5339\u914d\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3001\u524d\u7f00\u5339\u914d\u7b49\u3002location \u6307\u4ee4\u53ef\u4ee5\u5d4c\u5957\uff0c\u4f18\u5148\u7ea7\u7531\u5339\u914d\u987a\u5e8f\u51b3\u5b9a\uff0c\u540e\u7eed\u8bf7\u6c42\u53ef\u80fd\u53d7\u5176\u5f71\u54cd\u3002 \u4e2d location \u7528\u6cd5 location \u6307\u4ee4\u7528\u4e8e\u5b9a\u4e49 nginx \u4e2d\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\u7684\u89c4\u5219\u96c6\u3002\u5b83\u7528\u4e8e\u5339\u914d\u8bf7\u6c42\u7684\u67d0\u4e9b\u65b9\u9762\uff0c\u4f8b\u5982 URI\u3001HTTP \u65b9\u6cd5\u6216\u5176\u4ed6\u6807\u5934\uff0c\u5e76\u6307\u5b9a\u5e94\u5982\u4f55\u5904\u7406\u8bf7\u6c42\u3002 \u7528\u6cd5 location [= | ~ | ^~] [uri] { # \u6307\u4ee4\u5757 } \u767b\u5f55\u540e\u590d\u5236 =\uff1a\u7cbe\u786e\u5339\u914d URI ~\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d URI ^~\uff1a\u524d\u7f00\u5339\u914d URI \u8bed\u6cd5 location \u6307\u4ee4\u53ef\u4ee5\u5305\u542b\u4ee5\u4e0b\u8bed\u6cd5\uff1a alias\uff1a\u6307\u5b9a\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u522b\u540d auth_basic\uff1a\u542f\u7528\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1 auth_basic_user_file\uff1a\u6307\u5b9a\u5305\u542b\u7528\u6237\u51ed\u636e\u7684\u6587\u4ef6 charset\uff1a\u8bbe\u7f6e\u5b57\u7b26\u96c6 deny\uff1a\u62d2\u7edd\u5339\u914d\u8bf7\u6c42 error_page\uff1a\u6307\u5b9a\u9519\u8bef\u9875\u9762 if\uff1a\u57fa\u4e8e\u6761\u4ef6\u6267\u884c\u6307\u4ee4\u5757 include\uff1a\u4ece\u5916\u90e8\u6587\u4ef6\u5305\u542b\u6307\u4ee4 limit_except\uff1a\u6839\u636e\u8bf7\u6c42 URI \u6216 IP \u5730\u5740\u6392\u9664\u8bf7\u6c42\u9650\u5236 limit_req\uff1a\u9650\u5236\u6bcf\u79d2\u5904\u7406\u7684\u8bf7\u6c42\u6570 location\uff1a\u5d4c\u5957 location \u6307\u4ee4 [&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-18326","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18326","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=18326"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18326\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=18326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=18326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=18326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}