proxy_redirect

Syntax: proxy_redirect default;
proxy_redirect off;
proxy_redirect redirect replacement;
Default: proxy_redirect default;
Context: http, server, location

Sets the text that should be changed in the “Location” and “Refresh” header fields of a proxied server response. Suppose a proxied server returned the header field “Location: http://localhost:8000/two/some/uri/”. The directive

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

will rewrite this string to “Location: http://frontend/one/some/uri/”.

A server name may be omitted in the replacement string:

proxy_redirect http://localhost:8000/two/ /;

then the primary server’s name and port, if different from 80, will be inserted.

The default replacement specified by the default parameter uses the parameters of the location and proxy_pass directives. Hence, the two configurations below are equivalent:

location /one/ {
    proxy_pass     http://upstream:port/two/;
    proxy_redirect default;
location /one/ {
    proxy_pass     http://upstream:port/two/;
    proxy_redirect http://upstream:port/two/ /one/;

The default parameter is not permitted if proxy_pass is specified using variables.

A replacement string can contain variables:

proxy_redirect http://localhost:8000/ http://$host:$server_port/;

A redirect can also contain (1.1.11) variables:

proxy_redirect http://$proxy_host:8000/ /;

The directive can be specified (1.1.11) using regular expressions. In this case, redirect should either start with the “~” symbol for a case-sensitive matching, or with the “~*” symbols for case-insensitive matching. The regular expression can contain named and positional captures, and replacement can reference them:

proxy_redirect ~^(http://[^:]+):\d+(/.+)$ $1$2;
proxy_redirect ~*/user/([^/]+)/(.+)$      http://$1.example.com/$2;

There could be several proxy_redirect directives:

proxy_redirect default;
proxy_redirect http://localhost:8000/  /;
proxy_redirect http://www.example.com/ /;

The off parameter cancels the effect of all proxy_redirect directives on the current level:

proxy_redirect off;
proxy_redirect default;
proxy_redirect http://localhost:8000/  /;
proxy_redirect http://www.example.com/ /;

Using this directive, it is also possible to add host names to relative redirects issued by a proxied server:

proxy_redirect / /;
doc_nginx
2017-02-09 07:08:27
Comments
Leave a Comment

Please login to continue.