Http nginx反向代理重定向错误的应用

Http nginx反向代理重定向错误的应用,http,redirect,nginx,go,reverse-proxy,Http,Redirect,Nginx,Go,Reverse Proxy,我正在运行一个nginx(0.7.67)作为反向代理和golang应用程序。nginx服务器配置如下: ... 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

我正在运行一个nginx(0.7.67)作为反向代理和golang应用程序。nginx服务器配置如下:

...
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;
}
...
我的golang应用程序的来源(没有意义,只是一个示例):

问题:当我将浏览器指向我的应用程序的URL()时,我可以看到文本“You reach root”。但是,如果我尝试打开,我会被重定向到而不是。似乎我的应用程序正在重定向到nginx服务器的根目录,而不是应用程序


问题:如何从应用程序内部(运行在nginx反向代理之后)重定向到应用程序的根目录,而不是服务器的根目录?

这是一个任何语言都无法自动解决的问题,即使是PHP。像wordpress这样的应用程序所做的是有一个指定根的设置。有些应用程序需要完整的url(),或者只需要一个路径(/bar)

无论哪种方式,您都需要创建一个设置,然后手动配置所有重定向,使其基于该设置。您可以创建自己的重定向函数,这样就不必在每次执行重定向时都实现它

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)
}