{"id":1009,"date":"2014-10-15T17:01:53","date_gmt":"2014-10-15T09:01:53","guid":{"rendered":"http:\/\/www.poloo.org\/?p=1009"},"modified":"2014-10-15T17:14:09","modified_gmt":"2014-10-15T09:14:09","slug":"nginx-rewrite%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=1009","title":{"rendered":"Nginx rewrite\u57fa\u672c\u8bed\u6cd5"},"content":{"rendered":"<p>\u5728\u65b0\u4e3b\u673a\u7684\u8fc1\u79fb\u8fc7\u7a0b\u4e2d,\u6700\u5927\u7684\u56f0\u96be\u5c31\u662fWP permalink rewrite\u7684\u8bbe\u7f6e.<\/p>\n<p>\u56e0\u4e3a\u65e7\u4e3b\u673a\u662f\u7528\u7684Apache, \u4f7f\u7528\u7684\u662fWP\u672c\u8eab\u5c31\u53ef\u4ee5\u66f4\u6539\u7684.htaccess,\u6ca1\u6709\u592a\u5927\u7684\u96be\u5ea6.\u800c\u8fd9\u6b21\u5728VPS\u4e0a\u8dd1\u7684\u662fNginx,\u4e3b\u8981\u662f\u56e0\u4e3aNginx\u7684\u901f\u5ea6\u6bd4Apache\u8981\u5feb\u5f88\u591a.<\/p>\n<p>\u4f46\u662f\u53e6\u4e00\u65b9\u9762\u5c31\u4e0d\u662f\u90a3\u4e48\u8212\u670d\u4e86,\u56e0\u4e3aNginx\u7684rewrite\u8ddfApache\u4e0d\u540c,\u800c\u4e14\u662f\u5728\u670d\u52a1\u5668\u4e0a\u9762\u624d\u80fd\u66f4\u6539.<\/p>\n<p>\u4e0b\u9762\u662f\u5176\u95f4\u7684\u4e00\u4e9b\u7814\u7a76\u7b14\u8bb0.(\u4ee5\u4e0b\u7528\u4f8b\u5982\u65e0\u7279\u522b\u8bf4\u660e\u5747\u6458\u81eanginx wiki)<\/p>\n<p><!--more--><\/p>\n<h2>\/1 Nginx rewrite\u57fa\u672c\u8bed\u6cd5<\/h2>\n<p>&nbsp;<\/p>\n<p>Nginx\u7684rewrite\u8bed\u6cd5\u5176\u5b9e\u5f88\u7b80\u5355.\u7528\u5230\u7684\u6307\u4ee4\u65e0\u975e\u662f\u8fd9\u51e0\u4e2a<\/p>\n<ul>\n<li><strong>set<\/strong><\/li>\n<li><strong>if<\/strong><\/li>\n<li><strong>return<\/strong><\/li>\n<li><strong>break<\/strong><\/li>\n<li><strong>rewrite<\/strong><\/li>\n<\/ul>\n<p>\u9ebb\u96c0\u867d\u5c0f,\u53ef\u5fa1\u53ef\u841d\u4e94\u810f\u4ff1\u5168.\u53ea\u662f\u7b80\u5355\u7684\u51e0\u4e2a\u6307\u4ee4\u5374\u53ef\u4ee5\u505a\u51fa\u7edd\u5bf9\u4e0d\u8f93apache\u7684\u7b80\u5355\u7075\u6d3b\u7684\u914d\u7f6e.<\/p>\n<p><strong>1.set<\/strong><\/p>\n<p>set\u4e3b\u8981\u662f\u7528\u6765\u8bbe\u7f6e\u53d8\u91cf\u7528\u7684,\u6ca1\u4ec0\u4e48\u7279\u522b\u7684<\/p>\n<p><strong>2.if<\/strong><\/p>\n<p>if\u4e3b\u8981\u7528\u6765\u5224\u65ad\u4e00\u4e9b\u5728rewrite\u8bed\u53e5\u4e2d\u65e0\u6cd5\u76f4\u63a5\u5339\u914d\u7684\u6761\u4ef6,\u6bd4\u5982\u68c0\u6d4b\u6587\u4ef6\u5b58\u5728\u4e0e\u5426,http header,cookie\u7b49,<\/p>\n<blockquote><p>\u7528\u6cd5: if(\u6761\u4ef6) {\u2026}<\/p><\/blockquote>\n<p>&#8211; \u5f53if\u8868\u8fbe\u5f0f\u4e2d\u7684\u6761\u4ef6\u4e3atrue,\u5219\u6267\u884cif\u5757\u4e2d\u7684\u8bed\u53e5<\/p>\n<p>&#8211; \u5f53\u8868\u8fbe\u5f0f\u53ea\u662f\u4e00\u4e2a\u53d8\u91cf\u65f6,\u5982\u679c\u503c\u4e3a\u7a7a\u6216\u8005\u4efb\u4f55\u4ee50\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u90fd\u4f1a\u5f53\u4f5cfalse<\/p>\n<p>&#8211; \u76f4\u63a5\u6bd4\u8f83\u5185\u5bb9\u65f6,\u4f7f\u7528 = \u548c !=<\/p>\n<p>&#8211; \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u65f6,\u4f7f\u7528<\/p>\n<blockquote><p>~ \u5927\u5c0f\u5199\u654f\u611f\u5339\u914d<br \/>\n~* \u5927\u5c0f\u5199\u4e0d\u654f\u611f\u5339\u914d<br \/>\n!~ \u5927\u5c0f\u5199\u654f\u611f\u4e0d\u5339\u914d<br \/>\n!~* \u5927\u5c0f\u5199\u4e0d\u654f\u611f\u4e0d\u5339\u914d<\/p><\/blockquote>\n<p>\u8fd9\u51e0\u53e5\u8bdd\u770b\u8d77\u6765\u6709\u70b9\u7ed5,\u603b\u4e4b\u8bb0\u4f4f: ~\u4e3a\u6b63\u5219\u5339\u914d, \u540e\u7f6e*\u4e3a\u5927\u5c0f\u5199\u4e0d\u654f\u611f, \u524d\u7f6e!\u4e3a\u201d\u975e\u201d\u64cd\u4f5c<\/p>\n<p>\u968f\u4fbf\u4e00\u63d0,\u56e0\u4e3anginx\u4f7f\u7528\u82b1\u62ec\u53f7{}\u5224\u65ad\u533a\u5757,\u6240\u4ee5\u5f53\u6b63\u5219\u4e2d\u5305\u542b\u82b1\u62ec\u53f7\u65f6,\u5219\u5fc5\u987b\u7528\u53cc\u5f15\u53f7\u5c06\u6b63\u5219\u5305\u8d77\u6765.\u5bf9\u4e0b\u9762\u8bb2\u5230\u7684rewrite\u8bed\u53e5\u4e2d\u7684\u6b63\u5219\u4ea6\u662f\u5982\u6b64.<br \/>\n\u6bd4\u5982 \u201c\\d{4}\\d{2}\\.+\u201d<\/p>\n<p>&#8211; \u4f7f\u7528-f,-d,-e,-x\u68c0\u6d4b\u6587\u4ef6\u548c\u76ee\u5f55<\/p>\n<blockquote><p>-f \u68c0\u6d4b\u6587\u4ef6\u5b58\u5728<br \/>\n-d \u68c0\u6d4b\u76ee\u5f55\u5b58\u5728<br \/>\n-e \u68c0\u6d4b\u6587\u4ef6,\u76ee\u5f55\u6216\u8005\u7b26\u53f7\u94fe\u63a5\u5b58\u5728<br \/>\n-x \u68c0\u6d4b\u6587\u4ef6\u53ef\u6267\u884c<\/p><\/blockquote>\n<p>\u8ddf~\u7c7b\u4f3c,\u524d\u7f6e!\u5219\u4e3a\u201d\u975e\u201d\u64cd\u4f5c<\/p>\n<p>\u4e3e\u4f8b<\/p>\n<pre class=\"brush:js\">if ($http_user_agent ~ MSIE) {\r\n  rewrite  ^(.*)$  \/msie\/$1  break;\r\n}<\/pre>\n<div><\/div>\n<p>\/\/\u5982\u679cUA\u5305\u542b\u201dMSIE\u201d,rewrite \u8bf7\u6c42\u5230\/msie\u76ee\u5f55\u4e0b<\/p>\n<pre class=\"brush:js\">if ($http_cookie ~* \"id=([^;] +)(?:;|$)\" ) {\r\n  set  $id  $1;\r\n}<\/pre>\n<div><\/div>\n<p>\/\/\u5982\u679ccookie\u5339\u914d\u6b63\u5219,\u8bbe\u7f6e\u53d8\u91cf$id\u7b49\u4e8e\u6b63\u5219\u5f15\u7528\u90e8\u5206<\/p>\n<pre class=\"brush:js\">if ($request_method = POST ) {\r\n  return 405;\r\n}<\/pre>\n<p>\/\/\u5982\u679c\u63d0\u4ea4\u65b9\u6cd5\u4e3aPOST,\u5219\u8fd4\u56de\u72b6\u6001405 (Method not allowed)<\/p>\n<pre class=\"brush:js\">if (!-f $request_filename) {\r\n  break;\r\n  proxy_pass  http:\/\/127.0.0.1;\r\n}<\/pre>\n<div><\/div>\n<p>\/\/\u5982\u679c\u8bf7\u6c42\u6587\u4ef6\u540d\u4e0d\u5b58\u5728,\u5219\u53cd\u5411\u4ee3\u7406localhost<\/p>\n<pre class=\"brush:js\">if ($args ~ post=140){\r\n  rewrite ^ http:\/\/example.com\/ permanent;\r\n}<\/pre>\n<div><\/div>\n<p>&nbsp;<\/p>\n<p>\/\/\u5982\u679cquery string\u4e2d\u5305\u542b\u201dpost=140\u2033,\u6c38\u4e45\u91cd\u5b9a\u5411\u5230example.com<\/p>\n<p><strong>3.return<\/strong><\/p>\n<p>return\u53ef\u7528\u6765\u76f4\u63a5\u8bbe\u7f6eHTTP\u8fd4\u56de\u72b6\u6001,\u6bd4\u5982403,404\u7b49(301,302\u4e0d\u53ef\u7528return\u8fd4\u56de,\u8fd9\u4e2a\u4e0b\u9762\u4f1a\u5728rewrite\u63d0\u5230)<\/p>\n<p><strong>4.break<\/strong><\/p>\n<p>\u7acb\u5373\u505c\u6b62rewrite\u68c0\u6d4b,\u8ddf\u4e0b\u9762\u8bb2\u5230\u7684rewrite\u7684break flag\u529f\u80fd\u662f\u4e00\u6837\u7684,\u533a\u522b\u5728\u4e8e\u524d\u8005\u662f\u4e00\u4e2a\u8bed\u53e5,\u540e\u8005\u662frewrite\u8bed\u53e5\u7684flag<\/p>\n<p><strong>5.rewrite<\/strong><\/p>\n<p>\u6700\u6838\u5fc3\u7684\u529f\u80fd(\u5e9f\u8bdd)<\/p>\n<blockquote><p>\u7528\u6cd5: rewrite \u6b63\u5219 \u66ff\u6362 \u6807\u5fd7\u4f4d<\/p><\/blockquote>\n<p>\u5176\u4e2d\u6807\u5fd7\u4f4d\u6709\u56db\u79cd<\/p>\n<blockquote><p>break \u2013 \u505c\u6b62rewrite\u68c0\u6d4b,\u4e5f\u5c31\u662f\u8bf4\u5f53\u542b\u6709break flag\u7684rewrite\u8bed\u53e5\u88ab\u6267\u884c\u65f6,\u8be5\u8bed\u53e5\u5c31\u662frewrite\u7684\u6700\u7ec8\u7ed3\u679c<br \/>\nlast \u2013 \u505c\u6b62rewrite\u68c0\u6d4b,\u4f46\u662f\u8ddfbreak\u6709\u672c\u8d28\u7684\u4e0d\u540c,last\u7684\u8bed\u53e5\u4e0d\u4e00\u5b9a\u662f\u6700\u7ec8\u7ed3\u679c,\u8fd9\u70b9\u540e\u9762\u4f1a\u8ddfnginx\u7684location\u5339\u914d\u4e00\u8d77\u63d0\u5230<br \/>\nredirect \u2013 \u8fd4\u56de302\u4e34\u65f6\u91cd\u5b9a\u5411,\u4e00\u822c\u7528\u4e8e\u91cd\u5b9a\u5411\u5230\u5b8c\u6574\u7684URL(\u5305\u542bhttp:\u90e8\u5206)<br \/>\npermanent \u2013 \u8fd4\u56de301\u6c38\u4e45\u91cd\u5b9a\u5411,\u4e00\u822c\u7528\u4e8e\u91cd\u5b9a\u5411\u5230\u5b8c\u6574\u7684URL(\u5305\u542bhttp:\u90e8\u5206)<\/p><\/blockquote>\n<p>\u56e0\u4e3a301\u548c302\u4e0d\u80fd\u7b80\u5355\u7684\u53ea\u5355\u7eaf\u8fd4\u56de\u72b6\u6001\u7801,\u8fd8\u5fc5\u987b\u6709\u91cd\u5b9a\u5411\u7684URL,\u8fd9\u5c31\u662freturn\u6307\u4ee4\u65e0\u6cd5\u8fd4\u56de301,302\u7684\u539f\u56e0\u4e86. \u4f5c\u4e3a\u66ff\u6362,rewrite\u53ef\u4ee5\u66f4\u7075\u6d3b\u7684\u4f7f\u7528redirect\u548cpermanent\u6807\u5fd7\u5b9e\u73b0301\u548c302. \u6bd4\u5982\u4e0a\u4e00\u7bc7\u65e5\u5fd7\u4e2d\u63d0\u5230\u7684Blog\u642c\u5bb6\u8981\u505a\u7684\u57df\u540d\u91cd\u5b9a\u5411,\u5728nginx\u4e2d\u5c31\u4f1a\u8fd9\u4e48\u5199<\/p>\n<pre class=\"brush:js\">rewrite ^(.*)$ http:\/\/newdomain.com\/ permanent;<\/pre>\n<div>\u4e3e\u4f8b\u6765\u8bf4\u4e00\u4e0brewrite\u7684\u5b9e\u9645\u5e94\u7528<\/div>\n<div>\n<pre class=\"brush:js\">rewrite  ^(\/download\/.*)\/media\/(.*)\\..*$  $1\/mp3\/$2.mp3  last;<\/pre>\n<\/div>\n<div><\/div>\n<div>\u5982\u679c\u8bf7\u6c42\u4e3a \/download\/eva\/media\/op1.mp3 \u5219\u8bf7\u6c42\u88abrewrite\u5230 \/download\/eva\/mp3\/op1.mp3<\/div>\n<p>\u4f7f\u7528\u8d77\u6765\u5c31\u662f\u8fd9\u6837,\u5f88\u7b80\u5355\u4e0d\u662f\u4e48? \u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662frewrite\u6709\u5f88\u591a\u6f5c\u89c4\u5219\u9700\u8981\u6ce8\u610f<\/p>\n<p>&#8211; rewrite\u7684\u751f\u6548\u533a\u5757\u4e3asever, location, if<\/p>\n<p>&#8211; rewrite\u53ea\u5bf9\u76f8\u5bf9\u8def\u5f84\u8fdb\u884c\u5339\u914d,\u4e0d\u5305\u542bhostname \u6bd4\u5982\u8bf4\u4ee5\u4e0a\u9762301\u91cd\u5b9a\u5411\u7684\u4f8b\u5b50\u8bf4\u660e<\/p>\n<pre class=\"brush:js\">rewrite ~* cafeneko\\.info http:\/\/newdomain.com\/ permanent;<\/pre>\n<div><\/div>\n<p>\u8fd9\u53e5\u662f\u6c38\u8fdc\u65e0\u6cd5\u6267\u884c\u7684,\u4ee5\u8fd9\u4e2aURL\u4e3a\u4f8b<\/p>\n<blockquote><p>http:\/\/blog.cafeneko.info\/2010\/10\/neokoseseiki_in_new_home\/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=neokoseseiki_in_new_home<\/p><\/blockquote>\n<p>\u5176\u4e2dcafeneko.info\u53eb\u505ahostname,\u518d\u5f80\u540e\u5230?\u4e3a\u6b62\u53eb\u505a\u76f8\u5bf9\u8def\u5f84,?\u540e\u9762\u7684\u4e00\u4e32\u53eb\u505aquery string<\/p>\n<p>\u5bf9\u4e8erewrite\u6765\u8bf4,\u5176\u6b63\u5219\u8868\u8fbe\u5f0f\u4ec5\u5bf9\u201d\/2010\/10\/neokoseseiki_in_new_home\u201d\u8fd9\u4e00\u90e8\u5206\u8fdb\u884c\u5339\u914d,\u5373\u4e0d\u5305\u542bhostname,\u4e5f\u4e0d\u5305\u542bquery string .\u6240\u4ee5\u9664\u975e\u76f8\u5bf9\u8def\u5f84\u4e2d\u5305\u542b\u8ddf\u57df\u540d\u4e00\u6837\u7684string,\u5426\u5219\u662f\u4e0d\u4f1a\u5339\u914d\u7684. \u5982\u679c\u975e\u8981\u505a\u57df\u540d\u5339\u914d\u7684\u8bdd\u5c31\u8981\u4f7f\u7528if\u8bed\u53e5\u4e86,\u6bd4\u5982\u8fdb\u884c\u53bbwww\u8df3\u8f6c<\/p>\n<pre class=\"brush:js\">if ($host ~* ^www\\.(cafeneko\\.info)) {\r\n  set $host_without_www $1;\r\n  rewrite ^(.*)$ http:\/\/$host_without_www$1 permanent;\r\n}<\/pre>\n<div><\/div>\n<p>&#8211; \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84rewrite\u65f6,\u4f1a\u6839\u636eHTTP header\u4e2d\u7684HOST\u8ddfnginx\u7684server_name\u5339\u914d\u540e\u8fdb\u884crewrite,\u5982\u679cHOST\u4e0d\u5339\u914d\u6216\u8005\u6ca1\u6709HOST\u4fe1\u606f\u7684\u8bdd\u5219rewrite\u5230server_name\u8bbe\u7f6e\u7684\u7b2c\u4e00\u4e2a\u57df\u540d,\u5982\u679c\u6ca1\u6709\u8bbe\u7f6eserver_name\u7684\u8bdd,\u4f1a\u4f7f\u7528\u672c\u673a\u7684localhost\u8fdb\u884crewrite<\/p>\n<p>&#8211; \u524d\u9762\u63d0\u5230\u8fc7,rewrite\u7684\u6b63\u5219\u662f\u4e0d\u5339\u914dquery string\u7684,\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b,query string\u662f\u81ea\u52a8\u8ffd\u52a0\u5230rewrite\u540e\u7684\u5730\u5740\u4e0a\u7684,\u5982\u679c\u4e0d\u60f3\u81ea\u52a8\u8ffd\u52a0query string,\u5219\u5728rewrite\u5730\u5740\u7684\u672b\u5c3e\u6dfb\u52a0?<\/p>\n<pre class=\"brush:js\">rewrite  ^\/users\/(.*)$  \/show?user=$1?  last;<\/pre>\n<div><\/div>\n<p>rewrite\u7684\u57fa\u672c\u77e5\u8bc6\u5c31\u662f\u8fd9\u4e48\u591a..\u4f46\u8fd8\u6ca1\u6709\u5b8c..\u8fd8\u6709\u6700\u5934\u75bc\u7684\u90e8\u5206\u6ca1\u6709\u8bf4\u2026<\/p>\n<p>&nbsp;<\/p>\n<h2>\/2 Nginx location \u548c rewrite retry<\/h2>\n<p>&nbsp;<\/p>\n<p>nginx\u7684rewrite\u6709\u4e2a\u5f88\u5947\u7279\u7684\u7279\u6027 \u2014 rewrite\u540e\u7684url\u4f1a\u518d\u6b21\u8fdb\u884crewrite\u68c0\u67e5,\u6700\u591a\u91cd\u8bd510\u6b21,10\u6b21\u540e\u8fd8\u6ca1\u6709\u7ec8\u6b62\u7684\u8bdd\u5c31\u4f1a\u8fd4\u56deHTTP 500<\/p>\n<p>\u7528\u8fc7nginx\u7684\u670b\u53cb\u90fd\u77e5\u9053location\u533a\u5757,location\u533a\u5757\u6709\u70b9\u50cfApache\u4e2d\u7684RewriteBase,\u4f46\u5bf9\u4e8enginx\u6765\u8bf4location\u662f\u63a7\u5236\u7684\u7ea7\u522b\u800c\u5df2,\u91cc\u9762\u7684\u5185\u5bb9\u4e0d\u4ec5\u4ec5\u662frewrite.<\/p>\n<p>\u8fd9\u91cc\u5fc5\u987b\u7a0d\u5fae\u5148\u8bb2\u4e00\u70b9location\u7684\u77e5\u8bc6.location\u662fnginx\u7528\u6765\u5904\u7406\u5bf9\u540c\u4e00\u4e2aserver\u4e0d\u540c\u7684\u8bf7\u6c42\u5730\u5740\u4f7f\u7528\u72ec\u7acb\u7684\u914d\u7f6e\u7684\u65b9\u5f0f<\/p>\n<p>\u4e3e\u4f8b:<\/p>\n<pre class=\"brush:js\">location  = \/ {\r\n  ....\u914d\u7f6eA\r\n}\r\n\r\nlocation  \/ {\r\n  ....\u914d\u7f6eB\r\n}\r\n\r\nlocation ^~ \/images\/ {\r\n  ....\u914d\u7f6eC\r\n}\r\n\r\nlocation ~* \\.(gif|jpg|jpeg)$ {\r\n  ....\u914d\u7f6eD\r\n}<\/pre>\n<div><\/div>\n<p>\u8bbf\u95ee \/ \u4f1a\u4f7f\u7528\u914d\u7f6eA<br \/>\n\u8bbf\u95ee \/documents\/document.html \u4f1a\u4f7f\u7528\u914d\u7f6eB<br \/>\n\u8bbf\u95ee \/images\/1.gif \u4f1a\u4f7f\u7528\u914d\u7f6eC<br \/>\n\u8bbf\u95ee \/documents\/1.jpg \u4f1a\u4f7f\u7528\u914d\u7f6eD<\/p>\n<p>\u5982\u4f55\u5224\u65ad\u547d\u4e2d\u54ea\u4e2alocation\u6682\u4e14\u6309\u4e0b\u4e0d\u5a4a, \u6211\u4eec\u5728\u5b9e\u6218\u7bc7\u518d\u56de\u5934\u6765\u770b\u8fd9\u4e2a\u95ee\u9898.<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u53ea\u9700\u8981\u660e\u767d\u4e00\u4e2a\u60c5\u51b5: nginx\u53ef\u4ee5\u6709\u591a\u4e2alocation\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u914d.<\/p>\n<p>sever\u533a\u5757\u4e2d\u5982\u679c\u6709\u5305\u542brewrite\u89c4\u5219,\u5219\u4f1a\u6700\u5148\u6267\u884c,\u800c\u4e14\u53ea\u4f1a\u6267\u884c\u4e00\u6b21, \u7136\u540e\u518d\u5224\u65ad\u547d\u4e2d\u54ea\u4e2alocation\u7684\u914d\u7f6e,\u518d\u53bb\u6267\u884c\u8be5location\u4e2d\u7684rewrite, \u5f53\u8be5location\u4e2d\u7684rewrite\u6267\u884c\u5b8c\u6bd5\u65f6,rewrite\u5e76\u4e0d\u4f1a\u505c\u6b62,\u800c\u662f\u6839\u636erewrite\u8fc7\u7684URL\u518d\u6b21\u5224\u65adlocation\u5e76\u6267\u884c\u5176\u4e2d\u7684\u914d\u7f6e. \u90a3\u4e48,\u8fd9\u91cc\u5c31\u5b58\u5728\u4e00\u4e2a\u95ee\u9898,\u5982\u679crewrite\u5199\u7684\u4e0d\u6b63\u786e\u7684\u8bdd,\u662f\u4f1a\u5728location\u533a\u5757\u95f4\u9020\u6210\u65e0\u9650\u5faa\u73af\u7684.\u6240\u4ee5nginx\u624d\u4f1a\u52a0\u4e00\u4e2a\u6700\u591a\u91cd\u8bd510\u6b21\u7684\u4e0a\u9650. \u6bd4\u5982\u8fd9\u4e2a\u4f8b\u5b50<\/p>\n<pre class=\"brush:js\">location \/download\/ {\r\n  rewrite  ^(\/download\/.*)\/media\/(.*)\\..*$  $1\/mp3\/$2.mp3  last;\r\n}<\/pre>\n<div><\/div>\n<p>\u5982\u679c\u8bf7\u6c42\u4e3a \/download\/eva\/media\/op1.mp3 \u5219\u8bf7\u6c42\u88abrewrite\u5230 \/download\/eva\/mp3\/op1.mp3<\/p>\n<p>\u7ed3\u679crewrite\u7684\u7ed3\u679c\u91cd\u65b0\u547d\u4e2d\u4e86location \/download\/ \u867d\u7136\u8fd9\u6b21\u5e76\u6ca1\u6709\u547d\u4e2drewrite\u89c4\u5219\u7684\u6b63\u5219\u8868\u8fbe\u5f0f,\u4f46\u56e0\u4e3a\u7f3a\u5c11\u7ec8\u6b62rewrite\u7684\u6807\u5fd7,\u5176\u4ecd\u4f1a\u4e0d\u505c\u91cd\u8bd5download\u4e2drewrite\u89c4\u5219\u76f4\u5230\u8fbe\u523010\u6b21\u4e0a\u9650\u8fd4\u56deHTTP 500<\/p>\n<p>\u8ba4\u771f\u7684\u670b\u53cb\u8fd9\u65f6\u5c31\u4f1a\u95ee\u4e86,\u4e0a\u9762\u7684rewrite\u89c4\u5219\u4e0d\u662f\u6709\u6807\u5fd7\u4f4dlast\u4e48? last\u4e0d\u662f\u7ec8\u6b62rewrite\u7684\u610f\u601d\u4e48?<\/p>\n<p>\u8bf4\u5230\u8fd9\u91cc\u6211\u5c31\u8981\u62b1\u6028\u4e0b\u4e86,\u7f51\u4e0a\u80fd\u627e\u5230\u5173\u4e8enginx rewrite\u7684\u6587\u7ae0\u4e2d80%\u5bf9last\u6807\u5fd7\u7684\u89e3\u91ca\u90fd\u662f<\/p>\n<blockquote><p>last \u2013 \u57fa\u672c\u4e0a\u90fd\u7528\u8fd9\u4e2aFlag<\/p><\/blockquote>\n<p>\u2026\u2026\u8fd9\u4ed6\u5988\u5751\u7239\u5462!!! \u4ec0\u4e48\u53eb\u57fa\u672c\u4e0a\u90fd\u7528? \u4ec0\u4e48\u662f\u4e0d\u57fa\u672c\u7684\u60c5\u51b5?\u00a0 =\u76bf=<\/p>\n<p>\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u653e\u72d7\u201d\u57fa\u672c\u4e0a\u90fd\u7528\u8fd9\u4e2aFlag\u201d\u2026<\/p>\n<p>\u6211\u6700\u7ec8\u8fd8\u662f\u5728stack overflow\u627e\u5230\u4e86\u7b54\u6848:<\/p>\n<p>last\u548cbreak\u6700\u5927\u7684\u4e0d\u540c\u5728\u4e8e<\/p>\n<p>&#8211; break\u662f\u7ec8\u6b62\u5f53\u524dlocation\u7684rewrite\u68c0\u6d4b,\u800c\u4e14\u4e0d\u518d\u8fdb\u884clocation\u5339\u914d<br \/>\n\u2013 last\u662f\u7ec8\u6b62\u5f53\u524dlocation\u7684rewrite\u68c0\u6d4b,\u4f46\u4f1a\u7ee7\u7eed\u91cd\u8bd5location\u5339\u914d\u5e76\u5904\u7406\u533a\u5757\u4e2d\u7684rewrite\u89c4\u5219<\/p>\n<p>\u8fd8\u662f\u8fd9\u4e2a\u8be5\u6b7b\u7684\u4f8b\u5b50<\/p>\n<pre class=\"brush:js\">location \/download\/ {\r\n  rewrite  ^(\/download\/.*)\/media\/(.*)\\..*$  $1\/mp3\/$2.mp3  ;\r\n  rewrite  ^(\/download\/.*)\/movie\/(.*)\\..*$  $1\/avi\/$2.mp3  ;\r\n  rewrite  ^(\/download\/.*)\/avvvv\/(.*)\\..*$  $1\/rmvb\/$2.mp3 ;\r\n}<\/pre>\n<div><\/div>\n<p>\u4e0a\u9762\u6ca1\u6709\u5199\u6807\u5fd7\u4f4d,\u8bf7\u5404\u4f4d\u81ea\u884c\u8111\u8865\u2026<\/p>\n<p>\u5982\u679c\u8bf7\u6c42\u4e3a \/download\/acg\/moive\/UBW.avi<\/p>\n<p>last\u7684\u60c5\u51b5\u662f: \u5728\u7b2c2\u884crewrite\u5904\u7ec8\u6b62,\u5e76\u91cd\u8bd5location \/download..\u6b7b\u5faa\u73af<br \/>\nbreak\u7684\u60c5\u51b5\u662f: \u5728\u7b2c2\u884crewrite\u5904\u7ec8\u6b62,\u5176\u7ed3\u679c\u4e3a\u6700\u7ec8\u7684rewrite\u5730\u5740.<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4,\u4e0a\u9762\u7684\u67d0\u4f4d\u8bd5\u56fe\u4e0b\u8f7deva op\u4e0d\u4f46\u6ca1\u4e0b\u5230\u53cd\u800c\u88abHTTP 500\u5c04\u4e86\u4e00\u8138\u7684\u4f8b\u5b50\u6b63\u662f\u56e0\u4e3a\u7528\u4e86last\u6807\u5fd7\u6240\u4ee5\u624d\u4f1a\u9020\u6210\u6b7b\u5faa\u73af,\u5982\u679c\u7528break\u5c31\u6ca1\u4e8b\u4e86.<\/p>\n<pre class=\"brush:js\">location \/download\/ {\r\n  rewrite  ^(\/download\/.*)\/media\/(.*)\\..*$  $1\/mp3\/$2.mp3  break;\r\n}<\/pre>\n<div><\/div>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898,\u6211\u4e2a\u4eba\u7684\u5efa\u8bae\u662f,\u5982\u679c\u662f\u5168\u5c40\u6027\u8d28\u7684rewrite,\u6700\u597d\u653e\u5728server\u533a\u5757\u4e2d\u5e76\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684location\u533a\u5757.location\u533a\u5757\u4e2d\u7684rewrite\u8981\u60f3\u6e05\u695a\u662f\u7528last\u8fd8\u662fbreak.<\/p>\n<p>\u6709\u4eba\u53ef\u80fd\u4f1a\u95ee,\u7528break\u4e0d\u5c31\u4e07\u65e0\u4e00\u5931\u4e86\u4e48?<\/p>\n<p>\u4e0d\u5bf9.\u6709\u4e9b\u60c5\u51b5\u662f\u8981\u7528last\u7684. \u5178\u578b\u7684\u4f8b\u5b50\u5c31\u662fwordpress\u7684permalink rewrite<\/p>\n<p>\u5e38\u89c1\u7684\u60c5\u51b5\u4e0b, wordpress\u7684rewrite\u662f\u653e\u5728location \/\u4e0b\u9762,\u5e76\u5c06\u8bf7\u6c42rewrite\u5230\/index.php<\/p>\n<p>\u8fd9\u65f6\u5982\u679c\u8fd9\u91cc\u4f7f\u7528break\u4e43\u5c31\u6302\u4e86,\u4e0d\u4fe1\u8bd5\u8bd5. \uff42\uff08\uffe3\u25bd\uffe3\uff09\uff44\u2026\u56e0\u4e3anginx\u8fd4\u56de\u7684\u662f\u6ca1\u6709\u89e3\u91ca\u7684index.php\u7684\u6e90\u7801\u2026<\/p>\n<p>\u8fd9\u91cc\u4e00\u5b9a\u8981\u4f7f\u7528last\u624d\u53ef\u4ee5\u5728\u7ed3\u675flocation \/ \u7684rewrite, \u5e76\u518d\u6b21\u547d\u4e2dlocation ~ \\.php$,\u5c06\u5176\u4ea4\u7ed9fastcgi\u8fdb\u884c\u89e3\u91ca.\u5176\u540e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u7684\u624d\u662f\u89e3\u91ca\u8fc7\u7684html\u4ee3\u7801.<\/p>\n<p>\u5173\u4e8enginx rewrite\u7684\u7b80\u4ecb\u5230\u8fd9\u91cc\u5c31\u5168\u90e8\u8bb2\u5b8c\u4e86,\u6c34\u5e73\u53ca\u5176\u6709\u9650,\u8bf7\u5927\u5bb6\u6307\u51fa\u9519\u6f0f\u2026<\/p>\n<p>&nbsp;<\/p>\n<h2>\/3 \u5b9e\u6218! WordPress\u7684Permalink+Supercache rewrite\u5b9e\u73b0<\/h2>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u4e2arewrite\u5199\u6cd5\u5176\u5b9e\u662f\u6765\u81easupercache\u4f5c\u8005\u672c\u5bb6\u7684\u67d0\u4e2a\u8bc4\u8bba\u4e2d,\u7f51\u4e0a\u5f88\u5bb9\u6613\u67e5\u5230,\u505a\u4e86\u4e00\u4e9b\u4fee\u6539. \u5148\u7ed9\u51fa\u8be5\u914d\u7f6e\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9..\u90e8\u5206\u5185\u5bb9\u7801\u6389\u4e86..\u7edd\u5bf9\u8def\u5f84\u4ec0\u4e48\u7684\u4f60\u77e5\u9053\u4e5f\u6ca1\u5565\u7528\u5bf9\u5427?<\/p>\n<div>\n<pre class=\"brush:js\">server {\r\n\tlisten   80;\r\n\tserver_name  cafeneko.info www.cafeneko.info;\r\n\r\n\taccess_log  ***;\r\n\terror_log   *** ;\r\n\r\n\troot   ***;\r\n\tindex  index.php;\r\n\r\n\tgzip_static on;\r\n\r\n\tif (-f $request_filename) {\r\n\t\tbreak;\r\n\t}\r\n\r\n\tset $supercache_file '';\r\n\tset $supercache_uri $request_uri;\r\n\r\n\tif ($request_method = POST) {\r\n\t\tset $supercache_uri '';\r\n\t}\r\n\r\n\tif ($query_string) {\r\n\t\tset $supercache_uri '';\r\n\t}\r\n\r\n\tif ($http_cookie ~* \"comment_author_|wordpress_logged_|wp-postpass_\" ) {\r\n\t\tset $supercache_uri '';\r\n\t}\r\n\r\n\tif ($supercache_uri ~ ^(.+)$) {\r\n\t\tset $supercache_file \/wp-content\/cache\/supercache\/$http_host\/$1index.html;\r\n\t}\r\n\r\n\tif (-f $document_root$supercache_file) {\r\n\t\trewrite ^(.*)$ $supercache_file break;\r\n\t}\r\n\r\n\tif (!-e $request_filename) {\r\n\t\trewrite . \/index.php last;\r\n\t}\r\n\r\n\tlocation ~ \\.php$ {\r\n\r\n\t\tfastcgi_pass   127.0.0.1:9000;\r\n\t\tfastcgi_index  index.php;\r\n\t\tfastcgi_param  SCRIPT_FILENAME  ***$fastcgi_script_name;\r\n\t\tinclude        fastcgi_params;\r\n\t}\r\n\r\n\tlocation ~ \/\\.ht {\r\n\t\tdeny  all;\r\n\t}\r\n}<\/pre>\n<\/div>\n<p>\u4e0b\u9762\u662f\u89e3\u91ca:<\/p>\n<div>\n<pre class=\"brush:js\">gzip_static on;<\/pre>\n<\/div>\n<p>\u5982\u679c\u6d4f\u89c8\u5668\u652f\u6301gzip,\u5219\u5728\u538b\u7f29\u524d\u5148\u5bfb\u627e\u662f\u5426\u5b58\u5728\u538b\u7f29\u597d\u7684\u540c\u540dgz\u6587\u4ef6\u907f\u514d\u518d\u6b21\u538b\u7f29\u6d6a\u8d39\u8d44\u6e90,\u914d\u5408supercache\u7684\u538b\u7f29\u529f\u80fd\u4e00\u8d77\u4f7f\u7528\u6548\u679c\u6700\u597d,\u76f8\u6bd4supercache\u539f\u751f\u7684Apache mod_rewrite\u5b9e\u73b0,nginx\u7684\u5b9e\u73b0\u7b80\u5355\u7684\u591a. Apache mod_rewrite\u8db3\u8db3\u7528\u4e86\u4e24\u5957\u770b\u8d77\u6765\u4e00\u6a21\u4e00\u6837\u7684\u6761\u4ef6\u5224\u65ad\u6765\u5206\u522brewrite\u652f\u6301gzip\u538b\u7f29\u548c\u4e0d\u652f\u6301\u7684\u60c5\u51b5.<\/p>\n<div>\n<pre class=\"brush:js\">if (-f $request_filename) {\r\n\tbreak;\r\n}<\/pre>\n<\/div>\n<p>\/\/\u5982\u679c\u662f\u76f4\u63a5\u8bf7\u6c42\u67d0\u4e2a\u771f\u5b9e\u5b58\u5728\u7684\u6587\u4ef6,\u5219\u7528break\u8bed\u53e5\u505c\u6b62rewrite\u68c0\u67e5<\/p>\n<div>\n<pre class=\"brush:js\">set $supercache_file '';\r\nset $supercache_uri $request_uri;<\/pre>\n<\/div>\n<p>\/\/\u7528$request_uri\u521d\u59cb\u5316\u53d8\u91cf $supercache_uri.<\/p>\n<div>\n<pre class=\"brush:js\">if ($request_method = POST) {\r\n\tset $supercache_uri '';\r\n}<\/pre>\n<\/div>\n<p>\/\/\u5982\u679c\u8bf7\u6c42\u65b9\u5f0f\u4e3aPOST,\u5219\u4e0d\u4f7f\u7528supercache.\u8fd9\u91cc\u7528\u6e05\u7a7a$supercache_uri\u7684\u65b9\u6cd5\u6765\u8df3\u8fc7\u68c0\u6d4b,\u4e0b\u9762\u4f1a\u770b\u5230<\/p>\n<div>\n<pre class=\"brush:js\">if ($query_string) {\r\n\tset $supercache_uri '';\r\n}<\/pre>\n<\/div>\n<p>\/\/\u56e0\u4e3a\u4f7f\u7528\u4e86rewrite\u7684\u539f\u56e0,\u6b63\u5e38\u60c5\u51b5\u4e0b\u4e0d\u5e94\u8be5\u6709query_string(\u4e00\u822c\u53ea\u6709\u540e\u53f0\u624d\u4f1a\u51fa\u73b0query string),\u6709\u7684\u8bdd\u5219\u4e0d\u4f7f\u7528supercache<\/p>\n<div>\n<pre class=\"brush:js\">if ($http_cookie ~* \"comment_author_|wordpress_logged_|wp-postpass_\" ) {\r\n\tset $supercache_uri '';\r\n}<\/pre>\n<\/div>\n<p>\/\/\u9ed8\u8ba4\u60c5\u51b5\u4e0b,supercache\u662f\u4ec5\u5bf9unknown user\u4f7f\u7528\u7684.\u5176\u4ed6\u8bf8\u5982\u767b\u5f55\u7528\u6237\u6216\u8005\u8bc4\u8bba\u8fc7\u7684\u7528\u6237\u5219\u4e0d\u4f7f\u7528.<\/p>\n<p>comment_author\u662f\u6d4b\u8bd5\u8bc4\u8bba\u7528\u6237\u7684cookie, wordpress_logged\u662f\u6d4b\u8bd5\u767b\u5f55\u7528\u6237\u7684cookie. wp-postpass\u4e0d\u5927\u6e05\u695a,\u5b57\u9762\u4e0a\u6765\u770b\u53ef\u80fd\u662f\u66fe\u7ecf\u53d1\u8868\u8fc7\u6587\u7ae0\u7684?\u53ea\u8981cookie\u4e2d\u542b\u6709\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5219\u6761\u4ef6\u6210\u7acb.<\/p>\n<p>\u539f\u6765\u7684\u5199\u6cd5\u4e2d\u68c0\u6d4b\u767b\u5f55\u7528\u6237cookie\u7528\u7684\u662fwordpress_,\u4f46\u662f\u6211\u5728\u6d4b\u8bd5\u4e2d\u53d1\u73b0\u767b\u5165\/\u767b\u51fa\u4ee5\u540e\u8fd8\u4f1a\u6709\u4e00\u4e2a\u53ebwordpress_test_cookie\u5b58\u5728,\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\u4f5c\u7528,\u6211\u4e5f\u4e0d\u6e05\u695a\u4e00\u822c\u7528\u6237\u662f\u5426\u4f1a\u4ea7\u751f\u8fd9\u4e2acookie.\u7531\u4e8e\u8003\u8651\u5230\u767b\u51fa\u4ee5\u540e\u8fd9\u4e2acookie\u4f9d\u7136\u5b58\u5728\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230cache\u7684\u5224\u65ad,\u4e8e\u662f\u628a\u8fd9\u91cc\u6539\u6210\u4e86\u5339\u914dwordpress_logged_<\/p>\n<div>\n<pre class=\"brush:js\">if ($supercache_uri ~ ^(.+)$) {\r\n\tset $supercache_file \/wp-content\/cache\/supercache\/$http_host$1index.html;\r\n}<\/pre>\n<\/div>\n<p>\/\/\u5982\u679c\u53d8\u91cf$supercache_uri\u4e0d\u4e3a\u7a7a,\u5219\u8bbe\u7f6ecache file\u7684\u8def\u5f84<\/p>\n<p>\u8fd9\u91cc\u7a0d\u5fae\u7559\u610f\u4e0b$http_host$1index.html\u8fd9\u4e32\u4e1c\u897f,\u5176\u5b9e\u5199\u6210 $http_host\/$1\/index.html \u5c31\u597d\u61c2\u5f88\u591a<\/p>\n<p>\u4ee5\u8fd9\u4e2arewrite\u5f62\u5f0f\u7684url\u4e3a\u4f8b<\/p>\n<blockquote><p>cafeneko.info\/2010\/09\/tsukihime-doujin_part01\/<\/p><\/blockquote>\n<p>\u5176\u4e2d<br \/>\n$http_host = \u2018cafeneko.info\u2019 , $1 = $request_uri = \u2018\/2010\/09\/tsukihime-doujin_part01\/\u2019<\/p>\n<p>\u5219 $http_host$1index.html = \u2018cafeneko.info\/2010\/09\/tsukihime-doujin_part01\/index.html\u2019<\/p>\n<p>\u800c $http_host\/$1\/index.html = \u2018cafeneko.info\/\/2010\/09\/tsukihime-doujin_part01\/\/index.html\u2019<\/p>\n<p>\u867d\u7136\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u4e24\u8005\u5e76\u6ca1\u6709\u4e0d\u540c,\u4e0d\u8fc7\u4e3a\u4e86\u4fdd\u6301\u6b63\u786e\u7684\u8def\u5f84,\u8fd8\u662f\u7701\u7565\u4e86\u4e2d\u95f4\u7684\/\u7b26\u53f7.<\/p>\n<p>\u6700\u540e\u4e0a\u4f8brewrite\u540e\u7684url = \u2018cafeneko.info\/wp-content\/cache\/supercache\/cafeneko.info\/2010\/09\/tsukihime-doujin_part01\/index.html\u2019<\/p>\n<div>\n<pre class=\"brush:js\">if (-f $document_root$supercache_file) {\r\n\trewrite ^(.*)$ $supercache_file break;\r\n}<\/pre>\n<\/div>\n<p>\/\/\u68c0\u67e5cache\u6587\u4ef6\u662f\u5426\u5b58\u5728,\u5b58\u5728\u7684\u8bdd\u5219\u6267\u884crewrite,\u7559\u610f\u8fd9\u91cc\u56e0\u4e3a\u662frewrite\u5230html\u9759\u6001\u6587\u4ef6,\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u7528break\u7ec8\u6b62\u6389.<\/p>\n<div>\n<pre class=\"brush:js\">if (!-e $request_filename) {\r\n\trewrite . \/index.php last;\r\n}<\/pre>\n<\/div>\n<p>\/\/\u6267\u884c\u5230\u6b64\u5219\u8bf4\u660e\u4e0d\u4f7f\u7528suercache,\u8fdb\u884cwordpress\u7684permalink rewrite<\/p>\n<p>\u68c0\u67e5\u8bf7\u6c42\u7684\u6587\u4ef6\/\u76ee\u5f55\u662f\u5426\u5b58\u5728,\u5982\u679c\u4e0d\u5b58\u5728\u5219\u6761\u4ef6\u6210\u7acb, rewrite\u5230index.php<\/p>\n<p>\u987a\u4fbf\u8bf4\u4e00\u53e5,\u5f53\u65f6\u8fd9\u91cc\u8fd9\u53e5rewrite\u770b\u7684\u6211\u767e\u601d\u4e0d\u5f97\u5176\u89e3. .<\/p>\n<p>\u53ea\u80fd\u5339\u914d\u4e00\u4e2a\u5b57\u7b26\u554a?\u8fd9\u662f\u4ec0\u4e48\u610f\u601d?<\/p>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b,\u60f3\u8c03\u8bd5nginx rewrite\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u628aflag\u5199\u6210redirect,\u8fd9\u6837\u5c31\u80fd\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u91cc\u770b\u5230\u771f\u5b9e\u7684rewrite\u5730\u5740.<\/p>\n<p>\u7136\u800c\u5bf9\u4e8epermalink rewrite\u5374\u4e0d\u80fd\u7528\u8fd9\u79cd\u65b9\u6cd5,\u56e0\u4e3a\u4e00\u65e6\u5199\u6210redirect\u4ee5\u540e,\u4e0d\u7ba1\u70b9\u4ec0\u4e48\u94fe\u63a5,\u53ea\u8981\u6ca1\u6709supercache,\u90fd\u662f\u8df3\u8f6c\u56de\u9996\u9875\u4e86.<\/p>\n<p>\u540e\u6765\u770b\u4e86\u4e00\u4e9b\u6587\u7ae0\u624d\u660e\u767d\u4e86rewrite\u7684\u672c\u8d28,\u5176\u5b9e\u662f\u5728\u4fdd\u6301\u8bf7\u6c42\u5730\u5740\u4e0d\u53d8\u7684\u60c5\u51b5\u4e0b,\u5728\u670d\u52a1\u5668\u7aef\u5c06\u8bf7\u6c42\u8f6c\u5230\u7279\u5b9a\u7684\u9875\u9762.<\/p>\n<p>\u4e4d\u4e00\u770bsupercache\u7684\u6027\u8d28\u6709\u70b9\u50cf302\u5230\u9759\u6001\u6587\u4ef6,\u6240\u4ee5\u53ef\u4ee5\u7528redirect\u8c03\u8bd5.<\/p>\n<p>\u4f46\u662fpermalink\u5374\u662f\u6027\u8d28\u5b8c\u5168\u4e0d\u540c\u7684rewrite,\u8fd9\u8ddfwordpress\u7684\u5904\u7406\u65b9\u5f0f\u6709\u5173. \u6211\u7814\u7a76\u4e0d\u6df1\u5c31\u4e0d\u591a\u8bf4\u4e86,\u7b80\u5355\u8bf4\u5c31\u662f\u4fdd\u6301URL\u4e0d\u53d8\u5c06\u8bf7\u6c42rewrite\u5230index.php,WP\u5c06\u5206\u6790\u5176URL\u7ed3\u6784\u518d\u5bf9\u5176\u5e76\u8fdb\u884c\u5339\u914d(\u6587\u7ae0,\u9875\u9762,tag\u7b49),\u7136\u540e\u518d\u6784\u5efa\u9875\u9762. \u6240\u4ee5\u5176\u5b9e\u8fd9\u6761rewrite<\/p>\n<div>\n<pre class=\"brush:js\">rewrite . \/index.php last;<\/pre>\n<\/div>\n<p>\u8bf4\u7684\u662f,\u4efb\u4f55\u8bf7\u6c42\u90fd\u4f1a\u88abrewrite\u5230index.php.\u56e0\u4e3a\u201d.\u201d\u5339\u914d\u4efb\u610f\u5b57\u7b26,\u6240\u4ee5\u8fd9\u6761rewrite\u5176\u5b9e\u53ef\u4ee5\u5199\u6210\u4efb\u4f55\u5f62\u5f0f\u7684\u80fd\u4efb\u610f\u547d\u4e2d\u7684\u6b63\u5219.\u6bd4\u5982\u8bf4<\/p>\n<div>\n<pre class=\"brush:js\">rewrite . \/index.php last;\r\nrewrite ^ \/index.php last;\r\nrewrite .* \/index.php last;<\/pre>\n<\/div>\n<p>\u6548\u679c\u90fd\u662f\u4e00\u6837\u7684,\u90fd\u80fd\u505a\u5230permalink rewrite.<\/p>\n<p>\u6700\u540e\u8981\u63d0\u7684\u5c31\u662f\u6709\u4eba\u53ef\u80fd\u6ce8\u610f\u5230\u6211\u7684rewrite\u89c4\u5219\u662f\u653e\u5728server\u5757\u4e2d\u7684.\u7f51\u4e0a\u80fd\u627e\u5230\u7684\u5927\u591a\u6570\u5173\u4e8ewordpress\u7684nginx rewrite\u89c4\u5219\u90fd\u662f\u653e\u5728location \/\u4e0b\u9762\u7684,\u4f46\u662f\u4e0a\u9762\u6211\u5374\u653e\u5728\u4e86server\u5757\u4e2d,\u4e3a\u4f55?<\/p>\n<p>\u539f\u56e0\u662fWP\u6216\u67d0\u4e2a\u63d2\u4ef6\u4f1a\u5728\u5f53\u524d\u9875\u9762\u505a\u4e00\u4e2aPOST\u7684XHR\u8bf7\u6c42,\u672c\u6765\u6ca1\u4ec0\u4e48\u7279\u522b,\u4f46\u95ee\u9898\u5c31\u51fa\u5728\u5176XHR\u8bf7\u6c42\u7684URL\u7ed3\u6784\u4e0a.<\/p>\n<p>\u6b63\u5e38\u7684permalink\u4e00\u822c\u4e3a: domain.com\/year\/month\/postname\/ \u6216\u8005 domain.com\/tags\/tagname\/ \u4e4b\u7c7b.<\/p>\n<p>\u4f46\u8fd9\u4e2aXHR\u8bf7\u6c42\u7684URL\u5374\u662f domain.com\/year\/month\/postname\/index.php \u6216\u8005 domain.com\/tags\/tagname\/index.php<\/p>\n<p>\u8fd9\u6837\u4e00\u6765\u5c31\u547d\u4e2d\u4e86location ~ \\.php$\u800c\u4ea4\u7ed9fastcgi,\u4f46\u56e0\u4e3a\u6839\u672c\u6ca1\u6709\u505a\u8fc7rewrite\u5176\u9875\u9762\u4e0d\u53ef\u80fd\u5b58\u5728,\u7ed3\u679c\u5c31\u662f\u8fd9\u4e2aXHR\u8fd4\u56de\u4e00\u4e2a404<\/p>\n<p>\u9274\u4e8elocation\u4e4b\u95f4\u5339\u914d\u4f18\u5148\u7ea7\u7684\u539f\u56e0,\u6211\u5c06\u4e3b\u8981\u7684rewrite\u529f\u80fd\u5168\u90e8\u653e\u8fdb\u4e86server\u533a\u5757\u4e2d,\u8fd9\u6837\u5c31\u5f97\u4ee5\u4fdd\u8bc1\u5728\u8fdb\u884clocation\u5339\u914d\u4e4b\u524d\u662f\u4e00\u5b9a\u505a\u8fc7rewrite\u7684.<\/p>\n<p>\u8fd9\u65f6\u6709\u670b\u53cb\u53c8\u8981\u95ee\u4e86,\u4e3a\u4ec0\u4e48\u547d\u4e2d\u7684\u662flocation ~ \\.php$\u800c\u4e0d\u662flocation \/ ?<\/p>\n<p>\u2026\u671b\u5929\u2026\u957f\u53f9\u2026\u8fd9\u5c31\u8981\u626f\u5230\u5929\u6740\u7684location\u5339\u914d\u95ee\u9898\u4e86\u2026.<\/p>\n<p>locatoin\u5e76\u975e\u50cfrewrite\u90a3\u6837\u9010\u6761\u6267\u884c,\u800c\u662f\u6709\u7740\u5339\u914d\u4f18\u5148\u7ea7\u7684,\u5f53\u4e00\u6761\u8bf7\u6c42\u540c\u65f6\u6ee1\u8db3\u51e0\u4e2alocation\u7684\u5339\u914d\u65f6,\u5176\u53ea\u4f1a\u9009\u62e9\u5176\u4e00\u7684\u914d\u7f6e\u6267\u884c.<\/p>\n<p>\u5176\u5bfb\u627e\u7684\u65b9\u6cd5\u4e3a:<\/p>\n<blockquote><p>1. \u9996\u5148\u5bfb\u627e\u6240\u6709\u7684\u5e38\u91cf\u5339\u914d,\u5982location \/, location \/av\/, \u4ee5\u76f8\u5bf9\u8def\u5f84\u81ea\u5de6\u5411\u53f3\u5339\u914d,\u5339\u914d\u957f\u5ea6\u6700\u9ad8\u7684\u4f1a\u88ab\u4f7f\u7528,<br \/>\n2. \u7136\u540e\u6309\u7167\u914d\u7f6e\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u4f9d\u6b21\u6d4b\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f,\u5982 location ~ download\\\/$, location ~* \\.wtf, \u7b2c\u4e00\u4e2a\u5339\u914d\u4f1a\u88ab\u4f7f\u7528<br \/>\n3. \u5982\u679c\u6ca1\u6709\u5339\u914d\u7684\u6b63\u5219,\u5219\u4f7f\u7528\u4e4b\u524d\u7684\u5e38\u91cf\u5339\u914d<\/p><\/blockquote>\n<p>\u800c\u4e0b\u9762\u51e0\u79cd\u65b9\u6cd5\u5f53\u5339\u914d\u65f6\u4f1a\u7acb\u5373\u7ec8\u6b62\u5176\u4ed6location\u7684\u5c1d\u8bd5<\/p>\n<blockquote><p>1. = \u5b8c\u5168\u5339\u914d,location = \/download\/<br \/>\n2. ^~ \u7ec8\u6b62\u6b63\u5219\u6d4b\u8bd5,\u5982location ^~ \/download\/ \u5982\u679c\u8fd9\u6761\u662f\u6700\u957f\u5339\u914d,\u5219\u7ec8\u6b62\u6b63\u5219\u6d4b\u8bd5,\u8fd9\u4e2a\u7b26\u53f7\u53ea\u80fd\u5339\u914d\u5e38\u91cf<br \/>\n3. \u5728\u6ca1\u6709=\u6216\u8005^~\u7684\u60c5\u51b5\u4e0b,\u5982\u679c\u5e38\u91cf\u5b8c\u5168\u5339\u914d,\u4e5f\u4f1a\u7acb\u5373\u7ec8\u6b62\u6d4b\u8bd5,\u6bd4\u5982\u8bf7\u6c42\u4e3a \/download\/ \u4f1a\u5b8c\u5168\u547d\u4e2dlocation \/download\/\u800c\u4e0d\u7ee7\u7eed\u5176\u4ed6\u7684\u6b63\u5219\u6d4b\u8bd5<\/p><\/blockquote>\n<p>\u603b\u7ed3:<\/p>\n<blockquote><p>1. \u5982\u679c\u5b8c\u5168\u5339\u914d(\u4e0d\u7ba1\u6709\u6ca1\u6709=),\u5c1d\u8bd5\u4f1a\u7acb\u5373\u7ec8\u6b62<br \/>\n2. \u4ee5\u6700\u957f\u5339\u914d\u6d4b\u8bd5\u5404\u4e2a\u5e38\u91cf,\u5982\u679c\u5e38\u91cf\u5339\u914d\u5e76\u6709 ^~, \u5c1d\u8bd5\u4f1a\u7ec8\u6b62<br \/>\n3. \u6309\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u6d4b\u8bd5\u5404\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f<br \/>\n4. \u5982\u679c\u7b2c3\u6b65\u6709\u547d\u4e2d,\u5219\u4f7f\u7528\u5176\u5339\u914dlocation,\u5426\u5219\u4f7f\u7528\u7b2c2\u6b65\u7684location<\/p><\/blockquote>\n<p>\u53e6\u5916\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u79cd\u7279\u6b8a\u7684named location,\u4ee5@\u5f00\u5934,\u5982location @thisissparta \u4e0d\u8fc7\u8fd9\u79cdlocation\u5b9a\u4e49\u4e0d\u7528\u4e8e\u4e00\u822c\u7684\u5904\u7406,\u800c\u662f\u4e13\u95e8\u7528\u4e8etry_file, error_page\u7684\u5904\u7406,\u8fd9\u91cc\u4e0d\u518d\u6df1\u5165.<\/p>\n<p>\u6655\u4e86\u6ca1? \u7528\u524d\u6587\u7684\u4f8b\u5b50\u6765\u770b\u770b<\/p>\n<div>\n<pre class=\"brush:js\">location  = \/ {\r\n  ....\u914d\u7f6eA\r\n}\r\n\r\nlocation  \/ {\r\n  ....\u914d\u7f6eB\r\n}\r\n\r\nlocation ^~ \/images\/ {\r\n  ....\u914d\u7f6eC\r\n}\r\n\r\nlocation ~* \\.(gif|jpg|jpeg)$ {\r\n  ....\u914d\u7f6eD\r\n}<\/pre>\n<\/div>\n<p>\u8bbf\u95ee \/ \u4f1a\u4f7f\u7528\u914d\u7f6eA -&gt; \u5b8c\u5168\u547d\u4e2d<br \/>\n\u8bbf\u95ee \/documents\/document.html \u4f1a\u4f7f\u7528\u914d\u7f6eB -&gt; \u5339\u914d\u5e38\u91cfB,\u4e0d\u5339\u914d\u6b63\u5219C\u548cD,\u6240\u4ee5\u7528B<br \/>\n\u8bbf\u95ee \/images\/1.gif \u4f1a\u4f7f\u7528\u914d\u7f6eC -&gt; \u5339\u914d\u5e38\u91cfB,\u5339\u914d\u6b63\u5219C,\u4f7f\u7528\u9996\u4e2a\u547d\u4e2d\u7684\u6b63\u5219,\u6240\u4ee5\u7528C<br \/>\n\u8bbf\u95ee \/documents\/1.jpg \u4f1a\u4f7f\u7528\u914d\u7f6eD -&gt; \u5339\u914d\u5e38\u91cfB,\u4e0d\u5339\u914d\u6b63\u5219C,\u5339\u914d\u6b63\u5219D,\u4f7f\u7528\u9996\u4e2a\u547d\u4e2d\u7684\u6b63\u5219,\u6240\u4ee5\u7528D<\/p>\n<p>\u90a3\u4e48\u518d\u56de\u5934\u770b\u6211\u4eec\u521a\u624d\u8bf4\u7684\u95ee\u9898.\u4e3a\u4ec0\u4e48\u90a3\u4e2aURL\u7ed3\u679c\u5947\u602a\u7684XHR\u8bf7\u6c42\u4f1a\u547d\u4e2dlocation ~ \\.php$\u800c\u4e0d\u662flocation \/ ? \u6211\u76f8\u4fe1\u4f60\u5e94\u8be5\u5df2\u7ecf\u77e5\u9053\u7b54\u6848\u4e86.<\/p>\n<p>\u6240\u4ee5\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u628arewrite\u89c4\u5219\u653e\u5728\u6bd4location\u5148\u6267\u884c\u7684server\u5757\u91cc\u9762\u5c31\u53ef\u4ee5\u4e86\u54df.<\/p>\n<p>\u8fd9\u6b21\u7684\u7814\u7a76\u7b14\u8bb0\u5c31\u5230\u6b64\u4e3a\u6b62\u4e86.<\/p>\n<p>\u6700\u540e\u7559\u4e00\u4e2a\u601d\u8003\u9898,\u5982\u679c\u4e0d\u5c06rewrite\u89c4\u5219\u653e\u5165server\u5757,\u8fd8\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2aXHR 404\u7684\u95ee\u9898?<\/p>\n<p>\u539f\u6765\u7684location \/\u5757\u5305\u542b\u4ecelocation ~ \\.php$\u5230root\u4e3a\u6b62\u7684\u90e8\u5206.<\/p>\n<p>\u7b54\u6848\u662f\u5b58\u5728\u7684.\u5728\u7528\u4f7f\u7528\u76ee\u524d\u7684\u65b9\u6cd5\u524d\u6211\u6b7b\u8111\u7b4b\u7684\u5728\u4fdd\u7559location \/\u7684\u524d\u63d0\u4e0b\u5c1d\u8bd5\u4e86\u5f88\u591a\u79cd\u65b9\u6cd5\u2026\u8bf7\u4e0d\u8981\u5c1d\u8bd5\u4e3a\u5404\u79cdpermalink\u6784\u5efa\u72ec\u7acb\u7684location.\u56e0\u4e3awp\u7684permalink\u79cd\u7c7b\u5f88\u591a,\u5305\u62ec\u5355\u7bc7\u6587\u7ae0,\u9875\u9762,\u5206\u7c7b,tag,\u4f5c\u8005,\u5b58\u6863\u7b49\u7b49..\u6b22\u8fce\u5728\u56de\u590d\u4e2d\u8ba8\u8bba \/<\/p>\n<blockquote><p>\u53c2\u8003:<br \/>\n<a href=\"http:\/\/wiki.nginx.org\/\" target=\"_blank\">Nginx wiki<\/a><\/p><\/blockquote>\n<p>-EOF-<\/p>\n<p>&nbsp;<\/p>\n<p>\u66f4\u65b0\u00a0 @2010.10.23<\/p>\n<p>\u4e4b\u524d\u7684supercache rewrite\u89c4\u5219\u9002\u7528\u4e8e\u5927\u90e8\u5206\u7684WP.\u4f46\u662f\u5e76\u4e0d\u9002\u7528\u4e8emobile press\u63d2\u4ef6\u7684\u79fb\u52a8\u8bbe\u5907\u652f\u6301.<\/p>\n<p>\u56e0\u4e3a\u5176\u4e2d\u5e76\u6ca1\u6709\u68c0\u6d4b\u79fb\u52a8\u8bbe\u5907\u7684user agent,\u4ece\u800c\u5bfc\u81f4\u79fb\u52a8\u8bbe\u5907\u4e5f\u4f1a\u88abrewrite\u5230cache\u4e0a.\u8fd9\u6837\u7684\u7ed3\u679c\u662f\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u4e5f\u662f\u770b\u5230\u7684\u8ddfPC\u4e00\u6837\u7684\u5b8c\u5168\u7248blog. \u5bf9\u4e8e\u6027\u80fd\u6bd4\u8f83\u597d\u7684\u624b\u673a\u6bd4\u5982iphone\u5b89\u5353\u4ec0\u4e48\u7684\u5927\u6982\u6ca1\u4ec0\u4e48\u95ee\u9898,\u4f46\u6bd4\u8f83\u4e00\u822c\u7684\u6bd4\u5982nokia\u4e0a\u7528opera mini\u7b49\u770b\u5c31\u4f1a\u6bd4\u8f83\u8f9b\u82e6\u4e86,\u8fd9\u6b21\u628asupercache\u539f\u672c\u5728htaccess\u4e2d\u7684\u79fb\u52a8\u8bbe\u5907\u68c0\u6d4b\u7684\u4ee3\u7801\u5757\u4e5f\u79fb\u690d\u4e86\u8fc7\u6765.<\/p>\n<p>\u5728\u524d\u6587\u7684\u914d\u7f6e\u6587\u4ef6\u4e2dcookie\u68c0\u6d4b\u540e\u9762\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\u6bb5<\/p>\n<div>\n<pre class=\"brush:js\">\t# Bypass special user agent\r\n\tif ($http_user_agent ~* \"2.0 MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine\/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA\/WX310K|LG\/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo Wii|Nitro|Nokia|Opera Mini|Palm|PlayStation Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows CE|WinWAP|YahooSeeker\/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915 Obigo|LGE VX|webOS|Nokia5800\") {\r\n\t\tset $supercache_uri '';\r\n\t}\r\n\r\n\tif ($http_user_agent ~* \"w3c |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg\/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\\ |xda-\") {\r\n\t\tset $supercache_uri '';\r\n\t}<\/pre>\n<\/div>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5bf9\u79fb\u52a8\u8bbe\u5907\u7ed5\u5f00cache\u89c4\u5219,\u800c\u76f4\u63a5\u4f7f\u7528mobile press\u4ea7\u751f\u7684\u79fb\u52a8\u7248\u7684\u6548\u679c\u4e86.<\/p>\n<p>\u8f6c\u81ea\uff1ahttp:\/\/blog.cafeneko.info\/2010\/10\/nginx_rewrite_note\/<\/p>\n<p>\u4ed8\uff1a<\/p>\n<p>codeigniter nginx rewrite:<\/p>\n<div>\n<pre class=\"brush:js\">  if ($request_filename ~* [\/\\w\\.]*\\.(png|jpg|gif|css|js) ) {\r\n        break;\r\n    }\r\n  rewrite ^\/(.*)$ \/index.php?\/$1 last;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u65b0\u4e3b\u673a\u7684\u8fc1\u79fb\u8fc7\u7a0b\u4e2d,\u6700\u5927\u7684\u56f0\u96be\u5c31\u662fWP permalink rew&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,7,18],"tags":[131,45,48,58],"class_list":["post-1009","post","type-post","status-publish","format-standard","hentry","category-Linux","category-NetOS","category-Php","tag-codeigniter","tag-linux-2","tag-nginx","tag-rewrite"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1009"}],"version-history":[{"count":3,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1009\/revisions"}],"predecessor-version":[{"id":1011,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1009\/revisions\/1011"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}