【问题标题】:Golang ReverseProxy with Iris-Go frameworkGolang ReverseProxy 与 Iris-Go 框架
【发布时间】:2017-04-29 19:25:32
【问题描述】:

任何能够将 Golang 的 ReverseProxy 功能与 Iris-Go Web 框架连接起来的人。我无法让它工作。我可以用普通的 net/http 连接它。

func MultiHostReverseProxy(targets map[string]utils.Service) *httputil.ReverseProxy {
    r := regexp.MustCompile(`\/proxy/(?P<Service>[a-zA-Z_-]*)(?P<Path>\/.*)`)
    director := func(req *http.Request) {
        if strings.HasPrefix(req.URL.Path, "/proxy/") {
            temp := r.FindStringSubmatch(req.URL.Path);
            if (len(temp) > 1) {
                system := temp[1]
                if val, ok := targets[system]; ok {
                    s := val.Host + ":" + val.Port
                    req.URL.Scheme = val.Scheme
                    req.URL.Host = s
                    req.URL.Path = temp[2]

                    if enc, ok := GetAxleHeader(req.Header); ok {
                        dec := utils.Decrypt(KEY, enc)
                        req.Header.Set(val.AuthHeader, dec)
                        req.Header.Set(AXLE_HEADER, "")
                    } else {
                        token, nq := utils.FindAxleToken(req.URL.RawQuery);
                        fmt.Printf("%s -> token : %s    newQuery: %s\n", req.URL.RawQuery, token, nq);
                        if token != "" {
                            req.URL.RawQuery = nq
                            dec := utils.Decrypt(KEY, token)
                            req.Header.Set(val.AuthHeader, dec)
                            req.Header.Set(AXLE_HEADER, "")
                        }
                    }
                }
            }
        }
    }
    return &httputil.ReverseProxy{Director: director}
}

如何将这个 ReverseProxy 对象与 iris 框架一起使用;

【问题讨论】:

  • 我不明白为什么有人用iris florinpatan.ro/2016/10/…
  • 你不能使用httputil反向代理,因为iris不使用std lib net/http包。
  • @JimB 版本 5 之后,Iris 建立在 net/http 之上,以完成其 HTTP/2 功能。

标签: go reverse-proxy go-iris


【解决方案1】:

在 Iris 搜索和发布后,我在 iris 资源中得到了这个示例。可能对其他人有帮助

https://github.com/kataras/iris/blob/master/http.go#L1412

【讨论】:

    【解决方案2】:

    使用 iris 你有两个选择,创建一个代理服务器并运行它:

    import "github.com/kataras/iris/core/host"
    [...]
    target, _ := url.Parse("https://example.com")
    go host.NewProxy("example.com:80", target).ListenAndServe()
    // this will proxy all http://example.com to https://example.com
    // you can use that proxy as you like.
    

    创建一个新的代理处理程序并在您喜欢的任何地方使用它:

    import "github.com/kataras/iris/core/host"
    [...]
    target, _ := url.Parse("https://example.com")
    proxy := host.ProxyHandler(target)
    http.ListenAndServe("example.com:80", proxy)
    

    【讨论】:

    • 有没有办法进行动态代理,这意味着,我无法将代理配置为恒定的第三台服务器,而是将其配置在传入的请求标头(比如 X-Forward-To)上。所以那个请求我应该转发到那里提到的相应服务器...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2018-09-19
    • 2018-12-05
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    相关资源
    最近更新 更多