douxu2081 2019-09-08 13:01
浏览 749

https到http反向代理

I want to build a reverse proxy in golang, which acts as a middleware that forwards https requests to an http API and then forwards the response to the client in a secure and encrypted way.

I've had a look at the httputil.ReverseProxy but I'd like to know if I'm getting this right

this is the function that returns the handler that takes care of forwarding https to http and response back to https:

func (s *service) setUpRouter() (http.Handler, error) {
    // targetURL is the API where the requests are forwarded to
    targetURL, err := url.Parse(s.target) 
    if err != nil {
        return nil, err
    }
    reverseProxy := httputil.NewSingleHostReverseProxy(targetURL)

    reverseProxy.Director = func(request *http.Request) {
        request.Header.Add("X-Forwarded-Host", request.Host)
        request.Header.Add("X-Origin-Host", targetURL.Host)
        request.URL.Scheme = "http"
        request.URL.Host = targetURL.Host
    }

    return reverseProxy, nil
}

I'd expect the reverse proxy to forward the request to the http API and returnt the response to the client in the form of https.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!