代码改变世界

nginx rewrite

2010-11-17 15:52  Sun Yongyue  阅读(539)  评论(0编辑  收藏  举报

不支持location中针对参数的rewrite

"

Note that locations of all types test only a request URI part without a query string. This is done because arguments in the query string may be given in several ways, for example:

/index.php?user=john&page=1
/index.php?page=1&user=john

Besides, anyone may request anything in the query string:

/index.php?page=1&something+else&user=john

"

第一次遇到这个问题是几个月前项目一次性能大改造,架构修改后,入口节点也都发生变化。在nginx中使用rewrite进行兼容时,发现怎么都匹配不上,查询后得知不支持location中对参数的rewrite。今天刚好又有人再问,想起来这段时间有几个人问过,记在这里提示下。

 

可以用以下方式变通。

if ($request_uri ~ "YOURPATTERN")

{

rewrite ^ http://YOURHOST$request_uri?;
}

 

以上需要pcre的支持,关于nignx里添加pcre支持,可以查看文章《nginx编译记》