proxy_redirect

The `proxy_redirect` directive modifies the `Location` and `Refresh` headers in a proxied response to make them suitable for the client.

Syntaxproxy_redirect [default | off | redirect_uri new_uri];
Defaultnone
Contexthttp, server, location
Arguments1-2

Description

The proxy_redirect directive is used in NGINX to adjust the HTTP headers related to redirects from a proxied server. When requests are proxied, the response might contain Location or Refresh headers that inform the client where to redirect to. However, these headers may still point to the original upstream server rather than the client-facing NGINX server. This is where the proxy_redirect directive comes into play. It accepts one or two arguments: the first one specifies the URL to be modified (the upstream server URL), and the second one, which is optional, specifies the new URL that should be sent back to the client.

For example, if a proxied server sends a Location header pointing to http://upstream-server/uri, and if you set proxy_redirect http://upstream-server/ http://client-server/;, NGINX will modify the Location header to http://client-server/uri. This ensures that the client receives redirects pointing to NGINX instead of the proxied server. Multiple proxy_redirect directives can be specified, which allows for complex routing scenarios where different upstream servers may need different rewrites, making it highly flexible for varied environments.

Config Example

location /api {
    proxy_pass http://upstream-server;
    proxy_redirect http://upstream-server/ http://localhost:8080/;
}

Be careful with trailing slashes; they can affect the redirect URL construction.

The proxy_redirect directive must be used in a proper context (http, server, location); using it in the wrong context will lead to an error.

← Back to all directives