举例 http://www.jastme.org/Handler/FileHandler.ashx?fid=207ec068-2571-404d-84cd-f4c2a475865f_152X152_1
我现在有这样一个需求
把http://www.jastme.org/Handler/FileHandler.ashx?fid=207ec068-2571-404d-84cd-f4c2a475865f_152X152_1
在nginx中重写成
http://www.jastme.org/Handler/FileHandler.ashx?fid=207ec068-2571-404d-84cd-f4c2a475865f_152X152
代码 错误示范
 location ~* /Handler/FileHandler\.ashx?([^_]+)(_[0-9]+)$ {
		set $m1 $1;
		rewrite  ^/(.*)$  /\1?$m1;
	}
这个为什么是错误的呢?
因为location只能匹配uri 不能匹配参数
大概意思 uri
www.jastme.org/Handler/FileHandler.ashx?
参数
fid=207ec068-2571-404d-84cd-f4c2a475865f_152X152_1
就是这样 后面的这段在location中是不能匹配的,所以不管怎么写 都不会成功的,因为location的规则就不会生效
正确代码
        location ~*/Handler/FileHandler\.ashx?
        {
                if ($request_uri ~* ^/Handler/FileHandler.ashx\?([^_]+)(_[0-9]+)$) {
                set $m1 $1;
                rewrite ^/Handler/FileHandler.ashx /Handler/FileHandler.ashx?$m1? permanent;}
只有使用$request_uri 才能获取完整的链接
nginx location 与 $request_uri 的匹配区别
原文:http://my.oschina.net/jastme/blog/528541