2

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?

Kiril
  • 6,009
  • 13
  • 57
  • 77

1 Answers1

2

This is a problem that cannot be solved automatically in any language, even PHP. What applications such as wordpress do is have a setting to specify the root. Some applications require a full url (https://domain.tld/bar) or can just have a path (/bar).

Either way, you need to create a setting and then manually configure all redirects to be based from there. You can create your own redirect function so you don't need to implement it each time you do a redirect.

Stephen Weinberg
  • 51,320
  • 14
  • 134
  • 113