From 83fef17f9c7f85e1210771916ef37f79801970a6 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Wed, 4 Jan 2017 16:54:53 +0800 Subject: [PATCH] change type http to reverse --- config_example.yaml | 5 +++-- routers.go | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/config_example.yaml b/config_example.yaml index 5c80662..8a54353 100644 --- a/config_example.yaml +++ b/config_example.yaml @@ -60,14 +60,15 @@ type: unix path: /var/run/php-fpm/www.sock - - # url start with /proxy/ forward to http://10.10.1.1 + # url start with /proxy/ reverse proxy for http://10.10.1.1/ # this act as reverse proxy urlprefix: /proxy/ - type: http + type: reverse target: type: http host: 10.10.1.1 port: 8080 + path: / - &example1 <<: *example1_www hostname: example1.com diff --git a/routers.go b/routers.go index 8545e08..f5be881 100644 --- a/routers.go +++ b/routers.go @@ -4,10 +4,11 @@ import ( "crypto/tls" "fmt" "github.com/gorilla/mux" + "log" "net" "net/http" - //"net/url" - "log" + "net/http/httputil" + "net/url" "os" "regexp" //"path/filepath" @@ -44,7 +45,7 @@ func initRouters(cfg conf) { registerUwsgiHandler(rule, r) case "fastcgi": registerFastCGIHandler(rule, h.Docroot, r) - case "http": + case "reverse": registerHTTPHandler(rule, r) default: fmt.Printf("invalid type: %s\n", rule.Type) @@ -62,7 +63,7 @@ func initRouters(cfg conf) { registerUwsgiHandler(rule, router) case "fastcgi": registerFastCGIHandler(rule, l.Docroot, router) - case "http": + case "reverse": registerHTTPHandler(rule, router) default: fmt.Printf("invalid type: %s\n", rule.Type) @@ -180,13 +181,19 @@ func registerHTTPHandler(r rule, router *mux.Router) { switch r.Target.Type { case "unix": addr = r.Target.Path + u = newProxy(addr, r.URLPrefix) case "http": addr = fmt.Sprintf("%s:%d", r.Target.Host, r.Target.Port) + u1 := &url.URL{ + Scheme: "http", + Host: addr, + Path: r.Target.Path, + } + u = httputil.NewSingleHostReverseProxy(u1) default: fmt.Printf("invalid scheme: %s, only support unix, http", r.Target.Type) os.Exit(-1) } - u = newProxy(addr, r.URLPrefix) p := strings.TrimRight(r.URLPrefix, "/") router.PathPrefix(r.URLPrefix).Handler( http.StripPrefix(p, u))