<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[刘新修]]></title> 
<link>http://liuxinxiu.com:80/index.php</link> 
<description><![CDATA[刘新修的个人博客 (Liuxinxiu'S Blog)]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[刘新修]]></copyright>
<item>
<link>http://liuxinxiu.com:80/nginx_rewrite/</link>
<title><![CDATA[NGINX 重写规则]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[web服务器]]></category>
<pubDate>Fri, 11 Nov 2016 04:06:04 +0000</pubDate> 
<guid>http://liuxinxiu.com:80/nginx_rewrite/</guid> 
<description>
<![CDATA[ 
	<p>nginx rewrite 实现二级域名跳转</p><div>当访问http://abc.test.com跳转到http://www.test.com/test/abc/</div><div>方法一:</div><div>这种方法浏览器地址会变www.test.com/test/abc</div><div>实现访问如下:</div><div>server &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; listen 80;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; server_name www.test.com;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; location / &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root /data/test;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index index.html;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#125;&nbsp;</div><div>&#125;&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>server &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; listen 80;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; server_name *.test.com;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if ( $http_host ~* &quot;^(.*)&#92;.test&#92;.com$&quot;) &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set $domain $1;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rewrite ^(.*) http://www.test.com/test/$domain/ break;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#125;&nbsp;</div><div>&#125;</div><div>方法二、</div><div>&nbsp;</div><div>当访问http://abc.test.com跳转到http://www.test.com/test/abc/</div><div>server &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; listen 80;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; server_name *.test.com;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; root /usr/local/www;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; #这是里可以加多个目录,如果不加目录，会无法访问到abc.test.com/目录下的文件,如图片目录/images</div><div>&nbsp; &nbsp; &nbsp; &nbsp; location ~ ^/(test&#124;images&#124;styles)/&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proxy_redirect &nbsp; &nbsp; &nbsp; &nbsp;off;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header &nbsp; &nbsp;Host &nbsp; www.test.com;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proxy_pass &nbsp; &nbsp; &nbsp;http://192.168.1.2:8080;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#125;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; location / &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set $domain default;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $http_host ~* &quot;^(.*)&#92;.test&#92;.com$&quot;) &#123;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set $domain $1;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#125;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rewrite ^/(.*) &nbsp; &nbsp;/test/$domain/$1 last;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#125;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; access_log off;</div><div>&#125;</div><div>&nbsp;</div><div>rewrite命令</div><div>nginx的rewrite相当于apache的rewriterule(大多数情况下可以把原有apache的rewrite规则加上引号就可以直接使用)，它可以用在server,location 和IF条件判断块中,命</div><div>&nbsp;</div><div>令格式如下：</div><div>rewrite 正则表达式 替换目标 flag标记</div><div>flag标记可以用以下几种格式：</div><div>last - 基本上都用这个Flag。</div><div>break - 中止Rewirte，不在继续匹配</div><div>redirect - 返回临时重定向的HTTP状态302</div><div>permanent - 返回永久重定向的HTTP状态301</div><div>例如下面这段设定nginx将某个目录下面的文件重定向到另一个目录,$2对应第二个括号(.*)中对应的字符串：</div><div>&nbsp;</div><div>location /download/ &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; rewrite ^(/download/.*)/m/(.*)&#92;..*$ $1/nginx-rewrite/$2.gz break;</div><div>&#125;</div><div>&nbsp;</div><div>nginx重定向的IF条件判断</div><div>在server和location两种情况下可以使用nginx的IF条件判断，条件可以为以下几种：</div><div>正则表达式</div><div>&nbsp;</div><div>如：</div><div>匹配判断</div><div>~ 为区分大小写匹配; !~为区分大小写不匹配</div><div>~* 为不区分大小写匹配；!~为不区分大小写不匹配</div><div>&nbsp;</div><div>就是当用户输入 www.a.com.cn 自动跳转到www.a.com 这个域名：</div><div>&nbsp;</div><div>rewrite ^/(.*)$ http://www.a.com/$1 permanent; 或者cname</div><div>例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下：</div><div>if ($http_user_agent ~ MSIE) &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; rewrite ^(.*)$ /nginx-ie/$1 break;</div><div>&#125;</div><div>文件和目录判断</div><div>-f和!-f判断是否存在文件</div><div>-d和!-d判断是否存在目录</div><div>-e和!-e判断是否存在文件或目录</div><div>-x和!-x判断文件是否可执行</div><div>&nbsp;</div><div>例如下面设定nginx在文件和目录不存在的时候重定向：</div><div>if (!-e $request_filename) &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; proxy_pass http://127.0.0.1;</div><div>&#125;</div><div>&nbsp;</div><div>return</div><div>返回http代码，例如设置nginx防盗链：</div><div>location ~* &#92;.(gif&#124;jpg&#124;png&#124;swf&#124;flv)$ &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; valid_referers none blocked www.jefflei.comwww.leizhenfang.com;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if ($invalid_referer) &#123;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 404;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#125;</div><div>&#125;</div><div>&nbsp;</div><div>记一正则，匹配非某单词</div><div>由于要rewrite一个地址从</div><div>/mag/xx/xxx/ -&gt; /m/xxx</div><div>但原先 /mag/xx/more/ 要保留</div><div>这就得写一个比较奇特的正则了，尝试了比较多的写法也没成功</div><div>&nbsp;</div><div>最先想的是：</div><div>&nbsp;</div><div>location ~* ^/mag/[^/]+/[^(more)]+/ &#123;</div><div>&nbsp; rewrite ^/mag/[^/]+/(.*) /m/$1 permanent;</div><div>&#125;</div><div>&nbsp;</div><div>&nbsp;</div><div>[]的写法并不凑效，里面是匹配单个字符的，这样无效，匹配不了</div><div>&nbsp;</div><div>还是小范同学不错，研究的深入，提供了非某单词的写法 (?!more)</div><div>&nbsp;</div><div>location ~* ^/mag/[^/]+/(?!more)([^/]+)/ &#123;</div><div>&nbsp; rewrite ^/mag/[^/]+/(.*) /m/$1 permanent;</div><div>&#125;</div><div>&nbsp;</div><div>&nbsp;</div><div>这个写法勉强可以应付了，后面的匹配单元虽说还不完美，但也能够对付我的所有需求内容了。</div><div>有需要的可以参考此写法。</div><div>&nbsp;</div><div>引用</div><div>&nbsp;</div><div>常用分组语法</div><div>&nbsp;</div><div>捕获</div><div>(exp) 匹配exp,并捕获文本到自动命名的组里&nbsp;</div><div>(?exp) 匹配exp,并捕获文本到名称为name的组里，也可以写成(?'name'exp)&nbsp;</div><div>(?:exp) 匹配exp,不捕获匹配的文本，也不给此分组分配组号&nbsp;</div><div>&nbsp;</div><div>零宽断言&nbsp;</div><div>(?=exp) 匹配exp前面的位置&nbsp;</div><div>(?&lt;=exp) 匹配exp后面的位置&nbsp;</div><div>(?!exp) 匹配后面跟的不是exp的位置&nbsp;</div><div>(?</div><div>为何要使用301重定向</div><div>在网站建设中需要网页重定向的情况很多：如网页目录结构变动，网页重命名、网页的扩展名改变、网站域名改变等。如果不做重 定向，用户的收藏和搜索引擎数据库中的旧地址只能让访客得到一个404错误信息页面，访问流量白白丧失。不仅如此，之前该页面的一切积累（比如PR值）就 都白费了。</div><div>301重定向不仅能使页面实现自动跳转，对于搜索引擎来说，也可能可以传递PR值。</div><div>nginx重定向规则详细介绍</div><div>http://www.jefflei.com/post/1015.html</div><div>rewrite命令</div><div>nginx的rewrite相当于apache的rewriterule(大多数情况下可以把原有apache的rewrite规则加上引号就可以直接使用)，它可以用在server,location 和IF条件判断块中,命令格式如下：</div><div>rewrite 正则表达式 替换目标 flag标记</div><div>flag标记可以用以下几种格式：</div><div>last &ndash; 基本上都用这个Flag。</div><div>break &ndash; 中止Rewirte，不在继续匹配</div><div>redirect &ndash; 返回临时重定向的HTTP状态302</div><div>permanent &ndash; 返回永久重定向的HTTP状态301</div><div>例如下面这段设定nginx将某个目录下面的文件重定向到另一个目录,$2对应第二个括号(.*)中对应的字符串：</div><div>location /download/ &#123;</div><div>rewrite ^(/download/.*)/m/(.*)&#92;..*$ $1/nginx-rewrite/$2.gz break;</div><div>&#125;</div><div>nginx重定向的IF条件判断</div><div>在server和location两种情况下可以使用nginx的IF条件判断，条件可以为以下几种：</div><div>正则表达式</div><div>如：</div><div>匹配判断</div><div>~ &nbsp;为区分大小写匹配; !~为区分大小写不匹配</div><div>&nbsp;~* 为不区分大小写匹配；!~为不区分大小写不匹配</div><div>例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下：</div><div>if ($http_user_agent ~ MSIE) &#123;</div><div>rewrite ^(.*)$ /nginx-ie/$1 break;</div><div>&#125;</div><div>文件和目录判断</div><div>&nbsp; -f和!-f判断是否存在文件</div><div>&nbsp;-d和!-d判断是否存在目录</div><div>&nbsp;-e和!-e判断是否存在文件或目录</div><div>&nbsp;-x和!-x判断文件是否可执行</div><div>例如下面设定nginx在文件和目录不存在的时候重定向：</div><div>if (!-e $request_filename) &#123;</div><div>proxy_pass http://127.0.0.1/;</div><div>&#125;</div><div>return</div><div>返回http代码，例如设置nginx防盗链：</div><div>location ~* &#92;.(gif&#124;jpg&#124;png&#124;swf&#124;flv)$ &#123;</div><div>valid_referers none blocked http://www.jefflei.com/ http://www.leizhenfang.com/;</div><div>if ($invalid_referer) &#123;</div><div>return 404;</div><div>&#125;</div><div>&#125;</div><div>set</div><div>设置nginx变量</div><div>&nbsp;</div><div>301重定向方法</div><div>进行了301重定向，把www .jefflei.com和jefflei.com合并，并把之前的域名也一并合并. 有两种实现方法,第一种方法是判断nginx核心变量host(老版本是http_host)：</div><div>server &#123;</div><div>server_name www.jefflei.com jefflei.com ;</div><div>if ($host != 'www.jefflei.com' ) &#123;</div><div>rewrite ^/(.*)$ http://www.jefflei.com/$1 permanent;</div><div>&#125;</div><div>...</div><div>&#125;</div><div>第二种方法：</div><div>server &#123;</div><div>server_name jefflei.com;</div><div>rewrite ^/(.*) http://www.jefflei.com/$1 permanent;</div><div>&#125;</div><div>测试了第一种方法ok，这两种方法中， permanent是关键，详细说明见nginx重定向规则说明。</div><div>last &ndash; 基本上都用这个Flag。</div><div>break &ndash; 中止Rewirte，不在继续匹配</div><div>redirect &ndash; 返回临时重定向的HTTP状态302</div><div>permanent &ndash; 返回永久重定向的HTTP状态301</div><div>好了,现在可以检查结果，这里可以看返回的HTTP头信息：</div><div>http://www.seoconsultants.com/tools/headers.asp</div><div>第二种方法没有测试成功...</div><div>&nbsp;</div><div>测试是否定向成功</div><div>http://qinfy.net/301-redirect-for-nginx/</div><div>输入指令~</div><div>/usr/local/nginx/sbin/nginx -t</div><div>提示：</div><div>the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok</div><div>configuration file /usr/local/nginx/conf/nginx.conf test is successful</div><div>测试成功~ 重启nginx~ 输入指令~</div><div>/usr/local/nginx/sbin/nginx -s reload</div><div>重启之后测试一下~是否成功设定完成！ 输入指令~</div><div>curl -I imcat.tk</div><div>&nbsp;</div><div>会输出：</div><div>HTTP/1.1 301 Moved Permanently</div><div>Server: nginx/0.7.65</div><div>Date: Tue, 03 Aug 2010 01:12:37 GMT</div><div>Content-Type: text/html</div><div>Content-Length: 185</div><div>Connection: keep-alive</div><div>Location: http://qinfy.net/</div><div>nginx rewrite 伪静态配置参数详细说明（转）</div><div>http://hi.baidu.com/hx10/blog/item/942a0ad784f3ffd0a144df94.html</div><div>nginx rewrite 伪静态配置参数和使用例子 附正则使用说明</div><div>正则表达式匹配，其中：</div><div>* ~ 为区分大小写匹配&nbsp;</div><div>* ~* 为不区分大小写匹配&nbsp;</div><div>* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配</div><div>&nbsp;</div><div>文件及目录匹配，其中：</div><div>* -f和!-f用来判断是否存在文件&nbsp;</div><div>* -d和!-d用来判断是否存在目录&nbsp;</div><div>* -e和!-e用来判断是否存在文件或目录&nbsp;</div><div>* -x和!-x用来判断文件是否可执行</div><div>flag标记有：</div><div>* last 相当于Apache里的[L]标记，表示完成rewrite&nbsp;</div><div>* break 终止匹配, 不再匹配后面的规则&nbsp;</div><div>* redirect 返回302临时重定向 地址栏会显示跳转后的地址&nbsp;</div><div>* permanent 返回301永久重定向 地址栏会显示跳转后的地址</div><div>一些可用的全局变量有，可以用做条件判断(待补全)</div><div>$args&nbsp;</div><div>$content_length&nbsp;</div><div>$content_type&nbsp;</div><div>$document_root&nbsp;</div><div>$document_uri&nbsp;</div><div>$host&nbsp;</div><div>$http_user_agent&nbsp;</div><div>$http_cookie&nbsp;</div><div>$limit_rate&nbsp;</div><div>$request_body_file&nbsp;</div><div>$request_method&nbsp;</div><div>$remote_addr&nbsp;</div><div>$remote_port&nbsp;</div><div>$remote_user&nbsp;</div><div>$request_filename&nbsp;</div><div>$request_uri&nbsp;</div><div>$query_string&nbsp;</div><div>$scheme&nbsp;</div><div>$server_protocol&nbsp;</div><div>$server_addr&nbsp;</div><div>$server_name&nbsp;</div><div>$server_port&nbsp;</div><div>$uri</div><div>结合QeePHP的例子</div><div>if (!-d $request_filename) &#123;&nbsp;</div><div>rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&amp;controller=$1&amp;action=$2&amp;$3 last;&nbsp;</div><div>rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&amp;controller=$1 last;&nbsp;</div><div>break;</div><div>多目录转成参数</div><div>abc.domian.com/sort/2 =&gt; abc.domian.com/index.php?act=sort&amp;name=abc&amp;id=2</div><div>if ($host ~* (.*)&#92;.domain&#92;.com) &#123;&nbsp;</div><div>set $sub_name $1;&nbsp;</div><div>rewrite ^/sort&#92;/(&#92;d+)&#92;/?$ /index.php?act=sort&amp;cid=$sub_name&amp;id=$1 last;&nbsp;</div><div>&#125;</div><div>目录对换</div><div>/123456/xxxx -&gt; /xxxx?id=123456</div><div>rewrite ^/(&#92;d+)/(.+)/ /$2?id=$1 last;</div><div>例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下：</div><div>if ($http_user_agent ~ MSIE) &#123;&nbsp;</div><div>rewrite ^(.*)$ /nginx-ie/$1 break;&nbsp;</div><div>&#125;</div><div>目录自动加&ldquo;/&rdquo;</div><div>if (-d $request_filename)&#123;&nbsp;</div><div>rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;&nbsp;</div><div>&#125;</div><div>禁止htaccess</div><div>location ~/&#92;.ht &#123;&nbsp;</div><div>deny all;&nbsp;</div><div>&#125;</div><div>禁止多个目录</div><div>location ~ ^/(cron&#124;templates)/ &#123;&nbsp;</div><div>deny all;&nbsp;</div><div>break;&nbsp;</div><div>&#125;</div><div>禁止以/data开头的文件</div><div>可以禁止/data/下多级目录下.log.txt等请求;</div><div>location ~ ^/data &#123;&nbsp;</div><div>deny all;&nbsp;</div><div>&#125;</div><div>禁止单个目录</div><div>不能禁止.log.txt能请求</div><div>location /searchword/cron/ &#123;&nbsp;</div><div>deny all;&nbsp;</div><div>&#125;</div><div>禁止单个文件</div><div>location ~ /data/sql/data.sql &#123;&nbsp;</div><div>deny all;&nbsp;</div><div>&#125;</div><div>给favicon.ico和robots.txt设置过期时间;</div><div>这里为favicon.ico为99 天,robots.txt为7天并不记录404错误日志</div><div>location ~(favicon.ico) &#123;&nbsp;</div><div>log_not_found off;&nbsp;</div><div>expires 99d;&nbsp;</div><div>break;&nbsp;</div><div>&#125;&nbsp;</div><div>&nbsp;</div><div>location ~(robots.txt) &#123;&nbsp;</div><div>log_not_found off;&nbsp;</div><div>expires 7d;&nbsp;</div><div>break;&nbsp;</div><div>&#125;</div><div>设定某个文件的过期时间;这里为600秒，并不记录访问日志</div><div>location ^~ /html/scripts/loadhead_1.js &#123;&nbsp;</div><div>access_log off;&nbsp;</div><div>root /opt/lampp/htdocs/web;&nbsp;</div><div>expires 600;&nbsp;</div><div>break;&nbsp;</div><div>&#125;</div><div>文件反盗链并设置过期时间</div><div>这里的return 412 为自定义的http状态码，默认为403，方便找出正确的盗链的请求</div><div>&ldquo;rewrite ^/ http://leech.c1gstudio.com/leech.gif;&rdquo;显示一张防盗链图片</div><div>&ldquo;access_log off;&rdquo;不记录访问日志，减轻压力</div><div>&ldquo;expires 3d&rdquo;所有文件3天的浏览器缓存</div><div>location ~* ^.+&#92;.(jpg&#124;jpeg&#124;gif&#124;png&#124;swf&#124;rar&#124;zip&#124;css&#124;js)$ &#123;&nbsp;</div><div>valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;&nbsp;</div><div>if ($invalid_referer) &#123;&nbsp;</div><div>rewrite ^/ http://leech.c1gstudio.com/leech.gif;&nbsp;</div><div>return 412;&nbsp;</div><div>break;&nbsp;</div><div>&#125;&nbsp;</div><div>access_log off;&nbsp;</div><div>root /opt/lampp/htdocs/web;&nbsp;</div><div>expires 3d;&nbsp;</div><div>break;&nbsp;</div><div>&#125;</div><div>只充许固定ip访问网站，并加上密码</div><div>root /opt/htdocs/www;&nbsp;</div><div>allow 208.97.167.194;&nbsp;</div><div>allow 222.33.1.2;&nbsp;</div><div>allow 231.152.49.4;&nbsp;</div><div>deny all;&nbsp;</div><div>auth_basic &quot;C1G_ADMIN&quot;;&nbsp;</div><div>auth_basic_user_file htpasswd;</div><div>将多级目录下的文件转成一个文件，增强seo效果</div><div>/job-123-456-789.html 指向/job/123/456/789.html</div><div>rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)&#92;.html$ /job/$1/$2/jobshow_$3.html last;</div><div>将根目录下某个文件夹指向2级目录</div><div>如/shanghaijob/ 指向 /area/shanghai/</div><div>如果你将last改成permanent，那么浏览器地址栏显是 /location/shanghai/</div><div>rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;</div><div>上面例子有个问题是访问/shanghai 时将不会匹配</div><div>rewrite ^/([0-9a-z]+)job$ /area/$1/ last;&nbsp;</div><div>rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;</div><div>这样/shanghai 也可以访问了，但页面中的相对链接无法使用，</div><div>如./list_1.html真实地址是/area /shanghia/list_1.html会变成/list_1.html,导至无法访问。</div><div>那我加上自动跳转也是不行咯</div><div>(-d $request_filename)它有个条件是必需为真实目录，而我的rewrite不是的，所以没有效果</div><div>if (-d $request_filename)&#123;&nbsp;</div><div>rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;&nbsp;</div><div>&#125;</div><div>知道原因后就好办了，让我手动跳转吧</div><div>rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;&nbsp;</div><div>rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;</div><div>文件和目录不存在的时候重定向：</div><div>if (!-e $request_filename) &#123;&nbsp;</div><div>proxy_pass http://127.0.0.1/;&nbsp;</div><div>&#125;</div><div>域名跳转</div><div>server&nbsp;</div><div>&#123;&nbsp;</div><div>listen 80;&nbsp;</div><div>server_name jump.c1gstudio.com;&nbsp;</div><div>index index.html index.htm index.php;&nbsp;</div><div>root /opt/lampp/htdocs/www;&nbsp;</div><div>rewrite ^/ http://www.c1gstudio.com/;&nbsp;</div><div>access_log off;&nbsp;</div><div>&#125;</div><div>多域名转向</div><div>server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/;&nbsp;</div><div>index index.html index.htm index.php;&nbsp;</div><div>root /opt/lampp/htdocs;&nbsp;</div><div>if ($host ~ &quot;c1gstudio&#92;.net&quot;) &#123;&nbsp;</div><div>rewrite ^(.*) http://www.c1gstudio.com$1/ permanent;&nbsp;</div><div>&#125;</div><div>三级域名跳转</div><div>if ($http_host ~* &quot;^(.*)&#92;.i&#92;.c1gstudio&#92;.com$&quot;) &#123;&nbsp;</div><div>rewrite ^(.*) http://top.yingjiesheng.com$1/;&nbsp;</div><div>break;&nbsp;</div><div>&#125;</div><div>域名镜向</div><div>server&nbsp;</div><div>&#123;&nbsp;</div><div>listen 80;&nbsp;</div><div>server_name mirror.c1gstudio.com;&nbsp;</div><div>index index.html index.htm index.php;&nbsp;</div><div>root /opt/lampp/htdocs/www;&nbsp;</div><div>rewrite ^/(.*) http://www.c1gstudio.com/$1 last;&nbsp;</div><div>access_log off;&nbsp;</div><div>&#125;</div><div>某个子目录作镜向</div><div>location ^~ /zhaopinhui &#123;&nbsp;</div><div>rewrite ^.+ http://zph.c1gstudio.com/ last;&nbsp;</div><div>break;&nbsp;</div><div>&#125;</div><div>discuz ucenter home (uchome) rewrite</div><div>rewrite ^/(space&#124;network)-(.+)&#92;.html$ /$1.php?rewrite=$2 last;&nbsp;</div><div>rewrite ^/(space&#124;network)&#92;.html$ /$1.php last;&nbsp;</div><div>rewrite ^/([0-9]+)$ /space.php?uid=$1 last;</div><div>discuz 7 rewrite</div><div>rewrite ^(.*)/archiver/((fid&#124;tid)-[&#92;w&#92;-]+&#92;.html)$ $1/archiver/index.php?$2 last;&nbsp;</div><div>rewrite ^(.*)/forum-([0-9]+)-([0-9]+)&#92;.html$ $1/forumdisplay.php?fid=$2&amp;page=$3 last;&nbsp;</div><div>rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)&#92;.html$ $1/viewthread.php?tid=$2&amp;extra=page&#92;%3D$4&amp;page=$3 last;&nbsp;</div><div>rewrite ^(.*)/profile-(username&#124;uid)-(.+)&#92;.html$ $1/viewpro.php?$2=$3 last;&nbsp;</div><div>rewrite ^(.*)/space-(username&#124;uid)-(.+)&#92;.html$ $1/space.php?$2=$3 last;&nbsp;</div><div>rewrite ^(.*)/tag-(.+)&#92;.html$ $1/tag.php?name=$2 last;</div><div>给discuz某版块单独配置域名</div><div>server_name bbs.c1gstudio.com news.c1gstudio.com;&nbsp;</div><div>&nbsp;</div><div>location = / &#123;&nbsp;</div><div>if ($http_host ~ news&#92;.c1gstudio.com$) &#123;&nbsp;</div><div>rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;&nbsp;</div><div>break;&nbsp;</div><div>&#125;&nbsp;</div><div>&#125;</div><div>discuz ucenter 头像 rewrite 优化</div><div>location ^~ /ucenter &#123;&nbsp;</div><div>location ~ .*&#92;.php?$&nbsp;</div><div>&#123;&nbsp;</div><div>#fastcgi_pass unix:/tmp/php-cgi.sock;&nbsp;</div><div>fastcgi_pass 127.0.0.1:9000;&nbsp;</div><div>fastcgi_index index.php;&nbsp;</div><div>include fcgi.conf;&nbsp;</div><div>&#125;&nbsp;</div><div>&nbsp;</div><div>location /ucenter/data/avatar &#123;&nbsp;</div><div>log_not_found off;&nbsp;</div><div>access_log off;&nbsp;</div><div>location ~ /(.*)_big&#92;.jpg$ &#123;&nbsp;</div><div>error_page 404 /ucenter/images/noavatar_big.gif;&nbsp;</div><div>&#125;&nbsp;</div><div>location ~ /(.*)_middle&#92;.jpg$ &#123;&nbsp;</div><div>error_page 404 /ucenter/images/noavatar_middle.gif;&nbsp;</div><div>&#125;&nbsp;</div><div>location ~ /(.*)_small&#92;.jpg$ &#123;&nbsp;</div><div>error_page 404 /ucenter/images/noavatar_small.gif;&nbsp;</div><div>&#125;&nbsp;</div><div>expires 300;&nbsp;</div><div>break;&nbsp;</div><div>&#125;&nbsp;</div><div>&#125;</div><div>jspace rewrite</div><div>location ~ .*&#92;.php?$&nbsp;</div><div>&#123;&nbsp;</div><div>#fastcgi_pass unix:/tmp/php-cgi.sock;&nbsp;</div><div>fastcgi_pass 127.0.0.1:9000;&nbsp;</div><div>fastcgi_index index.php;&nbsp;</div><div>include fcgi.conf;&nbsp;</div><div>&#125;&nbsp;</div><div>&nbsp;</div><div>location ~* ^/index.php/&nbsp;</div><div>&#123;&nbsp;</div><div>rewrite ^/index.php/(.*) /index.php?$1 break;&nbsp;</div><div>fastcgi_pass 127.0.0.1:9000;&nbsp;</div><div>fastcgi_index index.php;&nbsp;</div><div>include fcgi.conf;&nbsp;</div><div>&#125;</div><br/>Tags - <a href="http://liuxinxiu.com:80/tags/nginx/" rel="tag">nginx</a> , <a href="http://liuxinxiu.com:80/tags/rewrite/" rel="tag">rewrite</a>
]]>
</description>
</item><item>
<link>http://liuxinxiu.com:80/nginx_rewrite/#blogcomment</link>
<title><![CDATA[[评论] NGINX 重写规则]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://liuxinxiu.com:80/nginx_rewrite/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>