我正在運行一個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反向代理之後)重定向到我的應用程序的根目錄而不是服務器的根目錄?