> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/devops/ngnix/ji-chu-zhi-shi/4.-nginx-fu-wu-qi-de-dai-li-fu-wu/4.3-nginx-fu-wu-qi-de-fan-xiang-dai-li-fu-wu/fan-xiang-dai-li-de-ji-ben-she-zhi-de-21-ge-zhi-ling/proxy_redirect.md).

# proxy\_redirect

<mark style="color:blue;">**proxy\_redirect**</mark>指令用于<mark style="color:blue;">**修改被代理服务器返回的响应头中的Location头域和“Refresh”头域**</mark>，与proxy\_pass指令配合使用。

比如，Nginx服务器通过proxy\_pass指令将客户端的请求地址重写为被代理服务器的地址，那么Nginx服务器返回给客户端的响应头中“Location”头域显示的地址就应该和客户端发起请求的地址相对应，而不是代理服务器直接返回的地址信息，否则就会出问题。该指令解决了这个问题，可以把代理服务器返回的地址信息更改为需要的地址信息。其语法结构为：

{% code lineNumbers="true" %}

```nginx
proxy_redirect redirect replacement;
proxy_redirect default;
proxy_redirect off;
```

{% endcode %}

* <mark style="color:blue;">**redirect**</mark>，匹配响应的“Location”头的字符串，支持变量的使用和正则表达式。
* <mark style="color:blue;">**replacement**</mark>，用于替换redirect变量内容的字符串，支持变量的使用。

{% hint style="success" %}
对于第1个结构，假设被代理服务器返回的响应头中“Location”头域为：

```http
Location: http://localhost:8081/proxy/some/uri/
```

该指令设置为：

```nginx
proxy_redirect  http://localhost:8081/proxy/  http://myweb/frontend/;
```

Nginx服务器会将“Location”头的值更改为：

```http
Location: http://myweb/frontend//some/uri/
```

{% endhint %}

{% hint style="success" %}
结构2使用<mark style="color:blue;">**default**</mark>，**代表使用location块的uri变量作为replacement，并使用proxy\_pass变量作为redirect**。

下面两段配置的效果是等同的：

```nginx
#配置1
location /server/
{
  proxy_pass  http://proxyserver/source/;
  proxy_redirect  default;
}
#配置2
location /server/
{
  proxy_pass  http://proxyserver/source/;
  proxy_redirect  http://proxyserver/source/  /server/;
}
```

{% endhint %}

{% hint style="warning" %} <mark style="color:orange;">**使用结构3可以将当前作用域下所有的proxy\_redirect指令配置全部设置为无效。**</mark>
{% endhint %}
