2013-02-05 255 views
2

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

回答

2

這是一個無法用任何語言自動解決的問題,甚至PHP。像wordpress這樣的應用程序有一個設置來指定根。某些應用程序需要完整的網址(https://domain.tld/bar)或者只能有路徑(/ bar)。

無論採用哪種方式,您都需要創建一個設置,然後手動配置所有重定向以便從那裏開始創建。您可以創建自己的重定向功能,因此每次執行重定向時都不需要實施它。