Ejemplo n.º 1
0
func main() {
	// get /blog => to server1
	// get /login => to server2
	var target1, target2 *url.URL
	var err error

	if target1, err = url.Parse(SERVER1); err != nil {
		log.Fatal("parse url: ", err)
	}

	if target2, err = url.Parse(SERVER2); err != nil {
		log.Fatal("parse url: ", err)
	}

	reverseProxy := new(httputil.ReverseProxy)

	reverseProxy.Director = func(req *http.Request) {
		req.URL.Scheme = "http"

		if strings.HasPrefix(req.URL.Path, "/blog") {
			req.URL.Host = target1.Host
		}

		if strings.HasPrefix(req.URL.Path, "/login") {
			req.URL.Host = target2.Host
		}
	}

	err = http.ListenAndServe(":80", reverseProxy)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Ejemplo n.º 2
0
func setProxyDirector(proxy *httputil.ReverseProxy) {
	director := proxy.Director
	proxy.Director = func(req *http.Request) {
		director(req)
		// use RequestURI so that we aren't unescaping encoded slashes in the request path
		req.URL.Opaque = req.RequestURI
		req.URL.RawQuery = ""
	}
}
Ejemplo n.º 3
0
func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
	director := proxy.Director
	proxy.Director = func(req *http.Request) {
		director(req)
		// use RequestURI so that we aren't unescaping encoded slashes in the request path
		req.Host = target.Host
		req.URL.Opaque = req.RequestURI
		req.URL.RawQuery = ""
	}
}
Ejemplo n.º 4
0
func main() {
	var srv httputil.ReverseProxy

	log.SetFlags(0)
	flag.Parse()
	if *file == "" {
		log.Print("config file (-c) is required")
		flag.PrintDefaults()
		os.Exit(1)
	}
	if cfg, err := config.ParseFile(*file); err != nil {
		log.Fatalf("parse %s failed: %s", *file, err)
	} else {
		srv.Director = cfg.MapRequest
		if *addr == "" {
			*addr = cfg.Address
		}
	}
	http.Handle("/render", accesslog.Handler(&srv, nil))
	log.Fatal(http.ListenAndServe(*addr, nil))
}