I am running an nginx (0.7.67) as a reverse proxy and a golang application. The nginx server is configured as follows:
...
location /bar/ {
proxy_pass http://localhost:8088/;
proxy_redirect off;
proxy_set_header Host $host;
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 https;
}
...
The source of my golang application (it doesn't make sense, only an example):
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "You reached root")
}
func foo(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}
func main() {
http.HandleFunc("/", root)
http.HandleFunc("/foo", foo)
http.ListenAndServe("localhost:8088", nil)
}
Problem: When I direct my browser to the URL of my application (https://domain.tld/bar/) I can see the text "You reached root". However, if I try to open https://domain.tld/bar/foo, I get redirected to https://domain.tld instead of https://domain.tld/bar. It seems that my application is redirecting to the root of the nginx server and not the application.
Question: How can I redirect from the inside of my application (running behind an nginx reverse proxy) to the root of my application, instead of the root of the server?