golang 启动一个简单 http 代理

  1. golang 转发 http 请求

golang 转发 http 请求

package main

import (
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
)

//将request转发给 http://127.0.0.1:2003
func proxyHandler(w http.ResponseWriter, r *http.Request) {

    trueServer := "http://127.0.0.1:15672"

    url, err := url.Parse(trueServer)
    if err != nil {
        log.Println(err)
        return
    }

    proxy := httputil.NewSingleHostReverseProxy(url)
    proxy.ServeHTTP(w, r)

}

func main() {
    http.HandleFunc("/", proxyHandler)
    log.Fatal(http.ListenAndServe(":2002", nil))
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。也可以邮件至 296245956@qq.com
github