diff --git a/README.md b/README.md index 7cfbefd..4ba447b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ Nova Proxy needs a configuration file: { "path": "/", "host": "http://blog:8000", - "modifyResponse": true + "modifyResponse": true, + "proxyPreserveHost": true } ] } @@ -30,6 +31,8 @@ The `locations` items require the `path` and `host` to let know to Nova Proxy wh The `modifyResponse` enable the serve-side includes to that location. +The `proxyPreserveHost` is used to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server. + ## Using Nova Proxy with [Ara CLI](https://github.com/ara-framework/ara-cli) Before to run the command we need to set the `HYPERNOVA_BATCH` variable using the Nova service endpoint. diff --git a/config/config.go b/config/config.go index e19d0c5..cb3624d 100644 --- a/config/config.go +++ b/config/config.go @@ -16,9 +16,10 @@ import ( ) type location struct { - Path string - Host string - ModifyResponse bool + Path string + Host string + ModifyResponse bool + ProxyPreserveHost bool } type configuration struct { @@ -40,7 +41,7 @@ func ReadConfigFile() { logger.Error(err, "Config file not found") err = json.Unmarshal(e, &jsonConfig) - logger.Fatal(err, "Unable to parse " + os.Getenv("CONFIG_FILE")) + logger.Fatal(err, "Unable to parse "+os.Getenv("CONFIG_FILE")) } @@ -53,7 +54,7 @@ func SetUpLocations() error { proxy := httputil.NewSingleHostReverseProxy(origin) if location.ModifyResponse { proxy.ModifyResponse = modifyResponse - proxy.Director = modifyRequest(origin) + proxy.Director = modifyRequest(origin, location.ProxyPreserveHost) } http.Handle(location.Path, proxy) } @@ -84,12 +85,15 @@ func modifyResponse(r *http.Response) error { return nil } -func modifyRequest(origin *url.URL) func(req *http.Request) { +func modifyRequest(origin *url.URL, proxyPreserveHost bool) func(req *http.Request) { return func(req *http.Request) { req.Header.Add("X-Forwarded-Host", req.Host) req.Header.Add("X-Origin-Host", origin.Host) req.Header.Del("Accept-Encoding") req.URL.Scheme = "http" req.URL.Host = origin.Host + if !proxyPreserveHost { + req.Host = origin.Host + } } }