简体   繁体   English

转到:Apache mod_proxy后面的相对http.Redirect

[英]Go: relative http.Redirect behind Apache mod_proxy

I have a simple go server listening on :8888 . 我有一个简单的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)
  }
}

I have this sitting behind apache which proxies all requests to /bar/* to the go server. 我坐在apache后面,该代理将对/bar/*所有请求代理到go服务器。 I'm using ProxyPassMatch to do this. 我正在使用ProxyPassMatch来做到这一点。

 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 问题是当我转到/bar/我被重定向到/foo而不是/bar/foo

Is there a way to get this working or do I need to prefix all my redirects with /bar ? 有没有一种方法可以使此工作正常进行,或者是否需要在所有重定向之前添加/bar前缀?

If you want Apache to rewrite locations in redirect responses, you'll need to also include the ProxyPassReverse directive in your configuration. 如果希望Apache在重定向响应中重写位置,则还需要在配置中包括ProxyPassReverse指令。 Something like this should do the trick: 这样的事情应该可以解决问题:

ProxyPassReverse /bar/ http://localhost:8888/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM