{"id":1406,"date":"2024-06-02T22:54:30","date_gmt":"2024-06-02T22:54:30","guid":{"rendered":""},"modified":"2024-06-05T06:12:19","modified_gmt":"2024-06-04T22:12:19","slug":"%e4%b8%80%e4%bb%bd%e7%ae%80%e5%8d%95%e5%a4%9f%e7%94%a8%e7%9a%84-nginx-location-%e9%85%8d%e7%bd%ae%e8%ae%b2%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.lccc.top\/?p=1406","title":{"rendered":"\u4e00\u4efd\u7b80\u5355\u591f\u7528\u7684 Nginx Location \u914d\u7f6e\u8bb2\u89e3"},"content":{"rendered":"<td class=\"d-block comment-body markdown-body  js-comment-body\">\n<h2 dir=\"auto\">\u524d\u8a00<\/h2>\n<p dir=\"auto\">Location \u662f Nginx \u4e2d\u4e00\u4e2a\u975e\u5e38\u6838\u5fc3\u7684\u914d\u7f6e\uff0c\u8fd9\u7bc7\u91cd\u70b9\u8bb2\u89e3\u4e00\u4e0b Location \u7684\u914d\u7f6e\u95ee\u9898\u4ee5\u53ca\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<h2 dir=\"auto\">\u8bed\u6cd5<\/h2>\n<p dir=\"auto\">\u5173\u4e8e Location\uff0c\u4e3e\u4e2a\u7b80\u5355\u7684\u914d\u7f6e\u4f8b\u5b50\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"http { \n  server {\n      listen 80;\n    \tserver_name www.yayujs.com;\n    \tlocation \/ {\n      \troot \/home\/www\/ts\/;\n\t      index index.html;\n    \t}\n  }\n}\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">http<\/span> { \n  <span class=\"pl-k\">server<\/span> {\n      <span class=\"pl-k\">listen<\/span> <span class=\"pl-s\">80<\/span>;\n    \t<span class=\"pl-k\">server_name<\/span> www.yayujs.com;\n    \t<span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/ <\/span>{\n      \t<span class=\"pl-k\">root<\/span> \/home\/www\/ts\/;\n\t      <span class=\"pl-k\">index<\/span> index.html;\n    \t}\n  }\n}<\/pre>\n<\/div>\n<p dir=\"auto\">\u5927\u81f4\u7684\u610f\u601d\u662f\uff0c\u5f53\u4f60\u8bbf\u95ee <code class=\"notranslate\">www.yayujs.com<\/code> \u7684 <code class=\"notranslate\">80<\/code> \u7aef\u53e3\u7684\u65f6\u5019\uff0c\u8fd4\u56de <code class=\"notranslate\">\/home\/www\/ts\/index.html<\/code> \u6587\u4ef6\u3002<\/p>\n<p dir=\"auto\">\u6211\u4eec\u770b\u4e0b Location \u7684\u5177\u4f53\u8bed\u6cd5\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location [ = | ~ | ~* | ^~ ] uri { ... }\">\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">[ = | ~ | ~* | ^~ ] uri <\/span>{ ... }<\/pre>\n<\/div>\n<p dir=\"auto\">\u91cd\u70b9\u770b\u65b9\u62ec\u53f7\u4e2d\u7684 <code class=\"notranslate\">[ = | ~ | ~* | ^~ ]<\/code>\uff0c\u5176\u4e2d <code class=\"notranslate\">|<\/code> \u5206\u9694\u7684\u5185\u5bb9\u8868\u793a\u4f60\u53ef\u80fd\u4f1a\u7528\u5230\u7684\u8bed\u6cd5\uff0c\u5176\u4e2d\uff1a<\/p>\n<ul dir=\"auto\">\n<li><code class=\"notranslate\">=<\/code> \u8868\u793a\u7cbe\u786e\u5339\u914d\uff0c\u6bd4\u5982\uff1a<\/li>\n<\/ul>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content='location = \/test {\n  return 200 \"hello\";\n}\n\n# \/test ok\n# \/test\/ not ok\n# \/test2 not ok\n# \/test\/2 not ok'><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">= \/test <\/span>{\n  <span class=\"pl-c1\">return<\/span> <span class=\"pl-s\">200<\/span> <span class=\"pl-s\">\"hello\"<\/span>;\n}\n\n<span class=\"pl-c\"># \/test ok<\/span>\n<span class=\"pl-c\"># \/test\/ not ok<\/span>\n<span class=\"pl-c\"># \/test2 not ok<\/span>\n<span class=\"pl-c\"># \/test\/2 not ok<\/span><\/pre>\n<\/div>\n<ul dir=\"auto\">\n<li><code class=\"notranslate\">~<\/code> \u8868\u793a\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d\uff0c\u6bd4\u5982\uff1a<\/li>\n<\/ul>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location ~ ^\/test$ {\n  [ configuration ] \n}\n\n# \/test ok\n# \/Test not ok\n# \/test\/ not ok\n# \/test2 not ok\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> ~ <span class=\"pl-sr\">^\/test$ <\/span>{\n  [ <span class=\"pl-k\">configuration<\/span> ] \n}\n\n<span class=\"pl-c\"># \/test ok<\/span>\n<span class=\"pl-c\"># \/Test not ok<\/span>\n<span class=\"pl-c\"># \/test\/ not ok<\/span>\n<span class=\"pl-c\"># \/test2 not ok<\/span><\/pre>\n<\/div>\n<ul dir=\"auto\">\n<li><code class=\"notranslate\">~*<\/code> \u8868\u793a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d<\/li>\n<\/ul>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location ~* ^\/test$ {     \n\t[ configuration ] \n}\n\n# \/test ok\n# \/Test ok\n# \/test\/ not ok\n# \/test2 not ok\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> ~* <span class=\"pl-sr\">^\/test$ <\/span>{     \n\t[ <span class=\"pl-k\">configuration<\/span> ] \n}\n\n<span class=\"pl-c\"># \/test ok<\/span>\n<span class=\"pl-c\"># \/Test ok<\/span>\n<span class=\"pl-c\"># \/test\/ not ok<\/span>\n<span class=\"pl-c\"># \/test2 not ok<\/span><\/pre>\n<\/div>\n<ul dir=\"auto\">\n<li><code class=\"notranslate\">^~<\/code> \u8868\u793a uri \u4ee5\u67d0\u4e2a\u5b57\u7b26\u4e32\u5f00\u5934<\/li>\n<\/ul>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location ^~ \/images\/ {    \n\t[ configuration ] \n}\n\n# \/images\/1.gif ok\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> ^~ <span class=\"pl-sr\">\/images\/ <\/span>{    \n\t[ <span class=\"pl-k\">configuration<\/span> ] \n}\n\n<span class=\"pl-c\"># \/images\/1.gif ok<\/span><\/pre>\n<\/div>\n<p dir=\"auto\">\u800c\u5f53\u4f60\u4e0d\u4f7f\u7528\u8fd9\u4e9b\u8bed\u6cd5\u7684\u65f6\u5019\uff0c\u53ea\u5199 uri \u7684\u65f6\u5019\uff1a<\/p>\n<p dir=\"auto\"><code class=\"notranslate\">\/<\/code> \u8868\u793a\u901a\u7528\u5339\u914d\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location \/ {     \n\t[ configuration ] \n}\n\n# \/index.html ok\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/ <\/span>{     \n\t[ <span class=\"pl-k\">configuration<\/span> ] \n}\n\n<span class=\"pl-c\"># \/index.html ok<\/span><\/pre>\n<\/div>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location \/test {\n    [ configuration ] \n}\n\n# \/test ok\n# \/test2 ok\n# \/test\/ ok\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/test <\/span>{\n    [ <span class=\"pl-k\">configuration<\/span> ] \n}\n\n<span class=\"pl-c\"># \/test ok<\/span>\n<span class=\"pl-c\"># \/test2 ok<\/span>\n<span class=\"pl-c\"># \/test\/ ok<\/span><\/pre>\n<\/div>\n<h2 dir=\"auto\">\u5339\u914d\u987a\u5e8f<\/h2>\n<p dir=\"auto\">\u5f53\u5b58\u5728\u591a\u4e2a location \u7684\u65f6\u5019\uff0c\u4ed6\u4eec\u7684\u5339\u914d\u987a\u5e8f\u5f15\u7528 <a href=\"http:\/\/nginx.org\/en\/docs\/http\/ngx_http_core_module.html#location\" rel=\"nofollow\">Nginx \u5b98\u65b9\u6587\u6863<\/a>\u5c31\u662f\uff1a<\/p>\n<blockquote>\n<p dir=\"auto\">A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding \u201c<del>*\u201d modifier (for case-insensitive matching), or the \u201c<\/del>\u201d modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.<\/p>\n<\/blockquote>\n<blockquote>\n<p dir=\"auto\">If the longest matching prefix location has the \u201c^~\u201d modifier then regular expressions are not checked.<\/p>\n<\/blockquote>\n<blockquote>\n<p dir=\"auto\">Also, using the \u201c=\u201d modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a \u201c\/\u201d request happens frequently, defining \u201clocation = \/\u201d will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.<\/p>\n<\/blockquote>\n<p dir=\"auto\">\u7ffb\u8bd1\u6574\u7406\u540e\u5c31\u662f\uff1a<\/p>\n<p dir=\"auto\">location \u7684\u5b9a\u4e49\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n<ul dir=\"auto\">\n<li>\u524d\u7f00\u5b57\u7b26\u4e32\uff08prefix string\uff09<\/li>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regular expression\uff09\uff0c\u5177\u4f53\u4e3a\u524d\u9762\u5e26 <code class=\"notranslate\">~*<\/code> \u548c <code class=\"notranslate\">~<\/code> \u4fee\u9970\u7b26\u7684<\/li>\n<\/ul>\n<p dir=\"auto\">\u800c\u5339\u914d location \u7684\u987a\u5e8f\u4e3a\uff1a<\/p>\n<ol dir=\"auto\">\n<li>\u68c0\u67e5\u4f7f\u7528\u524d\u7f00\u5b57\u7b26\u4e32\u7684 locations\uff0c\u5728\u4f7f\u7528\u524d\u7f00\u5b57\u7b26\u4e32\u7684 locations \u4e2d\u9009\u62e9\u6700\u957f\u5339\u914d\u7684\uff0c\u5e76\u5c06\u7ed3\u679c\u8fdb\u884c\u50a8\u5b58<\/li>\n<li>\u5982\u679c\u7b26\u5408\u5e26\u6709 <code class=\"notranslate\">=<\/code> \u4fee\u9970\u7b26\u7684 URI\uff0c\u5219\u7acb\u523b\u505c\u6b62\u5339\u914d<\/li>\n<li>\u5982\u679c\u7b26\u5408\u5e26\u6709  <code class=\"notranslate\">^~<\/code> \u4fee\u9970\u7b26\u7684 URI\uff0c\u5219\u4e5f\u7acb\u523b\u505c\u6b62\u5339\u914d\u3002<\/li>\n<li>\u7136\u540e\u6309\u7167\u5b9a\u4e49\u6587\u4ef6\u7684\u987a\u5e8f\uff0c\u68c0\u67e5\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5339\u914d\u5230\u5c31\u505c\u6b62<\/li>\n<li>\u5f53\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u5230\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4e4b\u524d\u50a8\u5b58\u7684\u524d\u7f00\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p dir=\"auto\">\u518d\u603b\u7ed3\u4e00\u4e0b\u5c31\u662f\uff1a<\/p>\n<p dir=\"auto\">\u5728\u987a\u5e8f\u4e0a\uff0c\u524d\u7f00\u5b57\u7b26\u4e32\u987a\u5e8f\u4e0d\u91cd\u8981\uff0c\u6309\u7167\u5339\u914d\u957f\u5ea6\u6765\u786e\u5b9a\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u6309\u7167\u5b9a\u4e49\u987a\u5e8f\u3002<\/p>\n<p dir=\"auto\">\u5728\u4f18\u5148\u7ea7\u4e0a\uff0c<code class=\"notranslate\">=<\/code> \u4fee\u9970\u7b26\u6700\u9ad8\uff0c<code class=\"notranslate\">^~<\/code> \u6b21\u4e4b\uff0c\u518d\u8005\u662f\u6b63\u5219\uff0c\u6700\u540e\u662f\u524d\u7f00\u5b57\u7b26\u4e32\u5339\u914d\u3002<\/p>\n<p dir=\"auto\">\u6211\u4eec\u4e3e\u51e0\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u590d\u4e60\u4e0b\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"server {\n    location \/doc {\n        [ configuration A ] \n    }\n    location \/docu {\n        [ configuration B ] \n    }\n}\n\n# \u8bf7\u6c42 \/document \u4f7f\u7528 configuration B\n# \u867d\u7136 \/doc \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46\u5728\u987a\u5e8f\u4e0a\uff0c\u524d\u7f00\u5b57\u7b26\u4e32\u987a\u5e8f\u4e0d\u91cd\u8981\uff0c\u6309\u7167\u5339\u914d\u957f\u5ea6\u6765\u786e\u5b9a\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">server<\/span> {\n    <span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/doc <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> A ] \n    }\n    <span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/docu <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> B ] \n    }\n}\n\n<span class=\"pl-c\"># \u8bf7\u6c42 \/document \u4f7f\u7528 configuration B<\/span>\n<span class=\"pl-c\"># \u867d\u7136 \/doc \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46\u5728\u987a\u5e8f\u4e0a\uff0c\u524d\u7f00\u5b57\u7b26\u4e32\u987a\u5e8f\u4e0d\u91cd\u8981\uff0c\u6309\u7167\u5339\u914d\u957f\u5ea6\u6765\u786e\u5b9a<\/span><\/pre>\n<\/div>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"server {\n    location ~ ^\/doc {\n        [ configuration A ] \n    }\n    location ~ ^\/docu {\n        [ configuration B ] \n    }\n}\n\n# \u8bf7\u6c42 \/document \u4f7f\u7528 configuration A\n# \u867d\u7136 ~ ^\/docu \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u6309\u7167\u5b9a\u4e49\u987a\u5e8f\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">server<\/span> {\n    <span class=\"pl-k\">location<\/span> ~ <span class=\"pl-sr\">^\/doc <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> A ] \n    }\n    <span class=\"pl-k\">location<\/span> ~ <span class=\"pl-sr\">^\/docu <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> B ] \n    }\n}\n\n<span class=\"pl-c\"># \u8bf7\u6c42 \/document \u4f7f\u7528 configuration A<\/span>\n<span class=\"pl-c\"># \u867d\u7136 ~ ^\/docu \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u6309\u7167\u5b9a\u4e49\u987a\u5e8f<\/span><\/pre>\n<\/div>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"server {\n    location ^~ \/doc {\n        [ configuration A ] \n    }\n    location ~ ^\/docu {\n        [ configuration B ] \n    }\n}\n\n# \u8bf7\u6c42 \/document \u4f7f\u7528 configuration A\n# \u867d\u7136 ~ ^\/docu \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46 ^~ \u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">server<\/span> {\n    <span class=\"pl-k\">location<\/span> ^~ <span class=\"pl-sr\">\/doc <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> A ] \n    }\n    <span class=\"pl-k\">location<\/span> ~ <span class=\"pl-sr\">^\/docu <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> B ] \n    }\n}\n\n<span class=\"pl-c\"># \u8bf7\u6c42 \/document \u4f7f\u7528 configuration A<\/span>\n<span class=\"pl-c\"># \u867d\u7136 ~ ^\/docu \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46 ^~ \u7684\u4f18\u5148\u7ea7\u66f4\u9ad8<\/span><\/pre>\n<\/div>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"server {\n    location \/document {\n        [ configuration A ] \n    }\n    location ~ ^\/docu {\n        [ configuration B ] \n    }\n}\n\n# \u8bf7\u6c42 \/document \u4f7f\u7528 configuration B\n# \u867d\u7136 \/document \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46\u6b63\u5219\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">server<\/span> {\n    <span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/document <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> A ] \n    }\n    <span class=\"pl-k\">location<\/span> ~ <span class=\"pl-sr\">^\/docu <\/span>{\n        [ <span class=\"pl-k\">configuration<\/span> B ] \n    }\n}\n\n<span class=\"pl-c\"># \u8bf7\u6c42 \/document \u4f7f\u7528 configuration B<\/span>\n<span class=\"pl-c\"># \u867d\u7136 \/document \u4e5f\u80fd\u5339\u914d\u5230\uff0c\u4f46\u6b63\u5219\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8<\/span><\/pre>\n<\/div>\n<h2 dir=\"auto\">root \u4e0e alias \u7684\u533a\u522b<\/h2>\n<p dir=\"auto\">\u5f53\u6211\u4eec\u8fd9\u6837\u8bbe\u7f6e <code class=\"notranslate\">root<\/code> \u7684\u65f6\u5019\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location \/i\/ {\n    root \/data\/w3;\n}\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/i\/ <\/span>{\n    <span class=\"pl-k\">root<\/span> \/data\/w3;\n}<\/pre>\n<\/div>\n<p dir=\"auto\">\u5f53\u8bf7\u6c42 <code class=\"notranslate\">\/i\/top.gif<\/code> \uff0c<code class=\"notranslate\">\/data\/w3\/i\/top.gif<\/code> \u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n<p dir=\"auto\">\u5f53\u6211\u4eec\u8fd9\u6837\u8bbe\u7f6e <code class=\"notranslate\">alias<\/code> \u7684\u65f6\u5019\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location \/i\/ {\n    alias \/data\/w3\/images\/;\n}\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/i\/ <\/span>{\n    <span class=\"pl-k\">alias<\/span> \/data\/w3\/images\/;\n}<\/pre>\n<\/div>\n<p dir=\"auto\">\u5f53\u8bf7\u6c42 <code class=\"notranslate\">\/i\/top.gif<\/code> \uff0c<code class=\"notranslate\">\/data\/w3\/images\/top.gif<\/code> \u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n<p dir=\"auto\">\u4e4d\u4e00\u770b\u4e24\u8005\u5f88\u50cf\uff0c\u4f46\u7ec6\u4e00\u770b\uff0c\u5c31\u80fd\u770b\u51fa\u4e24\u8005\u7684\u533a\u522b\uff0croot \u662f\u76f4\u63a5\u62fc\u63a5 <code class=\"notranslate\">root<\/code> + <code class=\"notranslate\">location<\/code> \u800c alias \u662f\u7528 <code class=\"notranslate\">alias<\/code> \u66ff\u6362 <code class=\"notranslate\">location<\/code>\uff0c\u6240\u4ee5 root \u4e2d\u6700\u540e\u7684\u8def\u5f84\u91cc\u6709 <code class=\"notranslate\">\/i\/<\/code>\uff0c\u800c alias \u4e2d\u6700\u540e\u7684\u8def\u5f84\u91cc\u6ca1\u6709  <code class=\"notranslate\">\/i\/<\/code> \u3002<\/p>\n<p dir=\"auto\">\u6240\u4ee5\u5982\u679c\u4f60\u8fd9\u6837\u4f7f\u7528 allias \u5b9a\u4e49\u4e00\u4e2a\u8def\u5f84\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location \/images\/ {\n    alias \/data\/w3\/images\/;\n}\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/images\/ <\/span>{\n    <span class=\"pl-k\">alias<\/span> \/data\/w3\/images\/;\n}<\/pre>\n<\/div>\n<p dir=\"auto\">\u5176\u5b9e\u4f7f\u7528 root \u4f1a\u66f4\u597d\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"location \/images\/ {\n    root \/data\/w3;\n}\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/images\/ <\/span>{\n    <span class=\"pl-k\">root<\/span> \/data\/w3;\n}<\/pre>\n<\/div>\n<h2 dir=\"auto\">server \u548c location \u4e2d\u7684 root<\/h2>\n<p dir=\"auto\">server \u548c location \u4e2d\u90fd\u53ef\u4ee5\u4f7f\u7528 root\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<div class=\"highlight highlight-source-nginx notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"http { \n  server {\n      listen 80;\n    \tserver_name www.yayujs.com;\n    \troot \/home\/www\/website\/;\n    \tlocation \/ {\n      \troot \/home\/www\/ts\/;\n\t      index index.html;\n    \t}\n  }\n}\"><\/p>\n<pre class=\"notranslate\"><span class=\"pl-k\">http<\/span> { \n  <span class=\"pl-k\">server<\/span> {\n      <span class=\"pl-k\">listen<\/span> <span class=\"pl-s\">80<\/span>;\n    \t<span class=\"pl-k\">server_name<\/span> www.yayujs.com;\n    \t<span class=\"pl-k\">root<\/span> \/home\/www\/website\/;\n    \t<span class=\"pl-k\">location<\/span> <span class=\"pl-en\">\/ <\/span>{\n      \t<span class=\"pl-k\">root<\/span> \/home\/www\/ts\/;\n\t      <span class=\"pl-k\">index<\/span> index.html;\n    \t}\n  }\n}<\/pre>\n<\/div>\n<p dir=\"auto\">\u5982\u679c\u4e24\u8005\u90fd\u51fa\u73b0\uff0c\u662f\u600e\u6837\u7684\u4f18\u5148\u7ea7\u5462\uff1f<\/p>\n<p dir=\"auto\">\u7b80\u5355\u7684\u6765\u8bf4\uff0c\u5c31\u662f\u5c31\u8fd1\u539f\u5219\uff0c\u5982\u679c location \u4e2d\u80fd\u5339\u914d\u5230\uff0c\u5c31\u662f\u7528 location \u4e2d\u7684 root \u914d\u7f6e\uff0c\u5ffd\u7565 server \u4e2d\u7684 root\uff0c\u5f53 location \u4e2d\u5339\u914d\u4e0d\u5230\u7684\u65f6\u5019\uff0c\u5219\u4f7f\u7528 server \u4e2d\u7684 root \u914d\u7f6e\u3002<\/p>\n<h2 dir=\"auto\">\u7cfb\u5217\u6587\u7ae0<\/h2>\n<p dir=\"auto\">\u535a\u5ba2\u642d\u5efa\u7cfb\u5217\u662f\u6211\u81f3\u4eca\u5199\u7684\u552f\u4e00\u4e00\u4e2a\u504f\u5b9e\u6218\u7684\u7cfb\u5217\u6559\u7a0b\uff0c\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 VuePress \u642d\u5efa\u535a\u5ba2\uff0c\u5e76\u90e8\u7f72\u5230 GitHub\u3001Gitee\u3001\u4e2a\u4eba\u670d\u52a1\u5668\u7b49\u5e73\u53f0\u3002<\/p>\n<ol dir=\"auto\">\n<li><a href=\"https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/235\" data-hovercard-type=\"issue\" data-hovercard-url=\"\/mqyqingfeng\/Blog\/issues\/235\/hovercard\">\u4e00\u7bc7\u5e26\u4f60\u7528 VuePress + GitHub Pages \u642d\u5efa\u535a\u5ba2<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/236\" data-hovercard-type=\"issue\" data-hovercard-url=\"\/mqyqingfeng\/Blog\/issues\/236\/hovercard\">\u4e00\u7bc7\u6559\u4f60\u4ee3\u7801\u540c\u6b65 GitHub \u548c Gitee<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/237\" data-hovercard-type=\"issue\" data-hovercard-url=\"\/mqyqingfeng\/Blog\/issues\/237\/hovercard\">\u8fd8\u4e0d\u4f1a\u7528 GitHub Actions \uff1f\u770b\u770b\u8fd9\u7bc7<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/238\" data-hovercard-type=\"issue\" data-hovercard-url=\"\/mqyqingfeng\/Blog\/issues\/238\/hovercard\">Gitee \u5982\u4f55\u81ea\u52a8\u90e8\u7f72 Pages\uff1f\u8fd8\u662f\u7528 GitHub Actions!<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/239\" data-hovercard-type=\"issue\" data-hovercard-url=\"\/mqyqingfeng\/Blog\/issues\/239\/hovercard\">\u4e00\u4efd\u524d\u7aef\u591f\u7528\u7684 Linux \u547d\u4ee4<\/a><\/li>\n<\/ol>\n<p dir=\"auto\">\u5fae\u4fe1\uff1a\u300cmqyqingfeng\u300d\uff0c\u52a0\u6211\u8fdb\u51b4\u7fbd\u552f\u4e00\u7684\u8bfb\u8005\u7fa4\u3002<\/p>\n<p dir=\"auto\">\u5982\u679c\u6709\u9519\u8bef\u6216\u8005\u4e0d\u4e25\u8c28\u7684\u5730\u65b9\uff0c\u8bf7\u52a1\u5fc5\u7ed9\u4e88\u6307\u6b63\uff0c\u5341\u5206\u611f\u8c22\u3002\u5982\u679c\u559c\u6b22\u6216\u8005 \u6709\u6240\u542f\u53d1\uff0c\u6b22\u8fce star\uff0c\u5bf9\u4f5c\u8005\u4e5f\u662f\u4e00\u79cd\u9f13\u52b1\u3002<\/p>\n<\/td>\n<p>\u6587\u7ae0\u8f6c\u8f7d\u81ea<a href='https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/242'>https:\/\/github.com\/mqyqingfeng\/Blog\/issues\/242<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 Location \u662f Nginx \u4e2d\u4e00\u4e2a\u975e\u5e38\u6838\u5fc3\u7684\u914d\u7f6e\uff0c\u8fd9\u7bc7\u91cd\u70b9\u8bb2\u89e3\u4e00\u4e0b Location \u7684\u914d\u7f6e\u95ee\u9898\u4ee5\u53ca\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u3002 \u8bed\u6cd5 \u5173\u4e8e Location\uff0c\u4e3e&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.lccc.top\/?p=1406\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1406","post","type-post","status-publish","format-standard","hentry","category-learn"],"_links":{"self":[{"href":"https:\/\/www.lccc.top\/index.php?rest_route=\/wp\/v2\/posts\/1406"}],"collection":[{"href":"https:\/\/www.lccc.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lccc.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lccc.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lccc.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1406"}],"version-history":[{"count":1,"href":"https:\/\/www.lccc.top\/index.php?rest_route=\/wp\/v2\/posts\/1406\/revisions"}],"predecessor-version":[{"id":2388,"href":"https:\/\/www.lccc.top\/index.php?rest_route=\/wp\/v2\/posts\/1406\/revisions\/2388"}],"wp:attachment":[{"href":"https:\/\/www.lccc.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lccc.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lccc.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}