{"id":6266,"date":"2017-07-19T12:52:45","date_gmt":"2017-07-19T04:52:45","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6266"},"modified":"2017-07-19T12:56:10","modified_gmt":"2017-07-19T04:56:10","slug":"nginx%e9%85%8d%e7%bd%aelocation%e6%80%bb%e7%bb%93%e5%8f%8arewrite%e8%a7%84%e5%88%99%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6266.html","title":{"rendered":"nginx\u914d\u7f6elocation\u603b\u7ed3\u53carewrite\u89c4\u5219\u5199\u6cd5"},"content":{"rendered":"<h1>1. location\u6b63\u5219\u5199\u6cd5<\/h1>\n<p>\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nlocation  = \/ {\r\n  # \u7cbe\u786e\u5339\u914d \/ \uff0c\u4e3b\u673a\u540d\u540e\u9762\u4e0d\u80fd\u5e26\u4efb\u4f55\u5b57\u7b26\u4e32\r\n  &#x5B; configuration A ]\r\n}\r\nlocation  \/ {\r\n  # \u56e0\u4e3a\u6240\u6709\u7684\u5730\u5740\u90fd\u4ee5 \/ \u5f00\u5934\uff0c\u6240\u4ee5\u8fd9\u6761\u89c4\u5219\u5c06\u5339\u914d\u5230\u6240\u6709\u8bf7\u6c42\r\n  # \u4f46\u662f\u6b63\u5219\u548c\u6700\u957f\u5b57\u7b26\u4e32\u4f1a\u4f18\u5148\u5339\u914d\r\n  &#x5B; configuration B ]\r\n}\r\nlocation \/documents\/ {\r\n  # \u5339\u914d\u4efb\u4f55\u4ee5 \/documents\/ \u5f00\u5934\u7684\u5730\u5740\uff0c\u5339\u914d\u7b26\u5408\u4ee5\u540e\uff0c\u8fd8\u8981\u7ee7\u7eed\u5f80\u4e0b\u641c\u7d22\r\n  # \u53ea\u6709\u540e\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6ca1\u6709\u5339\u914d\u5230\u65f6\uff0c\u8fd9\u4e00\u6761\u624d\u4f1a\u91c7\u7528\u8fd9\u4e00\u6761\r\n  &#x5B; configuration C ]\r\n}\r\nlocation ~ \/documents\/Abc {\r\n  # \u5339\u914d\u4efb\u4f55\u4ee5 \/documents\/Abc \u5f00\u5934\u7684\u5730\u5740\uff0c\u5339\u914d\u7b26\u5408\u4ee5\u540e\uff0c\u8fd8\u8981\u7ee7\u7eed\u5f80\u4e0b\u641c\u7d22\r\n  # \u53ea\u6709\u540e\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6ca1\u6709\u5339\u914d\u5230\u65f6\uff0c\u8fd9\u4e00\u6761\u624d\u4f1a\u91c7\u7528\u8fd9\u4e00\u6761\r\n  &#x5B; configuration CC ]\r\n}\r\nlocation ^~ \/images\/ {\r\n  # \u5339\u914d\u4efb\u4f55\u4ee5 \/images\/ \u5f00\u5934\u7684\u5730\u5740\uff0c\u5339\u914d\u7b26\u5408\u4ee5\u540e\uff0c\u505c\u6b62\u5f80\u4e0b\u641c\u7d22\u6b63\u5219\uff0c\u91c7\u7528\u8fd9\u4e00\u6761\u3002\r\n  &#x5B; configuration D ]\r\n}\r\nlocation ~* \\.(gif|jpg|jpeg)$ {\r\n  # \u5339\u914d\u6240\u6709\u4ee5 gif,jpg\u6216jpeg \u7ed3\u5c3e\u7684\u8bf7\u6c42\r\n  # \u7136\u800c\uff0c\u6240\u6709\u8bf7\u6c42 \/images\/ \u4e0b\u7684\u56fe\u7247\u4f1a\u88ab config D \u5904\u7406\uff0c\u56e0\u4e3a ^~ \u5230\u8fbe\u4e0d\u4e86\u8fd9\u4e00\u6761\u6b63\u5219\r\n  &#x5B; configuration E ]\r\n}\r\nlocation \/images\/ {\r\n  # \u5b57\u7b26\u5339\u914d\u5230 \/images\/\uff0c\u7ee7\u7eed\u5f80\u4e0b\uff0c\u4f1a\u53d1\u73b0 ^~ \u5b58\u5728\r\n  &#x5B; configuration F ]\r\n}\r\nlocation \/images\/abc {\r\n  # \u6700\u957f\u5b57\u7b26\u5339\u914d\u5230 \/images\/abc\uff0c\u7ee7\u7eed\u5f80\u4e0b\uff0c\u4f1a\u53d1\u73b0 ^~ \u5b58\u5728\r\n  # F\u4e0eG\u7684\u653e\u7f6e\u987a\u5e8f\u662f\u6ca1\u6709\u5173\u7cfb\u7684\r\n  &#x5B; configuration G ]\r\n}\r\nlocation ~ \/images\/abc\/ {\r\n  # \u53ea\u6709\u53bb\u6389 config D \u624d\u6709\u6548\uff1a\u5148\u6700\u957f\u5339\u914d config G \u5f00\u5934\u7684\u5730\u5740\uff0c\u7ee7\u7eed\u5f80\u4e0b\u641c\u7d22\uff0c\u5339\u914d\u5230\u8fd9\u4e00\u6761\u6b63\u5219\uff0c\u91c7\u7528\r\n    &#x5B; configuration H ]\r\n}\r\nlocation ~* \/js\/.*\/\\.js\r\n<\/pre>\n<ul>\n<li>\u4ee5=\u5f00\u5934\u8868\u793a\u7cbe\u786e\u5339\u914d \u3002\u5982 A \u4e2d\u53ea\u5339\u914d\u6839\u76ee\u5f55\u7ed3\u5c3e\u7684\u8bf7\u6c42\uff0c\u540e\u9762\u4e0d\u80fd\u5e26\u4efb\u4f55\u5b57\u7b26\u4e32\u3002<\/li>\n<li>^~ \u5f00\u5934\u8868\u793auri\u4ee5\u67d0\u4e2a\u5e38\u89c4\u5b57\u7b26\u4e32\u5f00\u5934\uff0c\u4e0d\u662f\u6b63\u5219\u5339\u914d<\/li>\n<li>~ \u5f00\u5934\u8868\u793a\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d;<\/li>\n<li>~* \u5f00\u5934\u8868\u793a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d<\/li>\n<li>\/ \u901a\u7528\u5339\u914d, \u5982\u679c\u6ca1\u6709\u5176\u5b83\u5339\u914d,\u4efb\u4f55\u8bf7\u6c42\u90fd\u4f1a\u5339\u914d\u5230<\/li>\n<\/ul>\n<p>\u987a\u5e8f no\u4f18\u5148\u7ea7\uff1a<\/p>\n<p>(location =) &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; (\/)<\/p>\n<p>\u4e0a\u9762\u7684\u5339\u914d\u7ed3\u679c<br \/>\n\u6309\u7167\u4e0a\u9762\u7684location\u5199\u6cd5\uff0c\u4ee5\u4e0b\u7684\u5339\u914d\u793a\u4f8b\u6210\u7acb\uff1a<\/p>\n<ul>\n<li>\/ -&gt; config A<br \/>\n\u7cbe\u786e\u5b8c\u5168\u5339\u914d\uff0c\u5373\u4f7f\/index.html\u4e5f\u5339\u914d\u4e0d\u4e86<\/li>\n<li>\/downloads\/download.html -&gt; config B<br \/>\n\u5339\u914dB\u4ee5\u540e\uff0c\u5f80\u4e0b\u6ca1\u6709\u4efb\u4f55\u5339\u914d\uff0c\u91c7\u7528B<\/li>\n<li>\/images\/1.gif -&gt; configuration D<br \/>\n\u5339\u914d\u5230F\uff0c\u5f80\u4e0b\u5339\u914d\u5230D\uff0c\u505c\u6b62\u5f80\u4e0b<\/li>\n<li>\/images\/abc\/def -&gt; config D<br \/>\n\u6700\u957f\u5339\u914d\u5230G\uff0c\u5f80\u4e0b\u5339\u914dD\uff0c\u505c\u6b62\u5f80\u4e0b<br \/>\n\u4f60\u53ef\u4ee5\u770b\u5230 \u4efb\u4f55\u4ee5\/images\/\u5f00\u5934\u7684\u90fd\u4f1a\u5339\u914d\u5230D\u5e76\u505c\u6b62\uff0cFG\u5199\u5728\u8fd9\u91cc\u662f\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\u7684\uff0cH\u662f\u6c38\u8fdc\u8f6e\u4e0d\u5230\u7684\uff0c\u8fd9\u91cc\u53ea\u662f\u4e3a\u4e86\u8bf4\u660e\u5339\u914d\u987a\u5e8f<\/li>\n<li>\/documents\/document.html -&gt; config C<br \/>\n\u5339\u914d\u5230C\uff0c\u5f80\u4e0b\u6ca1\u6709\u4efb\u4f55\u5339\u914d\uff0c\u91c7\u7528C<\/li>\n<li>\/documents\/1.jpg -&gt; configuration E<br \/>\n\u5339\u914d\u5230C\uff0c\u5f80\u4e0b\u6b63\u5219\u5339\u914d\u5230E<\/li>\n<li>\/documents\/Abc.jpg -&gt; config CC<br \/>\n\u6700\u957f\u5339\u914d\u5230C\uff0c\u5f80\u4e0b\u6b63\u5219\u987a\u5e8f\u5339\u914d\u5230CC\uff0c\u4e0d\u4f1a\u5f80\u4e0b\u5230E<\/li>\n<\/ul>\n<h2>\u5b9e\u9645\u4f7f\u7528\u5efa\u8bae<\/h2>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\u6240\u4ee5\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u4e2a\u4eba\u89c9\u5f97\u81f3\u5c11\u6709\u4e09\u4e2a\u5339\u914d\u89c4\u5219\u5b9a\u4e49\uff0c\u5982\u4e0b\uff1a\r\n#\u76f4\u63a5\u5339\u914d\u7f51\u7ad9\u6839\uff0c\u901a\u8fc7\u57df\u540d\u8bbf\u95ee\u7f51\u7ad9\u9996\u9875\u6bd4\u8f83\u9891\u7e41\uff0c\u4f7f\u7528\u8fd9\u4e2a\u4f1a\u52a0\u901f\u5904\u7406\uff0c\u5b98\u7f51\u5982\u662f\u8bf4\u3002\r\n#\u8fd9\u91cc\u662f\u76f4\u63a5\u8f6c\u53d1\u7ed9\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\u4e86\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u9759\u6001\u9996\u9875\r\n# \u7b2c\u4e00\u4e2a\u5fc5\u9009\u89c4\u5219\r\nlocation = \/ {\r\n    proxy_pass http:\/\/tomcat:8080\/index\r\n}\r\n# \u7b2c\u4e8c\u4e2a\u5fc5\u9009\u89c4\u5219\u662f\u5904\u7406\u9759\u6001\u6587\u4ef6\u8bf7\u6c42\uff0c\u8fd9\u662fnginx\u4f5c\u4e3ahttp\u670d\u52a1\u5668\u7684\u5f3a\u9879\r\n# \u6709\u4e24\u79cd\u914d\u7f6e\u6a21\u5f0f\uff0c\u76ee\u5f55\u5339\u914d\u6216\u540e\u7f00\u5339\u914d,\u4efb\u9009\u5176\u4e00\u6216\u642d\u914d\u4f7f\u7528\r\nlocation ^~ \/static\/ {\r\n    root \/webroot\/static\/;\r\n}\r\nlocation ~* \\.(gif|jpg|jpeg|png|css|js|ico)$ {\r\n    root \/webroot\/res\/;\r\n}\r\n#\u7b2c\u4e09\u4e2a\u89c4\u5219\u5c31\u662f\u901a\u7528\u89c4\u5219\uff0c\u7528\u6765\u8f6c\u53d1\u52a8\u6001\u8bf7\u6c42\u5230\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\r\n#\u975e\u9759\u6001\u6587\u4ef6\u8bf7\u6c42\u5c31\u9ed8\u8ba4\u662f\u52a8\u6001\u8bf7\u6c42\uff0c\u81ea\u5df1\u6839\u636e\u5b9e\u9645\u628a\u63e1\r\n#\u6bd5\u7adf\u76ee\u524d\u7684\u4e00\u4e9b\u6846\u67b6\u7684\u6d41\u884c\uff0c\u5e26.php,.jsp\u540e\u7f00\u7684\u60c5\u51b5\u5f88\u5c11\u4e86\r\nlocation \/ {\r\n    proxy_pass http:\/\/tomcat:8080\/\r\n}\r\n<\/pre>\n<p>http:\/\/tengine.taobao.org\/book\/chapter_02.html<br \/>\nhttp:\/\/nginx.org\/en\/docs\/http\/ngx_http_rewrite_module.html<\/p>\n<h1>2. Rewrite\u89c4\u5219<\/h1>\n<p>rewrite\u529f\u80fd\u5c31\u662f\uff0c\u4f7f\u7528nginx\u63d0\u4f9b\u7684\u5168\u5c40\u53d8\u91cf\u6216\u81ea\u5df1\u8bbe\u7f6e\u7684\u53d8\u91cf\uff0c\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u6807\u5fd7\u4f4d\u5b9e\u73b0url\u91cd\u5199\u4ee5\u53ca\u91cd\u5b9a\u5411\u3002rewrite\u53ea\u80fd\u653e\u5728server{},location{},if{}\u4e2d\uff0c\u5e76\u4e14\u53ea\u80fd\u5bf9\u57df\u540d\u540e\u8fb9\u7684\u9664\u53bb\u4f20\u9012\u7684\u53c2\u6570\u5916\u7684\u5b57\u7b26\u4e32\u8d77\u4f5c\u7528\uff0c\u4f8b\u5982 http:\/\/seanlook.com\/a\/we\/index.php?id=1&amp;u=str \u53ea\u5bf9\/a\/we\/index.php\u91cd\u5199\u3002\u8bed\u6cd5rewrite regex replacement [flag];<\/p>\n<p>\u5982\u679c\u76f8\u5bf9\u57df\u540d\u6216\u53c2\u6570\u5b57\u7b26\u4e32\u8d77\u4f5c\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u5339\u914d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528proxy_pass\u53cd\u5411\u4ee3\u7406\u3002<\/p>\n<p>\u8868\u660e\u770brewrite\u548clocation\u529f\u80fd\u6709\u70b9\u50cf\uff0c\u90fd\u80fd\u5b9e\u73b0\u8df3\u8f6c\uff0c\u4e3b\u8981\u533a\u522b\u5728\u4e8erewrite\u662f\u5728\u540c\u4e00\u57df\u540d\u5185\u66f4\u6539\u83b7\u53d6\u8d44\u6e90\u7684\u8def\u5f84\uff0c\u800clocation\u662f\u5bf9\u4e00\u7c7b\u8def\u5f84\u505a\u63a7\u5236\u8bbf\u95ee\u6216\u53cd\u5411\u4ee3\u7406\uff0c\u53ef\u4ee5proxy_pass\u5230\u5176\u4ed6\u673a\u5668\u3002\u5f88\u591a\u60c5\u51b5\u4e0brewrite\u4e5f\u4f1a\u5199\u5728location\u91cc\uff0c\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f\u662f\uff1a<\/p>\n<ol>\n<li>\u6267\u884cserver\u5757\u7684rewrite\u6307\u4ee4<\/li>\n<li>\u6267\u884clocation\u5339\u914d<\/li>\n<li>\u6267\u884c\u9009\u5b9a\u7684location\u4e2d\u7684rewrite\u6307\u4ee4<\/li>\n<\/ol>\n<p>\u5982\u679c\u5176\u4e2d\u67d0\u6b65URI\u88ab\u91cd\u5199\uff0c\u5219\u91cd\u65b0\u5faa\u73af\u6267\u884c1-3\uff0c\u76f4\u5230\u627e\u5230\u771f\u5b9e\u5b58\u5728\u7684\u6587\u4ef6\uff1b\u5faa\u73af\u8d85\u8fc710\u6b21\uff0c\u5219\u8fd4\u56de500 Internal Server Error\u9519\u8bef\u3002<\/p>\n<h2>2.1 flag\u6807\u5fd7\u4f4d<\/h2>\n<ul>\n<li>last : \u76f8\u5f53\u4e8eApache\u7684[L]\u6807\u8bb0\uff0c\u8868\u793a\u5b8c\u6210rewrite<\/li>\n<li>break : \u505c\u6b62\u6267\u884c\u5f53\u524d\u865a\u62df\u4e3b\u673a\u7684\u540e\u7eedrewrite\u6307\u4ee4\u96c6<\/li>\n<li>redirect : \u8fd4\u56de302\u4e34\u65f6\u91cd\u5b9a\u5411\uff0c\u5730\u5740\u680f\u4f1a\u663e\u793a\u8df3\u8f6c\u540e\u7684\u5730\u5740<\/li>\n<li>permanent : \u8fd4\u56de301\u6c38\u4e45\u91cd\u5b9a\u5411\uff0c\u5730\u5740\u680f\u4f1a\u663e\u793a\u8df3\u8f6c\u540e\u7684\u5730\u5740<\/li>\n<\/ul>\n<p>\u56e0\u4e3a301\u548c302\u4e0d\u80fd\u7b80\u5355\u7684\u53ea\u8fd4\u56de\u72b6\u6001\u7801\uff0c\u8fd8\u5fc5\u987b\u6709\u91cd\u5b9a\u5411\u7684URL\uff0c\u8fd9\u5c31\u662freturn\u6307\u4ee4\u65e0\u6cd5\u8fd4\u56de301,302\u7684\u539f\u56e0\u4e86\u3002\u8fd9\u91cc last \u548c break \u533a\u522b\u6709\u70b9\u96be\u4ee5\u7406\u89e3\uff1a<\/p>\n<ul>\n<li>last\u4e00\u822c\u5199\u5728server\u548cif\u4e2d\uff0c\u800cbreak\u4e00\u822c\u4f7f\u7528\u5728location\u4e2d<\/li>\n<li>last\u4e0d\u7ec8\u6b62\u91cd\u5199\u540e\u7684url\u5339\u914d\uff0c\u5373\u65b0\u7684url\u4f1a\u518d\u4eceserver\u8d70\u4e00\u904d\u5339\u914d\u6d41\u7a0b\uff0c\u800cbreak\u7ec8\u6b62\u91cd\u5199\u540e\u7684\u5339\u914d<\/li>\n<li>break\u548clast\u90fd\u80fd\u7ec4\u7ec7\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684rewrite\u6307\u4ee4<\/li>\n<\/ul>\n<h2>2.2 if\u6307\u4ee4\u4e0e\u5168\u5c40\u53d8\u91cf<\/h2>\n<p><strong>if\u5224\u65ad\u6307\u4ee4<\/strong><\/p>\n<p>\u8bed\u6cd5\u4e3aif(condition){&#8230;}\uff0c\u5bf9\u7ed9\u5b9a\u7684\u6761\u4ef6condition\u8fdb\u884c\u5224\u65ad\u3002\u5982\u679c\u4e3a\u771f\uff0c\u5927\u62ec\u53f7\u5185\u7684rewrite\u6307\u4ee4\u5c06\u88ab\u6267\u884c\uff0cif\u6761\u4ef6(conditon)\u53ef\u4ee5\u662f\u5982\u4e0b\u4efb\u4f55\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u5f53\u8868\u8fbe\u5f0f\u53ea\u662f\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5982\u679c\u503c\u4e3a\u7a7a\u6216\u4efb\u4f55\u4ee50\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u90fd\u4f1a\u5f53\u505afalse<\/li>\n<li>\u76f4\u63a5\u6bd4\u8f83\u53d8\u91cf\u548c\u5185\u5bb9\u65f6\uff0c\u4f7f\u7528=\u6216!=<\/li>\n<li>~\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0c~*\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5339\u914d\uff0c!~\u533a\u5206\u5927\u5c0f\u5199\u7684\u4e0d\u5339\u914d<\/li>\n<\/ul>\n<p>-f\u548c!-f\u7528\u6765\u5224\u65ad\u662f\u5426\u5b58\u5728\u6587\u4ef6<br \/>\n-d\u548c!-d\u7528\u6765\u5224\u65ad\u662f\u5426\u5b58\u5728\u76ee\u5f55<br \/>\n-e\u548c!-e\u7528\u6765\u5224\u65ad\u662f\u5426\u5b58\u5728\u6587\u4ef6\u6216\u76ee\u5f55<br \/>\n-x\u548c!-x\u7528\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u6267\u884c<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nif ($http_user_agent ~ MSIE) {\r\n    rewrite ^(.*)$ \/msie\/$1 break;\r\n} \/\/\u5982\u679cUA\u5305\u542b&quot;MSIE&quot;\uff0crewrite\u8bf7\u6c42\u5230\/msid\/\u76ee\u5f55\u4e0b\r\nif ($http_cookie ~* &quot;id=(&#x5B;^;]+)(?:;|$)&quot;) {\r\n    set $id $1;\r\n } \/\/\u5982\u679ccookie\u5339\u914d\u6b63\u5219\uff0c\u8bbe\u7f6e\u53d8\u91cf$id\u7b49\u4e8e\u6b63\u5219\u5f15\u7528\u90e8\u5206\r\nif ($request_method = POST) {\r\n    return 405;\r\n} \/\/\u5982\u679c\u63d0\u4ea4\u65b9\u6cd5\u4e3aPOST\uff0c\u5219\u8fd4\u56de\u72b6\u6001405\uff08Method not allowed\uff09\u3002return\u4e0d\u80fd\u8fd4\u56de301,302\r\nif ($slow) {\r\n    limit_rate 10k;\r\n} \/\/\u9650\u901f\uff0c$slow\u53ef\u4ee5\u901a\u8fc7 set \u6307\u4ee4\u8bbe\u7f6e\r\nif (!-f $request_filename){\r\n    break;\r\n    proxy_pass  http:\/\/127.0.0.1;\r\n} \/\/\u5982\u679c\u8bf7\u6c42\u7684\u6587\u4ef6\u540d\u4e0d\u5b58\u5728\uff0c\u5219\u53cd\u5411\u4ee3\u7406\u5230localhost \u3002\u8fd9\u91cc\u7684break\u4e5f\u662f\u505c\u6b62rewrite\u68c0\u67e5\r\nif ($args ~ post=140){\r\n    rewrite ^ http:\/\/example.com\/ permanent;\r\n} \/\/\u5982\u679cquery string\u4e2d\u5305\u542b&quot;post=140&quot;\uff0c\u6c38\u4e45\u91cd\u5b9a\u5411\u5230example.com\r\nlocation ~* \\.(gif|jpg|png|swf|flv)$ {\r\n    valid_referers none blocked www.jefflei.com www.leizhenfang.com;\r\n    if ($invalid_referer) {\r\n        return 404;\r\n    } \/\/\u9632\u76d7\u94fe\r\n}\r\n<\/pre>\n<p><strong>\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u53ef\u4ee5\u7528\u4f5cif\u5224\u65ad\u7684\u5168\u5c40\u53d8\u91cf<\/p>\n<ul>\n<li>$args \uff1a #\u8fd9\u4e2a\u53d8\u91cf\u7b49\u4e8e\u8bf7\u6c42\u884c\u4e2d\u7684\u53c2\u6570\uff0c\u540c$query_string<\/li>\n<li>$content_length \uff1a \u8bf7\u6c42\u5934\u4e2d\u7684Content-length\u5b57\u6bb5\u3002<\/li>\n<li>$content_type \uff1a \u8bf7\u6c42\u5934\u4e2d\u7684Content-Type\u5b57\u6bb5\u3002<\/li>\n<li>$document_root \uff1a \u5f53\u524d\u8bf7\u6c42\u5728root\u6307\u4ee4\u4e2d\u6307\u5b9a\u7684\u503c\u3002<\/li>\n<li>$host \uff1a \u8bf7\u6c42\u4e3b\u673a\u5934\u5b57\u6bb5\uff0c\u5426\u5219\u4e3a\u670d\u52a1\u5668\u540d\u79f0\u3002<\/li>\n<li>$http_user_agent \uff1a \u5ba2\u6237\u7aefagent\u4fe1\u606f<\/li>\n<li>$http_cookie \uff1a \u5ba2\u6237\u7aefcookie\u4fe1\u606f<\/li>\n<li>$limit_rate \uff1a \u8fd9\u4e2a\u53d8\u91cf\u53ef\u4ee5\u9650\u5236\u8fde\u63a5\u901f\u7387\u3002<\/li>\n<li>$request_method \uff1a \u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u52a8\u4f5c\uff0c\u901a\u5e38\u4e3aGET\u6216POST\u3002<\/li>\n<li>$remote_addr \uff1a \u5ba2\u6237\u7aef\u7684IP\u5730\u5740\u3002<\/li>\n<li>$remote_port \uff1a \u5ba2\u6237\u7aef\u7684\u7aef\u53e3\u3002<\/li>\n<li>$remote_user \uff1a \u5df2\u7ecf\u7ecf\u8fc7Auth Basic Module\u9a8c\u8bc1\u7684\u7528\u6237\u540d\u3002<\/li>\n<li>$request_filename \uff1a \u5f53\u524d\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u7531root\u6216alias\u6307\u4ee4\u4e0eURI\u8bf7\u6c42\u751f\u6210\u3002<\/li>\n<li>$scheme \uff1a HTTP\u65b9\u6cd5\uff08\u5982http\uff0chttps\uff09\u3002<\/li>\n<li>$server_protocol \uff1a \u8bf7\u6c42\u4f7f\u7528\u7684\u534f\u8bae\uff0c\u901a\u5e38\u662fHTTP\/1.0\u6216HTTP\/1.1\u3002<\/li>\n<li>$server_addr \uff1a \u670d\u52a1\u5668\u5730\u5740\uff0c\u5728\u5b8c\u6210\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528\u540e\u53ef\u4ee5\u786e\u5b9a\u8fd9\u4e2a\u503c\u3002<\/li>\n<li>$server_name \uff1a \u670d\u52a1\u5668\u540d\u79f0\u3002<\/li>\n<li>$server_port \uff1a \u8bf7\u6c42\u5230\u8fbe\u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\u3002<\/li>\n<li>$request_uri \uff1a \u5305\u542b\u8bf7\u6c42\u53c2\u6570\u7684\u539f\u59cbURI\uff0c\u4e0d\u5305\u542b\u4e3b\u673a\u540d\uff0c\u5982\uff1a\u201d\/foo\/bar.php?arg=baz\u201d\u3002<\/li>\n<li>$uri \uff1a \u4e0d\u5e26\u8bf7\u6c42\u53c2\u6570\u7684\u5f53\u524dURI\uff0c$uri\u4e0d\u5305\u542b\u4e3b\u673a\u540d\uff0c\u5982\u201d\/foo\/bar.html\u201d\u3002<\/li>\n<li>$document_uri \uff1a \u4e0e$uri\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p>\u4f8b\uff1ahttp:\/\/localhost:88\/test1\/test2\/test.php<\/p>\n<p>$host\uff1alocalhost<br \/>\n$server_port\uff1a88<br \/>\n$request_uri\uff1ahttp:\/\/localhost:88\/test1\/test2\/test.php<br \/>\n$document_uri\uff1a\/test1\/test2\/test.php<br \/>\n$document_root\uff1a\/var\/www\/html<br \/>\n$request_filename\uff1a\/var\/www\/html\/test1\/test2\/test.php<\/p>\n<h2>2.3 \u5e38\u7528\u6b63\u5219<\/h2>\n<ul>\n<li>. \uff1a \u5339\u914d\u9664\u6362\u884c\u7b26\u4ee5\u5916\u7684\u4efb\u610f\u5b57\u7b26<\/li>\n<li>? \uff1a \u91cd\u590d0\u6b21\u62161\u6b21<\/li>\n<li>+ \uff1a \u91cd\u590d1\u6b21\u6216\u66f4\u591a\u6b21<\/li>\n<li>* \uff1a \u91cd\u590d0\u6b21\u6216\u66f4\u591a\u6b21<\/li>\n<li>\\d \uff1a\u5339\u914d\u6570\u5b57<\/li>\n<li>^ \uff1a \u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb<\/li>\n<li>$ \uff1a \u5339\u914d\u5b57\u7b26\u4e32\u7684\u4ecb\u7ecd<\/li>\n<li>{n} \uff1a \u91cd\u590dn\u6b21<\/li>\n<li>{n,} \uff1a \u91cd\u590dn\u6b21\u6216\u66f4\u591a\u6b21<\/li>\n<li>[a-z] \uff1a \u5339\u914da-z\u5c0f\u5199\u5b57\u6bcd\u7684\u4efb\u610f\u4e00\u4e2a<\/li>\n<\/ul>\n<p>\u5c0f\u62ec\u53f7()\u4e4b\u95f4\u5339\u914d\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u540e\u9762\u901a\u8fc7$1\u6765\u5f15\u7528\uff0c$2\u8868\u793a\u7684\u662f\u524d\u9762\u7b2c\u4e8c\u4e2a()\u91cc\u7684\u5185\u5bb9\u3002\u6b63\u5219\u91cc\u9762\u5bb9\u6613\u8ba9\u4eba\u56f0\u60d1\u7684\u662f\\\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<h2>2.4 rewrite\u5b9e\u4f8b<\/h2>\n<p>\u4f8b1\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nhttp {\r\n    # \u5b9a\u4e49image\u65e5\u5fd7\u683c\u5f0f\r\n    log_format imagelog '&#x5B;$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;\r\n    # \u5f00\u542f\u91cd\u5199\u65e5\u5fd7\r\n    rewrite_log on;\r\n    server {\r\n        root \/home\/www;\r\n        location \/ {\r\n                # \u91cd\u5199\u89c4\u5219\u4fe1\u606f\r\n                error_log logs\/rewrite.log notice;\r\n                # \u6ce8\u610f\u8fd9\u91cc\u8981\u7528\u2018\u2019\u5355\u5f15\u53f7\u5f15\u8d77\u6765\uff0c\u907f\u514d{}\r\n                rewrite '^\/images\/(&#x5B;a-z]{2})\/(&#x5B;a-z0-9]{5})\/(.*)\\.(png|jpg|gif)$' \/data?file=$3.$4;\r\n                # \u6ce8\u610f\u4e0d\u80fd\u5728\u4e0a\u9762\u8fd9\u6761\u89c4\u5219\u540e\u9762\u52a0\u4e0a\u201clast\u201d\u53c2\u6570\uff0c\u5426\u5219\u4e0b\u9762\u7684set\u6307\u4ee4\u4e0d\u4f1a\u6267\u884c\r\n                set $image_file $3;\r\n                set $image_type $4;\r\n        }\r\n        location \/data {\r\n                # \u6307\u5b9a\u9488\u5bf9\u56fe\u7247\u7684\u65e5\u5fd7\u683c\u5f0f\uff0c\u6765\u5206\u6790\u56fe\u7247\u7c7b\u578b\u548c\u5927\u5c0f\r\n                access_log logs\/images.log mian;\r\n                root \/data\/images;\r\n                # \u5e94\u7528\u524d\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u5224\u65ad\u9996\u5148\u6587\u4ef6\u5728\u4e0d\u5728\uff0c\u4e0d\u5728\u518d\u5224\u65ad\u76ee\u5f55\u5728\u4e0d\u5728\uff0c\u5982\u679c\u8fd8\u4e0d\u5728\u5c31\u8df3\u8f6c\u5230\u6700\u540e\u4e00\u4e2aurl\u91cc\r\n                try_files \/$arg_file \/image404.html;\r\n        }\r\n        location = \/image404.html {\r\n                # \u56fe\u7247\u4e0d\u5b58\u5728\u8fd4\u56de\u7279\u5b9a\u7684\u4fe1\u606f\r\n                return 404 &quot;image not found\\n&quot;;\r\n        }\r\n}\r\n<\/pre>\n<p>\u5bf9\u5f62\u5982\/images\/ef\/uh7b3\/test.png\u7684\u8bf7\u6c42\uff0c\u91cd\u5199\u5230\/data?file=test.png\uff0c\u4e8e\u662f\u5339\u914d\u5230location \/data\uff0c\u5148\u770b\/data\/images\/test.png\u6587\u4ef6\u5b58\u4e0d\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u5219\u6b63\u5e38\u54cd\u5e94\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u91cd\u5199tryfiles\u5230\u65b0\u7684image404 location\uff0c\u76f4\u63a5\u8fd4\u56de404\u72b6\u6001\u7801\u3002<\/p>\n<p>\u4f8b2\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nrewrite ^\/images\/(.*)_(\\d+)x(\\d+)\\.(png|jpg|gif)$ \/resizer\/$1.$4?width=$2&amp;height=$3? last;\r\n<\/pre>\n<p>\u5bf9\u5f62\u5982\/images\/bla_500x400.jpg\u7684\u6587\u4ef6\u8bf7\u6c42\uff0c\u91cd\u5199\u5230\/resizer\/bla.jpg?width=500&amp;height=400\u5730\u5740\uff0c\u5e76\u4f1a\u7ee7\u7eed\u5c1d\u8bd5\u5339\u914dlocation\u3002<\/p>\n<p>\u4f8b3\uff1a<\/p>\n<p>\u89c1 ssl\u90e8\u5206\u9875\u9762\u52a0\u5bc6 \u3002<\/p>\n<p>\u53c2\u8003<\/p>\n<ul>\n<li>http:\/\/www.nginx.cn\/216.html<\/li>\n<li>http:\/\/www.ttlsa.com\/nginx\/nginx-rewriting-rules-guide\/<\/li>\n<li>http:\/\/fantefei.blog.51cto.com\/2229719\/919431<\/li>\n<\/ul>\n<h1>Nginx\u89c4\u5219\u6587\u4ef6\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177\uff1ahttp:\/\/nginx.viraptor.info\/<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>1. location\u6b63\u5219\u5199\u6cd5 \u4e00\u4e2a\u793a\u4f8b\uff1a location = \/ { # \u7cbe\u786e\u5339\u914d \/ \uff0c\u4e3b\u673a\u540d\u540e\u9762\u4e0d\u80fd\u5e26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6266","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6266"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6266\/revisions"}],"predecessor-version":[{"id":6269,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6266\/revisions\/6269"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}