2013-02-05 224 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)。

无论采用哪种方式,您都需要创建一个设置,然后手动配置所有重定向以便从那里开始创建。您可以创建自己的重定向功能,因此每次执行重定向时都不需要实施它。