{"id":2594,"date":"2020-11-03T11:38:25","date_gmt":"2020-11-03T03:38:25","guid":{"rendered":"https:\/\/www.charflower.com\/?p=2594"},"modified":"2020-11-03T11:38:25","modified_gmt":"2020-11-03T03:38:25","slug":"nginx-%e9%9b%86%e7%be%a4%e6%9e%b6%e6%9e%84-4-%e5%9f%ba%e7%a1%80%e5%ba%94%e7%94%a8-location","status":"publish","type":"post","link":"https:\/\/www.charflower.com\/?p=2594","title":{"rendered":"Nginx-\u96c6\u7fa4\u67b6\u6784-4-\u57fa\u7840\u5e94\u7528-Location"},"content":{"rendered":"\n<h5><strong>\u56de\u987e\uff1a<\/strong><\/h5>\n\n\n\n<p><strong>\u5728\u4e0a\u4e00\u7bc7Nginx\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u505a\u8fc7\u4e86Nginx\u7684\u8f6e\u8be2\u3001\u52a0\u6743\u8f6e\u8be2\u3001IP\u5730\u5740\u54c8\u5e0c\uff0c\u5728\u672c\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8981\u53bb\u719f\u6089\u4e00\u4e0bNginx\u4e3b\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684Location\u53c2\u6570\uff0c\u6765\u505a\u4e00\u4e2a\u4e1a\u52a1\u7684\u52a8\u9759\u5206\u79bb\uff0c\u5b9e\u73b0\u7528\u6237\u8bbf\u95ee\u9759\u6001\u8d44\u6e90\u76f4\u63a5\u4eceNginx\u8fd4\u56de\uff0c\u52a8\u6001\u4ea4\u4e92\u5f0f\u7684\u9875\u9762\u5c31\u4ece\u540e\u7aefWeb\u670d\u52a1\u5668\u83b7\u53d6\uff1b<\/strong><\/p>\n\n\n\n<h5><strong>\u73af\u5883\u63cf\u8ff0\uff1a<\/strong><\/h5>\n\n\n\n<p><strong>\u5bf9\u4e8e\u73af\u5883\u6211\u4eec\u4f9d\u65e7\u662f\u4f7f\u7528\u4e0a\u4e2a\u6587\u7ae0\u4e2d\u505a\u4e86IP_HASH \u4e4b\u540e\u7684\u73af\u5883\uff0c\u5c31\u4e0d\u7528\u6062\u590d\u5230\u5feb\u7167&#8221;LAMP+Nginx\u96c6\u7fa4\u67b6\u6784-1&#8243;\u8fdb\u884c\u5b9e\u9a8c\u4e86\uff0c\u62d3\u6251\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"574\" src=\"http:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/1-1024x574.png\" alt=\"\" class=\"wp-image-2595\" srcset=\"https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/1-1024x574.png 1024w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/1-300x168.png 300w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/1-768x430.png 768w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/1.png 1378w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h5><strong>Nginx Location\u6df1\u5165\u89e3\u6790<\/strong><\/h5>\n\n\n\n<p>          <strong>Nginx \u7531\u5185\u6838\u548c\u6a21\u5757\u7ec4\u6210\uff0c\u5176\u4e2d\u5185\u6838\u7684\u8bbe\u8ba1\u975e\u5e38\u5fae\u5c0f\u548c\u7b80\u6d01\uff0c\u5b8c\u6210\u7684\u5de5\u4f5c\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u4ec5\u4ec5\u901a\u8fc7\u67e5\u627e\u914d\u7f6e\u6587\u4ef6\u5c06\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u6620\u5c04\u5230\u4e00\u4e2alocation block\uff0c\u800clocation\u662fNginx\u914d\u7f6e\u4e2d\u7684\u4e00\u4e2a\u6307\u4ee4\uff0c\u7528\u4e8e\u8bbf\u95ee\u7684URL\u5339\u914d\uff0c\u800c\u5728\u8fd9\u4e2alocation\u4e2d\u6240\u914d\u7f6e\u7684\u6bcf\u4e2a\u6307\u4ee4\u5c06\u4f1a\u542f\u52a8\u4e0d\u540c\u7684\u6a21\u5757\u53bb\u5b8c\u6210\u76f8\u5e94\u7684\u5de5\u4f5c\u3002<\/strong><\/p>\n\n\n\n<p><strong>        \u9ed8\u8ba4Nginx.conf\u914d\u7f6e\u6587\u4ef6\u4e2d\u81f3\u5c11\u5b58\u5728\u4e00\u4e2a&#8221;location \/&#8221; ,\u5373\u8868\u793a\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684URL\u4e3a\uff1a\u57df\u540d+\/\uff0c\u5982\u679c location \/newindex\/\uff0c\u5219\u8868\u793a\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684URL\u4e3a\uff1a\u57df\u540d+\/newindex\/\u3002\u5e38\u89c1location\u5339\u914dURL\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=                               \u5b57\u9762\u7cbe\u786e\u5339\u914d\uff1b\n^~                              \u6700\u5927\u524d\u7f00\u5339\u914d\uff1b\n\/                               \u4e0d\u5e26\u4efb\u4f55\u524d\u7f00\uff1b\n~                               \u5927\u5c0f\u5199\u76f8\u5173\u7684\u6b63\u5219\u5339\u914d\uff1b\n~*                              \u5927\u5c0f\u5199\u65e0\u5173\u7684\u6b63\u5219\u5339\u914d\uff1b\n@                               location\u5185\u90e8\u91cd\u5b9a\u5411\u7684\u53d8\u91cf\u3002<\/code><\/pre>\n\n\n\n<p>             <strong>\u5176\u4e2dlocation =\u3001^~\u3001 \/   \u5c5e\u4e8e\u666e\u901a\u5b57\u7b26\u4e32\u5339\u914d\uff0clocation ~ \u3001~*\u5c5e\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0clocation\u4f18\u5148\u7ea7\u4e0e\u5176\u5728Nginx.conf\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5148\u540e\u987a\u5e8f\u6ca1\u6709\u5173\u7cfb\uff1b<\/strong><\/p>\n\n\n\n<p><strong>            location = \u7cbe\u786e\u5339\u914d\u4f1a\u7b2c\u4e00\u4e2a\u88ab\u5904\u7406\uff0c\u5982\u679c\u53d1\u73b0\u7cbe\u786e\u5339\u914d\uff0cNginx\u5219\u505c\u6b62\u641c\u7d22\u5176\u4ed6\u4efb\u4f55location\u7684\u5339\u914d\u3002<\/strong><\/p>\n\n\n\n<p><strong>            \u666e\u901a\u5b57\u7b26\u5339\u914d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u89c4\u5219\u548c\u5b8c\u6574URL\u89c4\u5219\u5c06\u88ab\u4f18\u5148\u548c\u67e5\u8be2\u5339\u914d\uff0c^~\u4e3a\u6700\u5927\u524d\u7f00\u5339\u914d\uff0c\u5982\u679c\u5339\u914d\u5230\u8be5\u89c4\u5219\uff0cNginx\u5219\u505c\u6b62\u641c\u7d22\u5176\u4ed6\u4efb\u4f55location\u7684\u5339\u914d\uff0c\u5426\u5219nginx\u4f1a\u7ee7\u7eed\u5904\u7406\u5176\u4ed6location\u6307\u4ee4\u3002<\/strong><\/p>\n\n\n\n<p>           <strong>\u6b63\u5219\u5339\u914d &#8220;~&#8221; \u548c &#8220;~*&#8221; ,\u5982\u679c\u627e\u5230\u76f8\u5e94\u7684\u5339\u914d\uff0c\u5219Nginx\u505c\u6b62\u641c\u7d22\u5176\u4ed6\u4efb\u4f55location\u7684\u5339\u914d\uff1b\u5f53\u6ca1\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8005\u6ca1\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u88ab\u5339\u914d\u7684\u60c5\u51b5\u4e0b\uff0c\u90a3\u4e48\u5339\u914d\u7a0b\u5ea6\u6700\u9ad8\u7684\u9010\u5b57\u5339\u914d\u6307\u4ee4\u4f1a\u88ab\u4f7f\u7528\u3002<\/strong><\/p>\n\n\n\n<h5><strong>location\u89c4\u5219\u5339\u914d\u4f18\u5148\u7ea7\u603b\u7ed3\u5982\u4e0b\uff1a<\/strong><\/h5>\n\n\n\n<p>                <strong>\uff08location =\uff09 &gt; (location \u5b8c\u6574\u8def\u5f84) &gt; (location ^~  \u8def\u5f84) &gt; (location ~ | ~* \u6b63\u5219\u987a\u5e8f) &gt; ( location \u90e8\u5206\u8d77\u59cb\u8def\u5f84 ) &gt; (\/)  <\/strong><\/p>\n\n\n\n<h5><strong>\u5b9e\u9a8c\u52a8\u9759\u5206\u79bb\uff1a<\/strong><\/h5>\n\n\n\n<p><strong>\u5728\u865a\u62df\u4e3b\u673a\u914d\u7f6e\u6587\u4ef6bbs.zhanggeng.com\u4e2d\uff0c\u6dfb\u52a0\u6807\u7ea2\u90e8\u5206<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"972\" height=\"530\" src=\"http:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/2.png\" alt=\"\" class=\"wp-image-2596\" srcset=\"https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/2.png 972w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/2-300x164.png 300w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/2-768x419.png 768w\" sizes=\"(max-width: 972px) 100vw, 972px\" \/><figcaption> <strong>\u6ce8\u91ca\uff1a\u6807\u7ea2\u90e8\u5206\u8868\u793a\u4ee5\u4e0a\u8ff0\u540e\u7f00\u7ed3\u5c3e\u7684\u6587\u4ef6\u90fd\u4ece\/var\/www\/html\/discuz\u4e2d\u83b7\u53d6\uff1b<\/strong> <\/figcaption><\/figure>\n\n\n\n<p><strong>LB\u4e0a\u521b\u5efa\/var\/www\/html\/discuz\u76ee\u5f55\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;root@LB ~]# mkdir -p \/var\/www\/html\/discuz<\/code><\/pre>\n\n\n\n<p><strong>\u4eceLamp1\u4e0a\u5c06discuz\u7684\u6587\u4ef6\u90fdcp\u5230LB\u4e0a\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;root@Lamp1 ~]# cd \/var\/www\/html\/discuz\/\n&#091;root@Lamp1 discuz]# scp -r .\/* 192.168.169.201:\/var\/www\/html\/discuz\/<\/code><\/pre>\n\n\n\n<p><strong>\u590d\u5236\u4e86discuz\u7684\u5168\u90e8\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06PHP\u7684\u6587\u4ef6\u5220\u9664\u6389\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;root@LB ~]# find \/var\/www\/html\/discuz\/ -name \"*.php\" -exec rm -rf {} \\;<\/code><\/pre>\n\n\n\n<p><strong>\u91cd\u542fNginx\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;root@LB ~]# \/usr\/local\/nginx\/sbin\/nginx -s reload    <\/code><\/pre>\n\n\n\n<p><strong>\u8fdb\u884c\u53cd\u5411\u6d4b\u8bd5\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u5c06Lamp2\u4e2d\u7684Apahce\u5173\u95ed\uff0c\u5e76\u4e14\u5c06Lamp1\u4e2ddiscuz\u76ee\u5f55\u4e0b\u7684static\u76ee\u5f55\u5220\u6389\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;root@Lamp1 discuz]# cd \/var\/www\/html\/discuz\/\n&#091;root@Lamp1 discuz]# rm -rf static\/\n&#091;root@Lamp1 discuz]# systemctl retsart httpd<\/code><\/pre>\n\n\n\n<p><strong>\u6b64\u65f6\u5237\u65b0\u9875\u9762\u4f9d\u7136\u53ef\u4ee5\u6b63\u5e38\u8bbf\u95ee\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"630\" src=\"http:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/3-1024x630.png\" alt=\"\" class=\"wp-image-2597\" srcset=\"https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/3-1024x630.png 1024w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/3-300x185.png 300w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/3-768x473.png 768w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/3.png 1433w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h5><strong>\u8865\u5145\uff1a<\/strong><\/h5>\n\n\n\n<p><strong>php | jsp | cgi | do     \u52a8\u6001\u7684\u4e1c\u897f\u4f7f\u7528\u660e\u7ec6\u6307\u5411\u540e\u7aef\u7684\u9875\u9762\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u5f53\u6807\u7ea2\u90e8\u5206\u5339\u914d\u4e0d\u4e0a\uff0c\u624d\u4f1a\u53bb\u627e\u4e0a\u9762\u7684  location \/   <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1005\" height=\"717\" src=\"http:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/4.png\" alt=\"\" class=\"wp-image-2598\" srcset=\"https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/4.png 1005w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/4-300x214.png 300w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/4-768x548.png 768w, https:\/\/www.charflower.com\/wp-content\/uploads\/2020\/11\/4-100x70.png 100w\" sizes=\"(max-width: 1005px) 100vw, 1005px\" \/><\/figure>\n\n\n\n<h5><strong>\u00a0\u5982\u4e0b\u4e3aNginx Location\u89c4\u5219\u6848\u4f8b\u6f14\u793a\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>location  = \/ {\n  &#091; configuration  L1 ]\n  #\u53ea\u4f1a\u5339\u914d\/\uff0c\u4f18\u5148\u7ea7\u6bd4Location \/\u4f4e\u3002\n}\nlocation  = \/index.html {\n  &#091; configuration  L2 ]\n#\u53ea\u4f1a\u5339\u914d\/index.html\uff0c\u4f18\u5148\u7ea7\u6700\u9ad8\u3002\n}\nlocation  \/ {\n  &#091; configuration L3 ]\n  #\u5339\u914d\u4efb\u4f55\u8bf7\u6c42\uff0c\u56e0\u4e3a\u6240\u6709\u8bf7\u6c42\u90fd\u662f\u4ee5\"\/\"\u5f00\u59cb\uff1b\n  #\u4f46\u662f\u66f4\u957f\u5b57\u7b26\u5339\u914d\u6216\u8005\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4f1a\u4f18\u5148\u5339\u914d\uff0c\u4f18\u5148\u7ea7\u6700\u4f4e\u3002\n}\nlocation = \/images\/ {\n  &#091; configuration L4 ]\n  #\u5339\u914d\u4efb\u4f55\u4ee5\/images\/\u5f00\u59cb\u7684\u8bf7\u6c42\uff0c\u5e76\u505c\u6b62\u5339\u914d\u5176\u5b83location\uff1b\n}\nlocation ~* \\.(html|txt|gif|jpg|jpeg)$ {\n  &#091; configuration L5]\n  # \u5339\u914d\u4ee5html\u3001txt\u3001gif\u3001jpg\u3001jpeg\u7ed3\u5c3e\u7684URL\u6587\u4ef6\u8bf7\u6c42\uff1b\n  # \u4f46\u662f\u6240\u6709\/images\/\u76ee\u5f55\u7684\u8bf7\u6c42\u5c06\u7531 &#091;Configuration L4]\u5904\u7406\u3002\n}\n\u6d4f\u89c8\u5668\u53d1\u8d77HTTP Request URI\u6848\u4f8b\u4e0eLocation\u89c4\u5219\u6848\u4f8b\u5339\u914d\u5982\u4e0b\uff1a\n* \/ -&gt;\u5339\u914dconfiguration L3\uff1b\n* \/index.html\u5339\u914dconfiguration L2\uff1b\n* \/images\/\u5339\u914dconfiguration L4\uff1b\n* \/images\/logo.png\u5339\u914dconfiguration L4\uff1b\n* \/img\/test.jpg\u5339\u914dconfiguration L5\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u4f01\u4e1a\u751f\u4ea7\u73af\u5883\u4e2d\u65e0\u9700\u5728Nginx.conf\u914d\u7f6e\u6587\u4ef6\u4e2d\u540c\u65f6\u6dfb\u52a0\u4e94\u79cd\u89c4\u5219\u5339\u914d\uff0c\u5982\u4e0b\u4e3a\u4f01\u4e1a\u751f\u4ea7\u73af\u5883Nginx <\/strong><\/p>\n\n\n\n<p><strong>Location\u90e8\u5206\u914d\u7f6e\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>location \/\n{\n    root \/var\/www\/html\/\uff1b\n    expires      60d\uff1b\n}\nlocation ~ .*\\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$\n{\n    root \/var\/www\/html\/\uff1b\n    expires      60d\uff1b     \n}\nlocation ~ .*\\.(jsp|php|cgi|do)$\n{\n    root \/var\/www\/html\/\uff1b\n    proxy_pass http:\/\/linux_web\uff1b\n    proxy_http_version 1.1\uff1b\n    proxy_set_header Connection \"\"\uff1b\n    proxy_set_header Host  $host\uff1b\n    proxy_set_header X-Real-IP $remote_addr\uff1b\n    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for\uff1b   \n}\nlocation =\/newindex.html\n{\n    root \/var\/www\/newwww\/\uff1b\n    expires      60d\uff1b\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u56de\u987e\uff1a \u5728\u4e0a\u4e00\u7bc7Nginx\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u505a\u8fc7\u4e86Nginx\u7684\u8f6e\u8be2\u3001\u52a0\u6743\u8f6e\u8be2\u3001IP\u5730\u5740\u54c8\u5e0c\uff0c\u5728\u672c\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8981\u53bb\u719f\u6089&hellip; <a class=\"more-link\" href=\"https:\/\/www.charflower.com\/?p=2594\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Nginx-\u96c6\u7fa4\u67b6\u6784-4-\u57fa\u7840\u5e94\u7528-Location<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":2599,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[72],"tags":[45,92],"_links":{"self":[{"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/posts\/2594"}],"collection":[{"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.charflower.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2594"}],"version-history":[{"count":1,"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/posts\/2594\/revisions"}],"predecessor-version":[{"id":2600,"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/posts\/2594\/revisions\/2600"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.charflower.com\/index.php?rest_route=\/wp\/v2\/media\/2599"}],"wp:attachment":[{"href":"https:\/\/www.charflower.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.charflower.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.charflower.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}