@marcel91200 / caddy Mirror · read-only / commits / 0130b699
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

cmd/reverse_proxy: Add --change-host-header flag

"Transparent mode" is the default, just like the actual handler.

Matthew Holt committed Feb 27, 2020 at 20:52 UTC 0130b699df7a3e669a60732b465079cdd7a4addb
1 file changed +15 -6
modules/caddyhttp/reverseproxy/command.go
+15 -6
@@ -36,7 +36,7 @@ func init() {
36 caddycmd.RegisterCommand(caddycmd.Command{
37 Name: "reverse-proxy",
38 Func: cmdReverseProxy,
39 - Usage: "[--from <addr>] [--to <addr>]",
39 + Usage: "[--from <addr>] [--to <addr>] [--change-host-header]",
40 Short: "A quick and production-ready reverse proxy",
41 Long: `
42 A simple but production-ready reverse proxy. Useful for quick deployments,
@@ -46,11 +46,16 @@ Simply shuttles HTTP traffic from the --from address to the --to address.
46
47 If the --from address has a domain name, Caddy will attempt to serve the
48 proxy over HTTPS with a certificate.
49 +
50 +If --change-host-header is set, the Host header on the request will be modified
51 +from its original incoming value to the address of the upstream. (Otherwise, by
52 +default, all incoming headers are passed through unmodified.)
53 `,
54 Flags: func() *flag.FlagSet {
55 fs := flag.NewFlagSet("file-server", flag.ExitOnError)
52 - fs.String("from", "", "Address to receive traffic on")
53 - fs.String("to", "", "Upstream address to proxy traffic to")
56 + fs.String("from", "", "Address on which to receive traffic")
57 + fs.String("to", "", "Upstream address to which to to proxy traffic")
58 + fs.Bool("change-host-header", false, "Set upstream Host header to address of upstream")
59 return fs
60 }(),
61 })
@@ -59,6 +64,7 @@ proxy over HTTPS with a certificate.
64 func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
65 from := fs.String("from")
66 to := fs.String("to")
67 + changeHost := fs.Bool("change-host-header")
68
69 if from == "" {
70 from = "localhost:" + httpcaddyfile.DefaultPort
@@ -97,13 +103,16 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
103 handler := Handler{
104 TransportRaw: caddyconfig.JSONModuleObject(ht, "protocol", "http", nil),
105 Upstreams: UpstreamPool{{Dial: toURL.Host}},
100 - Headers: &headers.Handler{
106 + }
107 +
108 + if changeHost {
109 + handler.Headers = &headers.Handler{
110 Request: &headers.HeaderOps{
111 Set: http.Header{
103 - "Host": []string{"{http.reverse_proxy.upstream.host}"},
112 + "Host": []string{"{http.reverse_proxy.upstream.hostport}"},
113 },
114 },
106 - },
115 + }
116 }
117
118 route := caddyhttp.Route{