{"id":95,"date":"2023-03-10T14:22:40","date_gmt":"2023-03-10T06:22:40","guid":{"rendered":"https:\/\/my.leaf.hair\/?p=95"},"modified":"2023-03-10T14:24:20","modified_gmt":"2023-03-10T06:24:20","slug":"%e5%bd%bb%e5%ba%95%e5%bc%84%e6%87%82-nginx-location-%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/my.di.cloudns.asia\/index.php\/2023\/03\/10\/95.html","title":{"rendered":"\u5f7b\u5e95\u5f04\u61c2 Nginx location \u5339\u914d"},"content":{"rendered":"<p>Nginx \u7684 location \u5b9e\u73b0\u4e86\u5bf9\u8bf7\u6c42\u7684\u7ec6\u5206\u5904\u7406\uff0c\u6709\u4e9b URI \u8fd4\u56de\u9759\u6001\u5185\u5bb9\uff0c\u6709\u4e9b\u5206\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u7b49\uff0c\u4eca\u5929\u6765\u5f7b\u5e95\u5f04\u61c2\u5b83\u7684\u5339\u914d\u89c4\u5219<\/p>\n<p>\u4e00\u4e2a\u6700\u7b80\u5355\u7684 location \u7684\u4f8b\u5b50\u5982\u4e0b<\/p>\n<pre><code>server {\n    server_name website.com;\n    location \/admin\/ {\n    # The configuration you place here only applies to\n    # http:\/\/website.com\/admin\/\n    }\n}\n<\/code><\/pre>\n<p>location \u652f\u6301\u7684\u8bed\u6cd5 location [=|~|~*|^~|@] pattern { &#8230; }\uff0c\u4e4d\u4e00\u770b\u8fd8\u633a\u590d\u6742\u7684\uff0c\u6765\u9010\u4e2a\u770b\u4e00\u4e0b\u3002<\/p>\n<h3>location\u4fee\u9970\u7b26\u7c7b\u578b<\/h3>\n<h4>\u300c=\u300d \u4fee\u9970\u7b26\uff1a\u8981\u6c42\u8def\u5f84\u5b8c\u5168\u5339\u914d<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location = \/abcd {\n    [\u2026]\n    }\n}<\/code><\/pre>\n<ul>\n<li><a href=\"http:\/\/website.com\/abcd\u5339\u914d\">http:\/\/website.com\/abcd\u5339\u914d<\/a><\/li>\n<li><a href=\"http:\/\/website.com\/ABCD\u53ef\u80fd\u4f1a\u5339\u914d\">http:\/\/website.com\/ABCD\u53ef\u80fd\u4f1a\u5339\u914d<\/a> \uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5339\u914d\uff0c\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u7cfb\u7edf\u662f\u5426\u5927\u5c0f\u5199\u654f\u611f\uff08case-sensitive\uff09\u3002ps: Mac \u9ed8\u8ba4\u662f\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\uff0cgit \u4f7f\u7528\u4f1a\u6709\u5927\u5751\u3002<\/li>\n<li><a href=\"http:\/\/website.com\/abcd?param1&param2\u5339\u914d\uff0c\u5ffd\u7565\">http:\/\/website.com\/abcd?param1&#038;param2\u5339\u914d\uff0c\u5ffd\u7565<\/a> querystring<\/li>\n<li><a href=\"http:\/\/website.com\/abcd\/\u4e0d\u5339\u914d\uff0c\u5e26\u6709\u7ed3\u5c3e\u7684\/\">http:\/\/website.com\/abcd\/\u4e0d\u5339\u914d\uff0c\u5e26\u6709\u7ed3\u5c3e\u7684\/<\/a><\/li>\n<li><a href=\"http:\/\/website.com\/abcde\u4e0d\u5339\u914d\">http:\/\/website.com\/abcde\u4e0d\u5339\u914d<\/a><\/li>\n<\/ul>\n<h4>\u300c~\u300d\u4fee\u9970\u7b26\uff1a\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location ~ ^\/abcd$ {\n    [\u2026]\n    }\n}<\/code><\/pre>\n<p>^\/abcd$\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u5b57\u7b26\u4e32\u5fc5\u987b\u4ee5\/\u5f00\u59cb\uff0c\u4ee5$\u7ed3\u675f\uff0c\u4e2d\u95f4\u5fc5\u987b\u662fabcd<\/p>\n<ul>\n<li><a href=\"http:\/\/website.com\/abcd\u5339\u914d\uff08\u5b8c\u5168\u5339\u914d\">http:\/\/website.com\/abcd\u5339\u914d\uff08\u5b8c\u5168\u5339\u914d<\/a>\uff09<\/li>\n<li><a href=\"http:\/\/website.com\/ABCD\u4e0d\u5339\u914d\uff0c\u5927\u5c0f\u5199\u654f\u611f\">http:\/\/website.com\/ABCD\u4e0d\u5339\u914d\uff0c\u5927\u5c0f\u5199\u654f\u611f<\/a><\/li>\n<li><a href=\"http:\/\/website.com\/abcd?param1&param2\u5339\u914d\">http:\/\/website.com\/abcd?param1&#038;param2\u5339\u914d<\/a><\/li>\n<li><a href=\"http:\/\/website.com\/abcd\/\u4e0d\u5339\u914d\uff0c\u4e0d\u80fd\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\">http:\/\/website.com\/abcd\/\u4e0d\u5339\u914d\uff0c\u4e0d\u80fd\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a href=\"http:\/\/website.com\/abcde\u4e0d\u5339\u914d\uff0c\u4e0d\u80fd\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\">http:\/\/website.com\/abcde\u4e0d\u5339\u914d\uff0c\u4e0d\u80fd\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f<\/a><\/li>\n<\/ul>\n<h4>\u300c~*\u300d\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location ~* ^\/abcd$ {\n    [\u2026]\n    }\n}<\/code><\/pre>\n<ul>\n<li><a href=\"http:\/\/website.com\/abcd\u5339\u914d\">http:\/\/website.com\/abcd\u5339\u914d<\/a> (\u5b8c\u5168\u5339\u914d)<\/li>\n<li><a href=\"http:\/\/website.com\/ABCD\u5339\u914d\">http:\/\/website.com\/ABCD\u5339\u914d<\/a> (\u5927\u5c0f\u5199\u4e0d\u654f\u611f)<\/li>\n<li><a href=\"http:\/\/website.com\/abcd?param1&param2\u5339\u914d\">http:\/\/website.com\/abcd?param1&#038;param2\u5339\u914d<\/a><\/li>\n<li><a href=\"http:\/\/website.com\/abcd\/\">http:\/\/website.com\/abcd\/<\/a> \u4e0d\u5339\u914d\uff0c\u4e0d\u80fd\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<li><a href=\"http:\/\/website.com\/abcde\">http:\/\/website.com\/abcde<\/a> \u4e0d\u5339\u914d\uff0c\u4e0d\u80fd\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<\/ul>\n<h4>\u300c^~\u300d\u4fee\u9970\u7b26\uff1a\u524d\u7f00\u5339\u914d<\/h4>\n<p>\u5982\u679c\u8be5 location \u662f\u6700\u4f73\u7684\u5339\u914d\uff0c\u90a3\u4e48\u5bf9\u4e8e\u5339\u914d\u8fd9\u4e2a location \u7684\u5b57\u7b26\u4e32\uff0c \u8be5\u4fee\u9970\u7b26\u4e0d\u518d\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u68c0\u6d4b\u3002\u6ce8\u610f\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0c\u5b83\u7684\u76ee\u7684\u662f\u4f18\u5148\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d<br \/>\n\u67e5\u627e\u7684\u987a\u5e8f\u53ca\u4f18\u5148\u7ea7<br \/>\n\u5f53\u6709\u591a\u6761 location \u89c4\u5219\u65f6\uff0cnginx \u6709\u4e00\u5957\u6bd4\u8f83\u590d\u6742\u7684\u89c4\u5219\uff0c\u4f18\u5148\u7ea7\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u7cbe\u786e\u5339\u914d =<\/li>\n<li>\u524d\u7f00\u5339\u914d ^~\uff08\u7acb\u523b\u505c\u6b62\u540e\u7eed\u7684\u6b63\u5219\u641c\u7d22\uff09<\/li>\n<li>\u6309\u6587\u4ef6\u4e2d\u987a\u5e8f\u7684\u6b63\u5219\u5339\u914d ~\u6216~*<\/li>\n<li>\u5339\u914d\u4e0d\u5e26\u4efb\u4f55\u4fee\u9970\u7684\u524d\u7f00\u5339\u914d\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u89c4\u5219\u5927\u4f53\u7684\u601d\u8def\u662f<\/p>\n<blockquote>\n<p>\u5148\u7cbe\u786e\u5339\u914d\uff0c\u6ca1\u6709\u5219\u67e5\u627e\u5e26\u6709 ^~\u7684\u524d\u7f00\u5339\u914d\uff0c\u6ca1\u6709\u5219\u8fdb\u884c\u6b63\u5219\u5339\u914d\uff0c\u6700\u540e\u624d\u8fd4\u56de\u524d\u7f00\u5339\u914d\u7684\u7ed3\u679c\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09<\/p>\n<\/blockquote>\n<p>\u5982\u679c\u4e0a\u8ff0\u89c4\u5219\u4e0d\u597d\u7406\u89e3\uff0c\u53ef\u4ee5\u770b\u4e0b\u9762\u7684\u4f2a\u4ee3\u7801\uff08\u975e\u5e38\u91cd\u8981\uff09<\/p>\n<pre><code>function match(uri):\n  rv = NULL\n\n  if uri in exact_match:\n    return exact_match[uri]\n\n  if uri in prefix_match:\n    if prefix_match[uri] is &#039;^~&#039;:\n      return prefix_match[uri]\n    else:\n      rv = prefix_match[uri] \/\/ \u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709 return\uff0c\u4e14\u8fd9\u91cc\u662f\u6700\u957f\u5339\u914d\n\n  if uri in regex_match:\n    return regex_match[uri] \/\/ \u6309\u6587\u4ef6\u4e2d\u987a\u5e8f\uff0c\u627e\u5230\u5373\u8fd4\u56de\n  return rv<\/code><\/pre>\n<p>\u4e00\u4e2a\u7b80\u5316\u8fc7\u7684Node.js\u5199\u7684\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code>function ngx_http_core_find_location(uri, static_locations, regex_locations, named_locations, track) {\n  let rc = null;\n  let l = ngx_http_find_static_location(uri, static_locations, track);\n  if (l) {\n    if (l.exact_match) {\n      return l;\n    }\n    if (l.noregex) {\n      return l;\n    }\n    rc = l;\n  }\n  if (regex_locations) {\n    for (let i = 0 ; i &lt; regex_locations.length; i ++) {\n      if (track) track(regex_locations[i].id);\n      let n = null;\n      if (regex_locations[i].rcaseless) {\n        n = uri.match(new RegExp(regex_locations[i].name));\n      } else {\n        n = uri.match(new RegExp(regex_locations[i].name), &quot;i&quot;);\n      }\n      if (n) {\n        return regex_locations[i];\n      }\n    }\n  }\n\n  return rc;\n}<\/code><\/pre>\n<h3>\u6848\u4f8b\u5206\u6790<\/h3>\n<h4>\u6848\u4f8b 1<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location \/doc {\n        return 701; # \u7528\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u77e5\u9053\u8bf7\u6c42\u5230\u4e86\u54ea\u91cc\n    }\n    location ~* ^\/document$ {\n        return 702; # \u7528\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u77e5\u9053\u8bf7\u6c42\u5230\u4e86\u54ea\u91cc\n\n    }\n}<\/code><\/pre>\n<p>curl -I  website.com:8080\/document<br \/>\nHTTP\/1.1 702<\/p>\n<p>\u6309\u7167\u4e0a\u8ff0\u7684\u89c4\u5219\uff0c\u7b2c\u4e8c\u4e2a\u4f1a\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7<\/p>\n<h4>\u6848\u4f8b 2<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location \/document {\n        return 701;\n    }\n    location ~* ^\/document$ {\n        return 702;\n    }\n}<\/code><\/pre>\n<p>curl -I  website.com:8080\/document<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u5339\u914d\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u7b2c\u4e00\u4e2a\u666e\u901a\u524d\u7f00\u5339\u914d<\/p>\n<h4>\u6848\u4f8b 3<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location ^~ \/doc {\n        return 701;\n    }\n    location ~* ^\/document$ {\n        return 702;\n    }\n}<\/code><\/pre>\n<p>curl <a href=\"http:\/\/website.com\/document\">http:\/\/website.com\/document<\/a><br \/>\nHTTP\/1.1 701<br \/>\n\u7b2c\u4e00\u4e2a\u524d\u7f00\u5339\u914d^~\u547d\u4e2d\u4ee5\u540e\u4e0d\u4f1a\u518d\u641c\u5bfb\u6b63\u5219\u5339\u914d\uff0c\u6240\u4ee5\u4f1a\u7b2c\u4e00\u4e2a\u547d\u4e2d<\/p>\n<h4>\u6848\u4f8b 4<\/h4>\n<pre><code>server {\n    server_name website.com;\n    location \/docu {\n        return 701;\n    }\n    location \/doc {\n        return 702;\n    }\n}<\/code><\/pre>\n<p>curl -I website.com:8080\/document \u8fd4\u56de HTTP\/1.1 701\uff0c<\/p>\n<pre><code>server {\n    server_name website.com;\n    location \/doc {\n        return 702;\n    }\n    location \/docu {\n        return 701;\n    }\n}<\/code><\/pre>\n<p>curl -I website.com:8080\/document \u4f9d\u7136\u8fd4\u56de HTTP\/1.1 701<br \/>\n\u524d\u7f00\u5339\u914d\u4e0b\uff0c\u8fd4\u56de\u6700\u957f\u5339\u914d\u7684 location\uff0c\u4e0e location \u6240\u5728\u4f4d\u7f6e\u987a\u5e8f\u65e0\u5173<\/p>\n<h4>\u6848\u4f8b 5<\/h4>\n<pre><code>server {\n    listen 8080;\n    server_name website.com;\n\n    location ~ ^\/doc[a-z]+ {\n        return 701;\n    }\n\n    location ~ ^\/docu[a-z]+ {\n        return 702;\n    }\n}<\/code><\/pre>\n<p>curl -I website.com:8080\/document \u8fd4\u56de HTTP\/1.1 701<\/p>\n<p>\u628a\u987a\u5e8f\u6362\u4e00\u4e0b<\/p>\n<pre><code>server {\n    listen 8080;\n    server_name website.com;\n\n    location ~ ^\/docu[a-z]+ {\n        return 702;\n    }\n\n    location ~ ^\/doc[a-z]+ {\n        return 701;\n    }\n}<\/code><\/pre>\n<p>curl -I website.com:8080\/document \u8fd4\u56de HTTP\/1.1 702<br \/>\n\u6b63\u5219\u5339\u914d\u662f\u4f7f\u7528\u6587\u4ef6\u4e2d\u7684\u987a\u5e8f\uff0c\u627e\u5230\u8fd4\u56de<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx \u7684 location \u5b9e\u73b0\u4e86\u5bf9\u8bf7\u6c42\u7684\u7ec6\u5206\u5904\u7406\uff0c\u6709\u4e9b URI \u8fd4\u56de\u9759\u6001\u5185\u5bb9\uff0c\u6709\u4e9b\u5206\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u7b49\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":4,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts\/95\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}