'Using NGINX proxy_pass on / location forces a 301 redirection

I want to configure NGINX to show the content of some other website I have. I have my website hosted at: https://example.com/my/awesome/website. Now, I want https://hello-world.com showing the same content as https://example.com/my/awesome/website.

I did write the following on my nginx.conf:

location = / {
    proxy_pass http://example.com/my/awesome/website/;
    proxy_set_header   Host              $host;
    proxy_set_header   Authorization     "";
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

However, when I try to reach https://hello-world.com, it redirects me with a 301 to https://hello-world.com/my/awesome/website/. It shows the content correctly, but that's not what I want.

I tried the same thing, but not in the root directory:

location = /test/ {
    proxy_pass http://example.com/my/awesome/website/;
    proxy_set_header   Host              $host;
    proxy_set_header   Authorization     "";
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

and when I try to reach https://hello-world.com/test/ it works fine.

I am wondering why on location / a 301 redirection is happening.



Solution 1:[1]

Try this (did not test), you can get explanation from here):

location = / {
    proxy_pass http://example.com/my/awesome/website/;
    proxy_redirect http://example.com/my/awesome/website/ /; 
    proxy_set_header   Host              $host;
    proxy_set_header   Authorization     "";
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user973254