---
title: "nginx转发不带参数"
author: "dxm1986"
date: "2011-07-23T20:34:50Z"
tags: []
categories: ["Web前端"]
slug: "nginx"
draft: false
lastmod: "2013-04-18T14:33:46Z"
id: 1131436
url: "https://wolfdream.iteye.com/blog/1131436"
---

## Nginx: rewrite重定向的参数处理问题

星期日, 九 19th, 2010 | 发布在：[Web服务器](http://www.yeeann.com/cat/system-utility/web-servser "查看 Web服务器 的全部文章") | [1 条评论](http://www.yeeann.com/archives/165.html#comments "Nginx: rewrite重定向的参数处理问题 上的评论")

今天在给某网站写rewrite重定向规则时，碰到了这个关于**重定向的参数处理**问题。默认的情况下，Nginx在进行rewrite后都会自动添加上旧地址中的参数部分，而这对于重定向到的新地址来说可能是多余。虽然这也不会对重定向的结果造成多少影响，但当你注意到新地址中包含有多余的“*?xxx=xxx*”时，心里总还是会觉得不爽。那么该如何来处理这部分的内容呢？看了下面两个简单的例子你就会明白了。

例如：  
把*http://example.com/test.php?para=xxx* 重定向到 *http://example.com/new*  
若按照默认的写法：rewrite ^/test.php(.\*) /new permanent;  
重定向后的结果是：*http://example.com/new?para=xxx*  
如果改写成：rewrite ^/test.php(.\*) /new? permanent;  
那结果就是：*http://example.com/new*

所以，关键点就在于“？”这个尾缀。假如又想保留某个特定的参数，那又该如何呢？可以利用Nginx本身就带有的$arg\_PARAMETER参数来实现。

例如：  
把*http://example.com/test.php?para=xxx&p=xx* 重写向到 *http://example.com/new?p=xx*  
可以写成：rewrite  ^/test.php   /new?p=$arg\_p?  permanent;

只求结果的朋友可以直接忽略前面的内容，**看这里**：

> rewrite  ^/test.php  /new  permanent;       //重写向带参数的地址
> 
> rewrite  ^/test.php  /new?  permanent;      //重定向后不带参数
> 
> rewrite  ^/test.php   /new?id=$arg\_id?  permanent;    //重定向后带指定的参数

**标签:** [Nginx](http://www.yeeann.com/tag/nginx)

 

```
upstream biz{
        server 10.4.16.8:8080 max_fails=1 fail_timeout=600s weight=100;
    }

location /vip/ { 
  proxy_pass          http://backend_lattice_web/;
} 

访问http://xiaozhao.renren.com/vip/12345  
后端服务接收到的URI：/12345

想得到同样的结果也可以用rewrite 去做:
```

```
location /vip/ { 
  proxy_pass          http://backend_lattice_web/;
} 

location /vip {
            rewrite ^/vip/(.*)$  /$1  break;
            proxy_pass          http://web_backend;
        }
```

 

```
upstream biz{
        server 10.4.16.8:8080 max_fails=1 fail_timeout=600s weight=100;
    }

location /vip { 
  proxy_pass          http://backend_lattice_web/;
} 

访问http://xiaozhao.renren.com/vip/12345  
后端服务接收到的URI：//12345
```

 

```
upstream biz{
        server 10.4.16.8:8080 max_fails=1 fail_timeout=600s weight=100;
    }

location /vip { 
  proxy_pass          http://backend_lattice_web;
} 

访问http://xiaozhao.renren.com/vip/12345  
后端服务接收到的URI：/vip/12345
```

 

 

 

 

 

 

 

 

 

[http://www.yeeann.com/archives/165.html](http://www.yeeann.com/archives/165.html)