{"id":50907,"date":"2024-10-08T14:37:16","date_gmt":"2024-10-08T06:37:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50907\/"},"modified":"2024-10-08T14:37:16","modified_gmt":"2024-10-08T06:37:16","slug":"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e8%a8%ad%e7%bd%ae-location-%e6%8c%87%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50907\/","title":{"rendered":"Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u8a2d\u7f6e location \u6307\u4ee4"},"content":{"rendered":"<h1 id=\"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e8%a8%ad%e7%bd%ae-location-%e6%8c%87%e4%bb%a4-XWGjeAZrle\">Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u8a2d\u7f6e location \u6307\u4ee4<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52d9\u3002\u5c0d\u65bc\u65b0\u624b\u4f86\u8aaa\uff0c\u7406\u89e3 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u7d50\u69cb\u53ca\u5176\u6307\u4ee4\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5176\u4e2d <code>location<\/code> \u6307\u4ee4\u662f\u6700\u5e38\u7528\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e <code>location<\/code> \u6307\u4ee4\u7684\u57fa\u672c\u7528\u6cd5\u53ca\u5176\u8a2d\u7f6e\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-location-%e6%8c%87%e4%bb%a4%ef%bc%9f-XWGjeAZrle\">\u4ec0\u9ebc\u662f location \u6307\u4ee4\uff1f<\/h2>\n<p><code>location<\/code> \u6307\u4ee4\u7528\u65bc\u5339\u914d\u8acb\u6c42\u7684 URI\uff0c\u4e26\u6839\u64da\u5339\u914d\u7d50\u679c\u57f7\u884c\u76f8\u61c9\u7684\u914d\u7f6e\u3002\u9019\u4f7f\u5f97 Nginx \u80fd\u5920\u6839\u64da\u4e0d\u540c\u7684\u8acb\u6c42\u8def\u5f91\u63d0\u4f9b\u4e0d\u540c\u7684\u8655\u7406\u65b9\u5f0f\uff0c\u4f8b\u5982\u975c\u614b\u6587\u4ef6\u3001\u4ee3\u7406\u8acb\u6c42\u6216\u91cd\u5b9a\u5411\u7b49\u3002<\/p>\n<h2 id=\"location-%e6%8c%87%e4%bb%a4%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-XWGjeAZrle\">location \u6307\u4ee4\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u57fa\u672c\u7684 <code>location<\/code> \u6307\u4ee4\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>location [modifier] uri {\n    # \u914d\u7f6e\u6307\u4ee4\n}<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>modifier<\/code> \u662f\u53ef\u9078\u7684\u4fee\u98fe\u7b26\uff0c<code>uri<\/code> \u662f\u8981\u5339\u914d\u7684\u8acb\u6c42\u8def\u5f91\u3002<\/p>\n<h2 id=\"location-%e6%8c%87%e4%bb%a4%e7%9a%84%e4%bf%ae%e9%a3%be%e7%ac%a6-XWGjeAZrle\">location \u6307\u4ee4\u7684\u4fee\u98fe\u7b26<\/h2>\n<p>\u5728\u4f7f\u7528 <code>location<\/code> \u6307\u4ee4\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u5e7e\u7a2e\u4fee\u98fe\u7b26\uff1a<\/p>\n<ul>\n<li><code>=<\/code>\uff1a\u7cbe\u78ba\u5339\u914d\uff0c\u53ea\u6709\u7576\u8acb\u6c42\u7684 URI \u5b8c\u5168\u7b49\u65bc\u6307\u5b9a\u7684 URI \u6642\uff0c\u624d\u6703\u4f7f\u7528\u8a72\u914d\u7f6e\u3002<\/li>\n<li><code>~<\/code>\uff1a\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff0c\u5340\u5206\u5927\u5c0f\u5beb\u3002<\/li>\n<li><code>~*<\/code>\uff1a\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff0c\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\u3002<\/li>\n<li><code>\/<\/code>\uff1a\u524d\u7db4\u5339\u914d\uff0c\u5339\u914d\u4ee5\u6307\u5b9a URI \u958b\u982d\u7684\u6240\u6709\u8acb\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"location-%e6%8c%87%e4%bb%a4%e7%9a%84%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8-XWGjeAZrle\">location \u6307\u4ee4\u7684\u5be6\u969b\u61c9\u7528<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684 <code>location<\/code> \u6307\u4ee4\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"1-%e7%b2%be%e7%a2%ba%e5%8c%b9%e9%85%8d-XWGjeAZrle\">1. \u7cbe\u78ba\u5339\u914d<\/h3>\n<pre><code>location = \/index.html {\n    root \/var\/www\/html;\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u8868\u793a\u7576\u8acb\u6c42\u7684 URI \u5b8c\u5168\u70ba <code>\/index.html<\/code> \u6642\uff0cNginx \u5c07\u5f9e <code>\/var\/www\/html<\/code> \u76ee\u9304\u63d0\u4f9b\u8a72\u6587\u4ef6\u3002<\/p>\n<h3 id=\"2-%e5%89%8d%e7%b6%b4%e5%8c%b9%e9%85%8d-XWGjeAZrle\">2. \u524d\u7db4\u5339\u914d<\/h3>\n<pre><code>location \/images\/ {\n    root \/var\/www\/html;\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u8868\u793a\u7576\u8acb\u6c42\u7684 URI \u4ee5 <code>\/images\/<\/code> \u958b\u982d\u6642\uff0cNginx \u5c07\u5f9e <code>\/var\/www\/html\/images\/<\/code> \u76ee\u9304\u63d0\u4f9b\u6587\u4ef6\u3002<\/p>\n<h3 id=\"3-%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f%e5%8c%b9%e9%85%8d-XWGjeAZrle\">3. \u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d<\/h3>\n<pre><code>location ~ .php$ {\n    include fastcgi_params;\n    fastcgi_pass 127.0.0.1:9000;\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u8868\u793a\u7576\u8acb\u6c42\u7684 URI \u4ee5 <code>.php<\/code> \u7d50\u5c3e\u6642\uff0cNginx \u5c07\u4f7f\u7528 FastCGI \u5c07\u8acb\u6c42\u8f49\u767c\u5230 PHP-FPM\u3002<\/p>\n<h2 id=\"%e5%a4%9a%e5%80%8b-location-%e6%8c%87%e4%bb%a4%e7%9a%84%e5%84%aa%e5%85%88%e7%b4%9a-XWGjeAZrle\">\u591a\u500b location \u6307\u4ee4\u7684\u512a\u5148\u7d1a<\/h2>\n<p>\u7576 Nginx \u540c\u6642\u5b58\u5728\u591a\u500b <code>location<\/code> \u6307\u4ee4\u6642\uff0c\u5339\u914d\u7684\u512a\u5148\u7d1a\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u7cbe\u78ba\u5339\u914d\uff08<code>=<\/code>\uff09<\/li>\n<li>\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff08<code>~<\/code> \u548c <code>~*<\/code>\uff09<\/li>\n<li>\u524d\u7db4\u5339\u914d\uff08<code>\/<\/code>\uff09<\/li>\n<\/ol>\n<p>\u9019\u610f\u5473\u8457\u5982\u679c\u6709\u591a\u500b <code>location<\/code> \u6307\u4ee4\u5339\u914d\u540c\u4e00\u8acb\u6c42\uff0cNginx \u5c07\u6839\u64da\u4e0a\u8ff0\u512a\u5148\u7d1a\u9078\u64c7\u6700\u5408\u9069\u7684\u914d\u7f6e\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XWGjeAZrle\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1 Nginx \u7684 <code>location<\/code> \u6307\u4ee4\u5c0d\u65bc\u6709\u6548\u914d\u7f6e\u4f3a\u670d\u5668\u81f3\u95dc\u91cd\u8981\u3002\u900f\u904e\u4e0d\u540c\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u9748\u6d3b\u5730\u8655\u7406\u5404\u7a2e\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u7db2\u7ad9\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Nginx \u4e2d\u8a2d\u7f6e location \u6307\u4ee4\uff0c\u638c\u63e1\u8def\u7531\u914d\u7f6e\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u5b89\u5168\u6027\uff0c\u9069\u5408\u65b0\u624b\u5165\u9580\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-50907","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=50907"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50907\/revisions"}],"predecessor-version":[{"id":50908,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50907\/revisions\/50908"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}