change type http to reverse

master
fangdingjun 8 years ago
parent 85276f92a1
commit 83fef17f9c

@ -60,14 +60,15 @@
type: unix type: unix
path: /var/run/php-fpm/www.sock 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 # this act as reverse proxy
urlprefix: /proxy/ urlprefix: /proxy/
type: http type: reverse
target: target:
type: http type: http
host: 10.10.1.1 host: 10.10.1.1
port: 8080 port: 8080
path: /
- &example1 - &example1
<<: *example1_www <<: *example1_www
hostname: example1.com hostname: example1.com

@ -4,10 +4,11 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"log"
"net" "net"
"net/http" "net/http"
//"net/url" "net/http/httputil"
"log" "net/url"
"os" "os"
"regexp" "regexp"
//"path/filepath" //"path/filepath"
@ -44,7 +45,7 @@ func initRouters(cfg conf) {
registerUwsgiHandler(rule, r) registerUwsgiHandler(rule, r)
case "fastcgi": case "fastcgi":
registerFastCGIHandler(rule, h.Docroot, r) registerFastCGIHandler(rule, h.Docroot, r)
case "http": case "reverse":
registerHTTPHandler(rule, r) registerHTTPHandler(rule, r)
default: default:
fmt.Printf("invalid type: %s\n", rule.Type) fmt.Printf("invalid type: %s\n", rule.Type)
@ -62,7 +63,7 @@ func initRouters(cfg conf) {
registerUwsgiHandler(rule, router) registerUwsgiHandler(rule, router)
case "fastcgi": case "fastcgi":
registerFastCGIHandler(rule, l.Docroot, router) registerFastCGIHandler(rule, l.Docroot, router)
case "http": case "reverse":
registerHTTPHandler(rule, router) registerHTTPHandler(rule, router)
default: default:
fmt.Printf("invalid type: %s\n", rule.Type) fmt.Printf("invalid type: %s\n", rule.Type)
@ -180,13 +181,19 @@ func registerHTTPHandler(r rule, router *mux.Router) {
switch r.Target.Type { switch r.Target.Type {
case "unix": case "unix":
addr = r.Target.Path addr = r.Target.Path
u = newProxy(addr, r.URLPrefix)
case "http": case "http":
addr = fmt.Sprintf("%s:%d", r.Target.Host, r.Target.Port) 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: default:
fmt.Printf("invalid scheme: %s, only support unix, http", r.Target.Type) fmt.Printf("invalid scheme: %s, only support unix, http", r.Target.Type)
os.Exit(-1) os.Exit(-1)
} }
u = newProxy(addr, r.URLPrefix)
p := strings.TrimRight(r.URLPrefix, "/") p := strings.TrimRight(r.URLPrefix, "/")
router.PathPrefix(r.URLPrefix).Handler( router.PathPrefix(r.URLPrefix).Handler(
http.StripPrefix(p, u)) http.StripPrefix(p, u))

Loading…
Cancel
Save