golang实现的http反向代理

今天找http代理的例子,一直没找到满意的,终于在github上找到一个,非常的精简,非常的入眼,记录一下。

package main

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

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        director := func(req *http.Request) {
            req = r
            req.URL.Scheme = "http"
            req.URL.Host = r.Host
        }
        proxy := &httputil.ReverseProxy{Director: director}
        proxy.ServeHTTP(w, r)
    })

    log.Fatal(http.ListenAndServe(":8888", nil))
}

出处:https://gist.github.com/montanaflynn/304aa58b188f036204af

    原文作者:TEAA
    原文地址: https://www.jianshu.com/p/f868c88b45e1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞