go 语言 请求转发 本地转发到域名

 go语言http本身的反向代理不支持一个ip多域名的情况,所以用下面这个

// proxyServer.go

package main

import (
"fmt"
    "log"
    "net/http"
	"io/ioutil"
	"strings"
   
)

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

    resp, err := http.Get("http://ylbzj.nanyang.gov.cn"+r.RequestURI)
    if err != nil {
        // handle error
    }
 //resp.Header.Add("Content-Type", "text/css")
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }
	if(strings.Index(r.RequestURI, "css") > -1){	
 w.Header().Set("Content-type", "text/css")
 }
    fmt.Fprintln(w,string(body))


}


func main() {
    http.HandleFunc("/", helloHandler)
    log.Fatal(http.ListenAndServe(":2002", nil))
}
发布了455 篇原创文章 · 获赞 77 · 访问量 131万+

猜你喜欢

转载自blog.csdn.net/az44yao/article/details/103799521
今日推荐