我正在运行一个nginx(0.7.67)作为反向代理和golang应用程序。 nginx的服务器配置如下: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应用程序的源(它没有意义,只是一个例子):
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)
}
问题:当我直接把我的浏览器到我的应用程序的URL(https://domain.tld/bar/)我可以看到文字“你到达了根”。但是,如果我尝试打开https://domain.tld/bar/foo,我将重定向到https://domain.tld而不是https://domain.tld/bar。看来我的应用程序正在重定向到nginx服务器的根目录,而不是应用程序。
问题:如何从应用程序的内部(运行在nginx反向代理之后)重定向到我的应用程序的根目录而不是服务器的根目录?