Go: 在 Apache mod_proxy 后面使用相对路径的 http.Redirect

huangapple go评论90阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2014年1月21日 06:41:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/21245667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定