英文:
Go: relative http.Redirect behind Apache mod_proxy
问题
我有一个简单的Go服务器在:8888
上监听。
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("redirecting to foo")
http.Redirect(w, r, "foo", http.StatusFound)
})
http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("fooooo"))
})
if err := http.ListenAndServe(":8888", nil); err != nil {
log.Fatal(err)
}
}
我将其放在Apache后面,将所有请求代理到/bar/*
到Go服务器。
我使用ProxyPassMatch
来实现这一点。
ProxyPassMatch ^/bar/?(:?(.*))?$ http://localhost:8888/$2
问题是,当我访问/bar/
时,我被重定向到/foo
而不是/bar/foo
。
有没有办法让这个工作,或者我需要在所有重定向前加上/bar
前缀?
英文:
I have a simple go server listening on :8888
.
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("redirecting to foo")
http.Redirect(w, r, "foo", http.StatusFound)
})
http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("fooooo"))
})
if err := http.ListenAndServe(":8888", nil); err != nil {
log.Fatal(err)
}
}
I have this sitting behind apache which proxies all requests to /bar/*
to the go server.
I'm using ProxyPassMatch
to do this.
ProxyPassMatch ^/bar/?(:?(.*))?$ http://localhost:8888/$2
Problem is that is what when I go to /bar/
I get redirected to /foo
instead of /bar/foo
Is there a way to get this working or do I need to prefix all my redirects with /bar
?
答案1
得分: 1
如果你想让Apache在重定向响应中重写位置,你还需要在配置中包含ProxyPassReverse指令。像这样的配置应该可以解决问题:
ProxyPassReverse /bar/ http://localhost:8888/
英文:
If you want Apache to rewrite locations in redirect responses, you'll need to also include the ProxyPassReverse directive in your configuration. Something like this should do the trick:
ProxyPassReverse /bar/ http://localhost:8888/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论