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))
}