Introduction to proxy_redirect in NGINX

nginx ngx_http_proxy_module proxy_redirect
03 June 2025

Overview of the ngx_http_proxy_module in NGINX


The ngx_http_proxy_module in NGINX provides various capabilities for managing HTTP requests. One of the important features of this module is the 'proxy_redirect' directive, which allows us to automatically modify redirect responses from proxied servers. This can be useful in cases where you have configured a server as a proxy for another server, as it can resolve potential issues.<\/p>

Do not forget that many websites, due to the use of proxies, need to correct redirect URLs. For example, if you set up a proxy application on one server and want users accessing that application to be directed to the correct URLs, 'proxy_redirect' will be very useful.


By using 'proxy_redirect', you can change redirected URLs sent by an upstream server to your own appropriate URLs. In this manner, the user directly remains on your domain, rather than being redirected to the upstream server.


Now, let’s consider a simple example to see how we can use 'proxy_redirect' in NGINX. Suppose you want to run an application on the server 'http://backend.example.com' and enable proxy access to it from the primary server where the user has access, which would be 'http://example.com'.


NGINX code example


server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend.example.com;
proxy_redirect http://backend.example.com http://example.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}<\/code><\/pre>

Code description


server {
This line indicates the beginning of the server block.

listen 80;<\/code>
The server listens on port 80 for HTTP requests.

server_name example.com;<\/code>
This line specifies the domain that the server should respond to.

location / {
This section specifies that requests to the root path ('/') should be handled here.

proxy_pass http://backend.example.com;<\/code>
Requests are passed to the proxied server 'backend.example.com'.

proxy_redirect http://backend.example.com http://example.com;<\/code>
All redirect URLs from 'backend.example.com' are changed to 'example.com'.

proxy_set_header Host $host;<\/code>
The proxied server will receive the Host header set to the current domain name.

proxy_set_header X-Real-IP $remote_addr;<\/code>
This line forwards the actual user's IP address to the upstream server.

}
Ends the location block.

}
Ends the server block.


<\/div>

FAQ

?

What is proxy_redirect and what usage does it have?

?

How can I use proxy_redirect in a block?

?

Can I define multiple proxy_redirect rules?

?

When do I need proxy_redirect?

About Mini Learn

Mini Learn is a platform for short and important programming tutorials in various languages. By using the short tutorials on Mini Learn, you can gain skills in different fields. Our goal is to help the programming community find and solve their questions and errors in programming through Mini Learn.

Contact Us

All rights to the products and content of this site belong to Mini Learn, and any copying of the content for educational purposes is permitted. :)