{"id":328,"date":"2013-06-20T14:35:02","date_gmt":"2013-06-20T06:35:02","guid":{"rendered":"http:\/\/www.ln.la\/?p=328"},"modified":"2013-06-20T14:35:02","modified_gmt":"2013-06-20T06:35:02","slug":"configure-nginx-rewrite-rules","status":"publish","type":"post","link":"https:\/\/www.lnlog.cn\/index.php\/2013\/06\/20\/configure-nginx-rewrite-rules\/","title":{"rendered":"Nginx\u914d\u7f6e\u53caRewrite\u89c4\u5219"},"content":{"rendered":"<p>Nginx Rewrite \u89c4\u5219\u76f8\u5173\u6307\u4ee4 \u76f8\u5173\u6307\u4ee4\u6709if,rewrite,set,return,break\u7b49\uff0c\u5176\u4e2d\u6700\u5173\u952e\u7684\u5c31\u662frewrite.\u4e00\u4e2a\u7b80\u5355\u7684Nginx Rewrite\u89c4\u5219\u8bed\u6cd5\u5982\u4e0b\uff1arewrite ^\/b\/(.*).html \/play.php?video=$1 break;<br \/>\n<b>1.break\u6307\u4ee4<\/b> \u9ed8\u8ba4\u503c\uff1anone ;\u4f7f\u7528\u73af\u5883\uff1aserver,location,if ; \u8be5\u6307\u4ee4\u7684\u4f5c\u7528\u662f\u5b8c\u6210\u5f53\u524d\u7684\u89c4\u5219\u96c6\uff0c\u4e0d\u518d\u5904\u7406rewrite\u6307\u4ee4\u3002<br \/>\n<b>2.if\u6307\u4ee4<\/b> \u9ed8\u8ba4\u503c\uff1anone ;\u4f7f\u7528\u73af\u5883\uff1aserver,location \u8be5\u6307\u4ee4\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u6761\u4ef6\u662f\u5426\u7b26\u5408\uff0c\u5982\u679c\u6761\u4ef6\u7b26\u5408\uff0c\u5219\u6267\u884c\u5927\u62ec\u53f7\u5185\u7684\u8bed\u53e5\u3002If\u6307\u4ee4\u4e0d\u652f\u6301\u5d4c\u5957\uff0c\u4e0d\u652f\u6301\u591a\u4e2a\u6761\u4ef6&amp;&amp;\u548c||\u5904\u7406\u3002<\/p>\n<ul>\n<li>A.\u53d8\u91cf\u540d\uff0c\u9519\u8bef\u7684\u503c\u5305\u62ec\uff1a\u7a7a\u5b57\u7b26\u4e32&#8221;&#8221;\u6216\u8005\u4efb\u4f55\u4ee50\u5f00\u59cb\u7684\u5b57\u7b26\u4e32<\/li>\n<li>B.\u53d8\u91cf\u6bd4\u8f83\u53ef\u4ee5\u4f7f\u7528&#8221;=&#8221;(\u8868\u793a\u7b49\u4e8e)\u548c&#8221;!=&#8221;(\u8868\u793a\u4e0d\u7b49\u4e8e)<\/li>\n<li>C.\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u5339\u914d\u53ef\u4ee5\u4f7f\u7528&#8221;~*&#8221;\u548c&#8221;~&#8221;\u7b26\u53f7<\/li>\n<li>D.&#8221;~&#8221;\u7b26\u53f7\u8868\u793a\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u5339\u914d<\/li>\n<li>E.&#8221;~*&#8221;\u7b26\u53f7\u8868\u793a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u5339\u914d<\/li>\n<li>F.&#8221;!~&#8221;\u548c&#8221;!~*&#8221;\u7b26\u53f7\u7684\u4f5c\u7528\u521a\u597d\u548c&#8221;~&#8221;\u3001&#8221;~*&#8221;\u76f8\u53cd\uff0c\u8868\u793a\u4e0d\u5339\u914d<\/li>\n<li>G.&#8221;-f&#8221;\u548c&#8221;!-f&#8221;\u7528\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/li>\n<li>H.&#8221;-d&#8221;\u548c&#8221;!-d&#8221;\u7528\u6765\u5224\u65ad\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/li>\n<li>I.&#8221;-e&#8221;\u548c&#8221;!-e&#8221;\u7528\u6765\u5224\u65ad\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/li>\n<li>J.&#8221;-x&#8221;\u548c&#8221;!-x&#8221;\u7528\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u53ef\u6267\u884c<\/li>\n<li>K.\u90e8\u5206\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728()\u5185\uff0c\u7528$1~$9\u6765\u8bbf\u95ee<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p><b>3.return\u6307\u4ee4<\/b> \u8bed\u6cd5\uff1areturn code ;\u4f7f\u7528\u73af\u5883\uff1aserver,location,if ; \u8be5\u6307\u4ee4\u7528\u4e8e\u7ed3\u675f\u89c4\u5219\u7684\u6267\u884c\u5e76\u8fd4\u56de\u72b6\u6001\u7801\u7ed9\u5ba2\u6237\u7aef\u3002 \u793a\u4f8b\uff1a\u5982\u679c\u8bbf\u95ee\u7684URL\u4ee5&#8221;.sh&#8221;\u6216&#8221;.bash&#8221;\u7ed3\u5c3e\uff0c\u5219\u8fd4\u56de403\u72b6\u6001\u7801location ~ .*.(sh|bash)?${return 403;} <b>4.rewrite \u6307\u4ee4<\/b> \u8bed\u6cd5\uff1arewrite regex replacement flag \u9ed8\u8ba4\u503c\uff1anone ; \u4f7f\u7528\u73af\u5883\uff1aserver,location,if \u8be5\u6307\u4ee4\u6839\u636e\u8868\u8fbe\u5f0f\u6765\u91cd\u5b9a\u5411URI\uff0c\u6216\u8005\u4fee\u6539\u5b57\u7b26\u4e32\u3002\u6307\u4ee4\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u987a\u5e8f\u6765\u6267\u884c\u3002\u6ce8\u610f\u91cd\u5199\u8868\u8fbe\u5f0f\u53ea\u5bf9\u76f8\u5bf9\u8def\u5f84\u6709\u6548\u3002\u5982\u679c\u4f60\u60f3\u914d\u5bf9\u4e3b\u673a\u540d\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528if\u8bed\u53e5\uff0c\u793a\u4f8b\u5982\u4e0b\uff1aif( $host ~* www.(.*) ){set $host_without_www $1;rewrite\u00a0\u00a0^(.*)$\u00a0\u00a0http:\/\/$host_without_www$1 permanent;}<br \/>\nrewrite\u6307\u4ee4\u7684\u6700\u540e\u4e00\u9879\u53c2\u6570\u4e3aflag\u6807\u8bb0\uff0c\u652f\u6301flag\u6807\u8bb0\u6709\uff1a 1.last\u00a0\u00a0\u00a0\u00a0 \u76f8\u5f53\u4e8eapache\u91cc\u9762\u7684[L]\u6807\u8bb0\uff0c\u8868\u793arewrite\u3002 2.break\u672c\u6761\u89c4\u5219\u5339\u914d\u5b8c\u6210\u540e\uff0c\u7ec8\u6b62\u5339\u914d\uff0c\u4e0d\u518d\u5339\u914d\u540e\u9762\u7684\u89c4\u5219\u3002 3.redirect\u00a0\u00a0\u8fd4\u56de302\u4e34\u65f6\u91cd\u5b9a\u5411\uff0c\u6d4f\u89c8\u5668\u5730\u5740\u4f1a\u663e\u793a\u8df3\u8f6c\u540e\u7684URL\u5730\u5740\u3002 4.permanent\u00a0\u00a0\u8fd4\u56de301\u6c38\u4e45\u91cd\u5b9a\u5411\uff0c \u6d4f\u89c8\u5668\u5730\u5740\u4f1a\u663e\u793a\u8df3\u8f6c\u540e\u7684URL\u5730\u5740\u3002<br \/>\n\u4f7f\u7528last\u548cbreak\u5b9e\u73b0URI\u91cd\u5199\uff0c\u6d4f\u89c8\u5668\u5730\u5740\u680f\u4e0d\u53d8\u3002\u800c\u4e14\u4e24\u8005\u6709\u7ec6\u5fae\u5dee\u522b\uff0c\u4f7f\u7528alias\u6307\u4ee4\u5fc5\u987b\u7528last\u6807\u8bb0;\u4f7f\u7528proxy_pass\u6307\u4ee4\u65f6\uff0c\u9700\u8981\u4f7f\u7528break\u6807\u8bb0\u3002Last\u6807\u8bb0\u5728\u672c\u6761rewrite\u89c4\u5219\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f1a\u5bf9\u5176\u6240\u5728server{&#8230;&#8230;}\u6807\u7b7e\u91cd\u65b0\u53d1\u8d77\u8bf7\u6c42\uff0c\u800cbreak\u6807\u8bb0\u5219\u5728\u672c\u6761\u89c4\u5219\u5339\u914d\u5b8c\u6210\u540e\uff0c\u7ec8\u6b62\u5339\u914d\u3002<br \/>\n\u4e00\u822c\u5728\u8ddflocation\u4e2d(location \/{&#8230;})\u6216\u76f4\u63a5\u5728server\u6807\u7b7e\u4e2d\u7f16\u5199rewrite\u89c4\u5219\uff0c\u63a8\u8350\u4f7f\u7528last\u6807\u8bb0\uff1b\u5728\u975e\u6839location\u4e2d(location \/cms\/{&#8230;})\uff0c\u5219\u4f7f\u7528break\u3002<br \/>\n\u5982\u679cURI\u4e2d\u542b\u6709\u53c2\u6570(\/app\/test.php?id=5)\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u53c2\u6570\u4f1a\u88ab\u81ea\u52a8\u9644\u52a0\u5230\u66ff\u6362\u4e32\u4e0a\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u66ff\u6362\u4e32\u7684\u672b\u5c3e\u52a0\u4e0a?\u6807\u8bb0\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002 \u4f8b\u5982\uff1arewrite ^\/test(.*)$ http:\/\/www.ln.la\/home\u00a0\u00a0permanent; \u8bbf\u95eehttp:\/\/www.ln.la\/test?id=5 \u4f1a\u8df3\u8f6c\u5230 http:\/\/www.ln.la\/home?id=5<br \/>\n\u4f8b\u5982\uff1a\u5982\u679c\u6211\u4eec\u5c06\u7c7b\u4f3cURL \/photo\/123456 \u91cd\u5b9a\u5411\u5230 \/path\/to\/photo\/12\/1234\/123456.pngRewrite &#8220;\/photo\/([0-9]{2})([0-9]{2})([0-9]{2})&#8221; \/path\/to\/photo\/$1\/$1$2\/$1$2$3.png ;<br \/>\n\u6ce8\uff1a\u5982\u679c\u6b63\u5219\u8868\u8fbe\u5f0f\u91cc\u9762\u6709\u82b1\u62ec\u53f7&#8221;{&#8220;\u6216&#8221;}&#8221; \uff0c\u5e94\u8be5\u4f7f\u7528\u53cc\u5f15\u53f7\u6216\u5355\u5f15\u53f7\u3002<br \/>\n<b>5.Set\u6307\u4ee4<\/b> \u8bed\u6cd5\uff1aset variable value ; \u9ed8\u8ba4\u503c:none ; \u4f7f\u7528\u73af\u5883\uff1aserver,location,if; \u8be5\u6307\u4ee4\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff0c\u5e76\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u53d8\u91cf\u7684\u503c\u53ef\u4ee5\u4e3a\u6587\u672c\u3001\u53d8\u91cf\u4ee5\u53ca\u6587\u672c\u53d8\u91cf\u7684\u8054\u5408\u3002 \u793a\u4f8b\uff1aset $varname &#8220;hello world&#8221;;<br \/>\n<b>6.Uninitialized_variable_warn\u6307\u4ee4<\/b> \u8bed\u6cd5\uff1auninitialized_variable_warn on|off \u4f7f\u7528\u73af\u5883\uff1ahttp,server,location,if \u8be5\u6307\u4ee4\u7528\u4e8e\u5f00\u542f\u548c\u5173\u95ed\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8b66\u544a\u4fe1\u606f\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5f00\u542f\u3002<br \/>\n<b>7.Nginx Rewrite\u53ef\u4ee5\u7528\u5230\u7684\u5168\u5c40\u53d8\u91cf<\/b><\/p>\n<blockquote><p>$args ,$content_length ,$content_type ,$document_root ,$document_uri ,$host ,$http_user_agent ,$http_cookie ,$limit_rate ,$request_body_file ,$request_method ,$remote_addr ,$remote_port ,$remote_user ,$request_filename ,$request_uri ,$query_string ,$scheme ,$server_protocol ,$server_addr ,$server_name ,$server_port ,$uri<\/p><\/blockquote>\n<p><b><span style=\"text-decoration: underline;\">====(\u8be6\u60c5\u89c1\u9644\u5f55)====<\/span><\/b><br \/>\nNginx\u7684Rewrite\u89c4\u5219\u7f16\u5199\u5b9e\u4f8b <b>1.\u5f53\u8bbf\u95ee\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u4e0d\u5b58\u5728\u65f6\uff0c\u91cd\u5b9a\u5411\u5230\u67d0\u4e2aphp\u6587\u4ef6<\/b>if( !-e $request_filename ){rewrite ^\/(.*)$ index.php last;}<br \/>\n<b>2.\u76ee\u5f55\u5bf9\u6362 \/123456\/xxxx\u00a0\u00a0====&gt;\u00a0\u00a0 \/xxxx?id=123456<\/b>rewrite ^\/(d )\/(. )\/\u00a0\u00a0\/$2?id=$1 last;<br \/>\n<b>3.\u5982\u679c\u5ba2\u6237\u7aef\u4f7f\u7528\u7684\u662fIE\u6d4f\u89c8\u5668\uff0c\u5219\u91cd\u5b9a\u5411\u5230\/ie\u76ee\u5f55\u4e0b<\/b>if( $http_user_agent\u00a0\u00a0~ MSIE){rewrite ^(.*)$ \/ie\/$1 break;}<br \/>\n<b>4.\u7981\u6b62\u8bbf\u95ee\u591a\u4e2a\u76ee\u5f55<\/b> location ~ ^\/(cron|templates)\/{deny all;break;}<br \/>\n<b>5.\u7981\u6b62\u8bbf\u95ee\u4ee5\/data\u5f00\u5934\u7684\u6587\u4ef6<\/b>location ~ ^\/data{deny all;}<br \/>\n<b>6.\u7981\u6b62\u8bbf\u95ee\u4ee5.sh,.flv,.mp3\u4e3a\u6587\u4ef6\u540e\u7f00\u540d\u7684\u6587\u4ef6<\/b>location ~ .*.(sh|flv|mp3)${return 403;}<br \/>\n<b>7.\u8bbe\u7f6e\u67d0\u4e9b\u7c7b\u578b\u6587\u4ef6\u7684\u6d4f\u89c8\u5668\u7f13\u5b58\u65f6\u95f4<\/b>location ~ .*.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~ .*.(js|css)${expires 1h;} <b>8.\u7ed9favicon.ico\u548crobots.txt\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4;<\/b> \u8fd9\u91cc\u4e3afavicon.ico\u4e3a99\u5929,robots.txt\u4e3a7\u5929\u5e76\u4e0d\u8bb0\u5f55404\u9519\u8bef\u65e5\u5fd7<br \/>\nlocation ~(favicon.ico) {log_not_found off;expires 99d;break;}location ~(robots.txt) {log_not_found off;expires 7d;break;}<br \/>\n<b>9.\u8bbe\u5b9a\u67d0\u4e2a\u6587\u4ef6\u7684\u8fc7\u671f\u65f6\u95f4;\u8fd9\u91cc\u4e3a600\u79d2\uff0c\u5e76\u4e0d\u8bb0\u5f55\u8bbf\u95ee\u65e5\u5fd7<\/b><br \/>\nlocation ^~ \/html\/scripts\/loadhead_1.js {access_log\u00a0\u00a0 off;root \/opt\/lampp\/htdocs\/web;expires 600;break;}<br \/>\n<b>10.\u6587\u4ef6\u53cd\u76d7\u94fe\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/b><\/p>\n<p>\u8fd9\u91cc\u7684return 412 \u4e3a\u81ea\u5b9a\u4e49\u7684http\u72b6\u6001\u7801\uff0c\u9ed8\u8ba4\u4e3a403\uff0c\u65b9\u4fbf\u627e\u51fa\u6b63\u786e\u7684\u76d7\u94fe\u7684\u8bf7\u6c42 \u201crewrite ^\/ &lt;img alt=&#8221;&#8221; src=&#8221;http:\/\/img.tiyee.net\/leech.gif&#8221; border=&#8221;0&#8243; \/&gt;;\u201d\u663e\u793a\u4e00\u5f20\u9632\u76d7\u94fe\u56fe\u7247 \u201caccess_log off;\u201d\u4e0d\u8bb0\u5f55\u8bbf\u95ee\u65e5\u5fd7\uff0c\u51cf\u8f7b\u538b\u529b \u201cexpires 3d\u201d\u6240\u6709\u6587\u4ef63\u5929\u7684\u6d4f\u89c8\u5668\u7f13\u5b58<\/p>\n<p>location ~* ^. .(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;if ($invalid_referer) {rewrite ^\/ &lt;img alt=&#8221;&#8221; src=&#8221;http:\/\/img.tiyee.net\/leech.gif&#8221; border=&#8221;0&#8243; \/&gt;;return 412;break;}access_log off;root \/opt\/lampp\/htdocs\/web;expires 3d;break;}<\/p>\n<p>&nbsp;<\/p>\n<p><b>11.\u53ea\u5145\u8bb8\u56fa\u5b9aip\u8bbf\u95ee\u7f51\u7ad9\uff0c\u5e76\u52a0\u4e0a\u5bc6\u7801<\/b><br \/>\nroot\u00a0\u00a0\/opt\/htdocs\/www;allow\u00a0\u00a0 208.97.167.194;allow\u00a0\u00a0 222.33.1.2;allow\u00a0\u00a0 231.152.49.4;deny\u00a0\u00a0\u00a0\u00a0all;auth_basic \u201cC1G_ADMIN\u201d;auth_basic_user_file htpasswd;<br \/>\n<b>12\u5c06\u591a\u7ea7\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u8f6c\u6210\u4e00\u4e2a\u6587\u4ef6\uff0c\u589e\u5f3aseo\u6548\u679c<\/b> \/job-123-456-789.html \u6307\u5411\/job\/123\/456\/789.html<br \/>\nrewrite ^\/job-([0-9] )-([0-9] )-([0-9] ).html$ \/job\/$1\/$2\/jobshow_$3.html last;<br \/>\n<b>13.\u5c06\u6839\u76ee\u5f55\u4e0b\u67d0\u4e2a\u6587\u4ef6\u5939\u6307\u54112\u7ea7\u76ee\u5f55<\/b> \u5982\/<b>shanghai<\/b>job\/ \u6307\u5411 \/area\/<b>shanghai<\/b>\/ \u5982\u679c\u4f60\u5c06last\u6539\u6210permanent\uff0c\u90a3\u4e48\u6d4f\u89c8\u5668\u5730\u5740\u680f\u663e\u662f\/location\/shanghai\/<br \/>\nrewrite ^\/([0-9a-z] )job\/(.*)$ \/area\/$1\/$2 last;<br \/>\n\u4e0a\u9762\u4f8b\u5b50\u6709\u4e2a\u95ee\u9898\u662f\u8bbf\u95ee\/shanghai \u65f6\u5c06\u4e0d\u4f1a\u5339\u914d<br \/>\nrewrite ^\/([0-9a-z] )job$ \/area\/$1\/ last;rewrite ^\/([0-9a-z] )job\/(.*)$ \/area\/$1\/$2 last;<br \/>\n\u8fd9\u6837\/shanghai \u4e5f\u53ef\u4ee5\u8bbf\u95ee\u4e86\uff0c\u4f46\u9875\u9762\u4e2d\u7684\u76f8\u5bf9\u94fe\u63a5\u65e0\u6cd5\u4f7f\u7528\uff0c \u5982.\/list_1.html\u771f\u5b9e\u5730\u5740\u662f\/area\/shanghia\/list_1.html\u4f1a\u53d8\u6210\/list_1.html,\u5bfc\u81f3\u65e0\u6cd5\u8bbf\u95ee\u3002 \u90a3\u6211\u52a0\u4e0a\u81ea\u52a8\u8df3\u8f6c\u4e5f\u662f\u4e0d\u884c\u54af (-d $request_filename)\u5b83\u6709\u4e2a\u6761\u4ef6\u662f\u5fc5\u9700\u4e3a\u771f\u5b9e\u76ee\u5f55\uff0c\u800c\u6211\u7684rewrite\u4e0d\u662f\u7684\uff0c\u6240\u4ee5\u6ca1\u6709\u6548\u679c<br \/>\nif (-d $request_filename){rewrite ^\/(.*)([^\/])$ http:\/\/$host\/$1$2\/ permanent;}<br \/>\n\u77e5\u9053\u539f\u56e0\u540e\u5c31\u597d\u529e\u4e86\uff0c\u8ba9\u6211\u624b\u52a8\u8df3\u8f6c\u5427<br \/>\nrewrite ^\/([0-9a-z] )job$ \/$1job\/ permanent;rewrite ^\/([0-9a-z] )job\/(.*)$ \/area\/$1\/$2 last;<br \/>\n<b>14.\u6587\u4ef6\u548c\u76ee\u5f55\u4e0d\u5b58\u5728\u7684\u65f6\u5019\u91cd\u5b9a\u5411\uff1a<\/b><br \/>\nif (!-e $request_filename) {proxy_pass http:\/\/127.0.0.1;}<\/p>\n<p>Nginx\u548cApache\u7684Rewrite\u89c4\u5219\u5b9e\u4f8b\u5bf9\u6bd4 1.\u4e00\u822c\u7b80\u5355\u7684Nginx\u548cApache\u89c4\u5219\u7684\u533a\u522b\u4e0d\u5927\uff0c\u57fa\u672c\u80fd\u591f\u5b8c\u5168\u517c\u5bb9\uff0c\u4f8b\u5982\uff1a Apache: RewriteRule\u00a0\u00a0^\/abc\/$\u00a0\u00a0 \/web\/abc.php [L] Nginx:\u00a0\u00a0rewrite\u00a0\u00a0^\/abc\/$\u00a0\u00a0\/web\/abc.php last ; \u6211\u4eec\u53ef\u4ee5\u770b\u51fa\u6765\u53ea\u8981\u628aApache\u7684RewriteRule\u6539\u4e3aNginx\u7684rewrite\uff0cApache\u7684[L]\u6539\u4e3alast \u5373\u53ef\u3002 \u5982\u679c\u5c06Apache\u7684\u89c4\u5219\u6539\u4e3aNginx\u89c4\u5219\u540e\uff0c\u7528\u547d\u4ee4Nginx -t \u68c0\u67e5\u53d1\u73b0\u9519\u8bef\uff0c\u5219\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u7ed9\u6761\u4ef6\u52a0\u4e0a\u5f15\u53f7\uff0c\u4f8b\u5982\uff1a rewrite \u201c^\/([0-9]{5}).html$\u201d\u00a0\u00a0 \/x.php?id=$1 last;<br \/>\n2.Apache\u548cNginx\u7684Rewrite\u89c4\u5219\u5728URL\u8df3\u8f6c\u65f6\u6709\u7ec6\u5fae\u533a\u522b\uff1a Apache:\u00a0\u00a0RewriteRule ^\/html\/([a-zA-Z] )\/.*$\u00a0\u00a0\/$1\/\u00a0\u00a0[R=301,L] Nginx:\u00a0\u00a0 rewrite ^\/html\/([a-zA-Z] )\/.*$\u00a0\u00a0<span style=\"text-decoration: underline;\">http:\/\/$host\/$1\/<\/span> premanent ; \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728Nginx\u7684\u8df3\u8f6c\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u52a0\u4e0ahttp:\/\/$host\uff0c\u8fd9\u662f\u5728Nginx\u4e2d\u5f3a\u70c8\u8981\u6c42\u7684\u3002<br \/>\n3.\u4e0b\u9762\u662f\u4e00\u4e9bApache\u548cNginx\u89c4\u5219\u7684\u5bf9\u5e94\u5173\u7cfb a.Apache\u7684RewriteCond\u5bf9\u5e94Nginx\u7684if b.Apache\u7684RewriteRule\u5bf9\u5e94Nginx\u7684rewrite c.Apache\u7684[R]\u5bf9\u5e94Nginx\u7684redirect d.Apache\u7684[P]\u5bf9\u5e94Nginx\u7684last e.Apache\u7684[R,L]\u5bf9\u5e94Nginx\u7684redirect f.Apache\u7684[P,L]\u5bf9\u5e94Nginx\u7684last g.Apache\u7684[PT,L]\u5bf9\u5e94Nginx\u7684last<br \/>\n\u4f8b\u5982\uff1a\u5141\u8bb8\u6307\u5b9a\u7684\u57df\u540d\u8bbf\u95ee\u672c\u7ad9\uff0c\u5176\u4ed6\u7684\u57df\u540d\u4e00\u5f8b\u8f6c\u5411www.<span style=\"color: #444444;\">ln.la <\/span>Apache:RewriteCond %{HTTP_HOST} !^(.*?).aaa.com$ [NC]RewriteCond %{HTTP_HOST} !^localhost$RewriteCond %{HTTP_HOST} !^192.168.0.(.*?)$RewriteRule ^\/(.*)$ http:\/\/www.ln.la [R,L]<br \/>\nNginx:if( $host ~* ^(.*).aaa.com$ ){set $allowHost \u20181\u2019;}if( $host ~* ^localhost ){set $allowHost \u20181\u2019;}if( $host ~* ^192.168.1.(.*?)$ ){set $allowHost \u20181\u2019;}if( $allowHost !~ \u20181\u2019 ){rewrite ^\/(.*)$ http:\/\/www.ln.la redirect ;} <b>\u300a\u9644\u5f55\uff1anginx\u5168\u5c40\u53d8\u91cf\u300b<\/b> <b>\u7ecf\u5e38\u9700\u8981\u914d\u7f6eNginx \uff0c\u5176\u4e2d\u6709\u8bb8\u591a\u4ee5\u00a0\u00a0$ \u5f00\u5934\u7684\u53d8\u91cf\uff0c\u7ecf\u5e38\u9700\u8981\u67e5\u9605nginx \u6240\u652f\u6301\u7684\u53d8\u91cf\u3002Nginx\u652f\u6301\u7684http\u53d8\u91cf\u5b9e\u73b0\u5728 ngx_http_variables.c \u7684 ngx_http_core_variables\u5b58\u50a8\u5b9e\u73b0<\/b>ngx_http_core_variablesstatic ngx_http_variable_t\u00a0\u00a0ngx_http_core_variables[] = {{ ngx_string(&#8220;http_host&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.host), 0, 0 },{ ngx_string(&#8220;http_user_agent&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },{ ngx_string(&#8220;http_referer&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },#if (NGX_HTTP_GZIP){ ngx_string(&#8220;http_via&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.via), 0, 0 },#endif#if (NGX_HTTP_PROXY || NGX_HTTP_REALIP){ ngx_string(&#8220;http_x_forwarded_for&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },#endif{ ngx_string(&#8220;http_cookie&#8221;), NULL, ngx_http_variable_headers,offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },{ ngx_string(&#8220;content_length&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },{ ngx_string(&#8220;content_type&#8221;), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },{ ngx_string(&#8220;host&#8221;), NULL, ngx_http_variable_host, 0, 0, 0 },{ ngx_string(&#8220;binary_remote_addr&#8221;), NULL,ngx_http_variable_binary_remote_addr, 0, 0, 0 },{ ngx_string(&#8220;remote_addr&#8221;), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },{ ngx_string(&#8220;remote_port&#8221;), NULL, ngx_http_variable_remote_port, 0, 0, 0 },{ ngx_string(&#8220;server_addr&#8221;), NULL, ngx_http_variable_server_addr, 0, 0, 0 },{ ngx_string(&#8220;server_port&#8221;), NULL, ngx_http_variable_server_port, 0, 0, 0 },{ ngx_string(&#8220;server_protocol&#8221;), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, http_protocol), 0, 0 },{ ngx_string(&#8220;scheme&#8221;), NULL, ngx_http_variable_scheme, 0, 0, 0 },{ ngx_string(&#8220;request_uri&#8221;), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },{ ngx_string(&#8220;uri&#8221;), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, uri),NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;document_uri&#8221;), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, uri),NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;request&#8221;), NULL, ngx_http_variable_request_line, 0, 0, 0 },{ ngx_string(&#8220;document_root&#8221;), NULL,ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;realpath_root&#8221;), NULL,ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;query_string&#8221;), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, args),NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;args&#8221;),ngx_http_variable_request_set,ngx_http_variable_request,offsetof(ngx_http_request_t, args),NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;is_args&#8221;), NULL, ngx_http_variable_is_args,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;request_filename&#8221;), NULL,ngx_http_variable_request_filename, 0,NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;server_name&#8221;), NULL, ngx_http_variable_server_name, 0, 0, 0 },{ ngx_string(&#8220;request_method&#8221;), NULL,ngx_http_variable_request_method, 0,NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;remote_user&#8221;), NULL, ngx_http_variable_remote_user, 0, 0, 0 },{ ngx_string(&#8220;body_bytes_sent&#8221;), NULL, ngx_http_variable_body_bytes_sent,0, 0, 0 },{ ngx_string(&#8220;request_completion&#8221;), NULL,ngx_http_variable_request_completion,0, 0, 0 },{ ngx_string(&#8220;request_body&#8221;), NULL,ngx_http_variable_request_body,0, 0, 0 },{ ngx_string(&#8220;request_body_file&#8221;), NULL,ngx_http_variable_request_body_file,0, 0, 0 },{ ngx_string(&#8220;sent_http_content_type&#8221;), NULL,ngx_http_variable_sent_content_type, 0, 0, 0 },{ ngx_string(&#8220;sent_http_content_length&#8221;), NULL,ngx_http_variable_sent_content_length, 0, 0, 0 },{ ngx_string(&#8220;sent_http_location&#8221;), NULL,ngx_http_variable_sent_location, 0, 0, 0 },{ ngx_string(&#8220;sent_http_last_modified&#8221;), NULL,ngx_http_variable_sent_last_modified, 0, 0, 0 },{ ngx_string(&#8220;sent_http_connection&#8221;), NULL,ngx_http_variable_sent_connection, 0, 0, 0 },{ ngx_string(&#8220;sent_http_keep_alive&#8221;), NULL,ngx_http_variable_sent_keep_alive, 0, 0, 0 },{ ngx_string(&#8220;sent_http_transfer_encoding&#8221;), NULL,ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },{ ngx_string(&#8220;sent_http_cache_control&#8221;), NULL, ngx_http_variable_headers,offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },{ ngx_string(&#8220;limit_rate&#8221;), ngx_http_variable_request_set_size,ngx_http_variable_request_get_size,offsetof(ngx_http_request_t, limit_rate),NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string(&#8220;nginx_version&#8221;), NULL, ngx_http_variable_nginx_version,0, 0, 0 },{ ngx_string(&#8220;hostname&#8221;), NULL, ngx_http_variable_hostname,0, 0, 0 },{ ngx_string(&#8220;pid&#8221;), NULL, ngx_http_variable_pid,0, 0, 0 },{ ngx_null_string, NULL, NULL, 0, 0, 0 }}; <b>\u628a\u8fd9\u4e9b\u53d8\u91cf\u63d0\u53d6\u4e0b\uff0c\u603b\u7ed3\u5982\u4e0b\uff1a<\/b><\/p>\n<ul>\n<li>arg_PARAMETER\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u8fd9\u4e2a\u53d8\u91cf\u5305\u542bGET\u8bf7\u6c42\u4e2d\uff0c\u5982\u679c\u6709\u53d8\u91cfPARAMETER\u65f6\u7684\u503c\u3002<\/li>\n<li>args\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u8fd9\u4e2a\u53d8\u91cf\u7b49\u4e8e\u8bf7\u6c42\u884c\u4e2d(GET\u8bf7\u6c42)\u7684\u53c2\u6570\uff0c\u4f8b\u5982foo=123&amp;bar=blahblah;<\/li>\n<li>binary_remote_addr #\u4e8c\u8fdb\u5236\u7684\u5ba2\u6237\u5730\u5740\u3002<\/li>\n<li>body_bytes_sent\u00a0\u00a0\u00a0\u00a0#\u54cd\u5e94\u65f6\u9001\u51fa\u7684body\u5b57\u8282\u6570\u6570\u91cf\u3002\u5373\u4f7f\u8fde\u63a5\u4e2d\u65ad\uff0c\u8fd9\u4e2a\u6570\u636e\u4e5f\u662f\u7cbe\u786e\u7684\u3002<\/li>\n<li>content_length\u00a0\u00a0\u00a0\u00a0 #\u8bf7\u6c42\u5934\u4e2d\u7684Content-length\u5b57\u6bb5\u3002<\/li>\n<li>content_type\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u8bf7\u6c42\u5934\u4e2d\u7684Content-Type\u5b57\u6bb5\u3002<\/li>\n<li>cookie_COOKIE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#cookie COOKIE\u53d8\u91cf\u7684\u503c<\/li>\n<li>document_root\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u5f53\u524d\u8bf7\u6c42\u5728root\u6307\u4ee4\u4e2d\u6307\u5b9a\u7684\u503c\u3002<\/li>\n<li>document_uri\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u4e0e<\/li>\n<li>uri\u76f8\u540c\u3002<\/li>\n<li>host\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u8bf7\u6c42\u4e3b\u673a\u5934\u5b57\u6bb5\uff0c\u5426\u5219\u4e3a\u670d\u52a1\u5668\u540d\u79f0\u3002<\/li>\n<li>hostname\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #Set to the machine\u2019s hostname as returned by gethostname<\/li>\n<li>http_HEADER<\/li>\n<li>is_args\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u5982\u679c\u6709<\/li>\n<li>args\u53c2\u6570\uff0c\u8fd9\u4e2a\u53d8\u91cf\u7b49\u4e8e\u201d?\u201d\uff0c\u5426\u5219\u7b49\u4e8e\u201d&#8221;\uff0c\u7a7a\u503c\u3002<\/li>\n<li>http_user_agent\u00a0\u00a0\u00a0\u00a0#\u5ba2\u6237\u7aefagent\u4fe1\u606f<\/li>\n<li>http_cookie\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u5ba2\u6237\u7aefcookie\u4fe1\u606f<\/li>\n<li>limit_rate\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u8fd9\u4e2a\u53d8\u91cf\u53ef\u4ee5\u9650\u5236\u8fde\u63a5\u901f\u7387\u3002<\/li>\n<li>query_string\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u4e0e<\/li>\n<li>args\u76f8\u540c\u3002<\/li>\n<li>request_body_file\u00a0\u00a0#\u5ba2\u6237\u7aef\u8bf7\u6c42\u4e3b\u4f53\u4fe1\u606f\u7684\u4e34\u65f6\u6587\u4ef6\u540d\u3002<\/li>\n<li>request_method\u00a0\u00a0\u00a0\u00a0 #\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u52a8\u4f5c\uff0c\u901a\u5e38\u4e3aGET\u6216POST\u3002<\/li>\n<li>remote_addr\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u5ba2\u6237\u7aef\u7684IP\u5730\u5740\u3002<\/li>\n<li>remote_port\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u5ba2\u6237\u7aef\u7684\u7aef\u53e3\u3002<\/li>\n<li>remote_user\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u5df2\u7ecf\u7ecf\u8fc7Auth Basic Module\u9a8c\u8bc1\u7684\u7528\u6237\u540d\u3002<\/li>\n<li>request_completion #\u5982\u679c\u8bf7\u6c42\u7ed3\u675f\uff0c\u8bbe\u7f6e\u4e3aOK. \u5f53\u8bf7\u6c42\u672a\u7ed3\u675f\u6216\u5982\u679c\u8be5\u8bf7\u6c42\u4e0d\u662f\u8bf7\u6c42\u94fe\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u65f6\uff0c\u4e3a\u7a7a(Empty)\u3002<\/li>\n<li>request_method\u00a0\u00a0\u00a0\u00a0 #GET\u6216POST<\/li>\n<li>request_filename\u00a0\u00a0 #\u5f53\u524d\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u7531root\u6216alias\u6307\u4ee4\u4e0eURI\u8bf7\u6c42\u751f\u6210\u3002<\/li>\n<li>request_uri\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\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\u4e0d\u80fd\u4fee\u6539\u3002<\/li>\n<li>scheme\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #HTTP\u65b9\u6cd5\uff08\u5982http\uff0chttps\uff09\u3002<\/li>\n<li>server_protocol\u00a0\u00a0\u00a0\u00a0#\u8bf7\u6c42\u4f7f\u7528\u7684\u534f\u8bae\uff0c\u901a\u5e38\u662fHTTP\/1.0\u6216HTTP\/1.1\u3002<\/li>\n<li>server_addr\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u670d\u52a1\u5668\u540d\u79f0\u3002<\/li>\n<li>server_port\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u8bf7\u6c42\u5230\u8fbe\u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Nginx Rewrite \u89c4\u5219\u76f8\u5173\u6307\u4ee4 \u76f8\u5173\u6307\u4ee4\u6709if,rewrite,set,return,break\u7b49\uff0c &hellip; <a href=\"https:\/\/www.lnlog.cn\/index.php\/2013\/06\/20\/configure-nginx-rewrite-rules\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Nginx\u914d\u7f6e\u53caRewrite\u89c4\u5219<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[60,73,301],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-tech","tag-nginx","tag-rewrite","tag-301"],"_links":{"self":[{"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/posts\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=328"}],"version-history":[{"count":0,"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/posts\/328\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lnlog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}