mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-20 22:52:58 -05:00
cmd/reverse_proxy: Add --change-host-header flag
"Transparent mode" is the default, just like the actual handler.
This commit is contained in:
parent
ca5c679880
commit
0130b699df
1 changed files with 15 additions and 6 deletions
|
@ -36,7 +36,7 @@ func init() {
|
||||||
caddycmd.RegisterCommand(caddycmd.Command{
|
caddycmd.RegisterCommand(caddycmd.Command{
|
||||||
Name: "reverse-proxy",
|
Name: "reverse-proxy",
|
||||||
Func: cmdReverseProxy,
|
Func: cmdReverseProxy,
|
||||||
Usage: "[--from <addr>] [--to <addr>]",
|
Usage: "[--from <addr>] [--to <addr>] [--change-host-header]",
|
||||||
Short: "A quick and production-ready reverse proxy",
|
Short: "A quick and production-ready reverse proxy",
|
||||||
Long: `
|
Long: `
|
||||||
A simple but production-ready reverse proxy. Useful for quick deployments,
|
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.
|
||||||
|
|
||||||
If the --from address has a domain name, Caddy will attempt to serve the
|
If the --from address has a domain name, Caddy will attempt to serve the
|
||||||
proxy over HTTPS with a certificate.
|
proxy over HTTPS with a certificate.
|
||||||
|
|
||||||
|
If --change-host-header is set, the Host header on the request will be modified
|
||||||
|
from its original incoming value to the address of the upstream. (Otherwise, by
|
||||||
|
default, all incoming headers are passed through unmodified.)
|
||||||
`,
|
`,
|
||||||
Flags: func() *flag.FlagSet {
|
Flags: func() *flag.FlagSet {
|
||||||
fs := flag.NewFlagSet("file-server", flag.ExitOnError)
|
fs := flag.NewFlagSet("file-server", flag.ExitOnError)
|
||||||
fs.String("from", "", "Address to receive traffic on")
|
fs.String("from", "", "Address on which to receive traffic")
|
||||||
fs.String("to", "", "Upstream address to proxy traffic to")
|
fs.String("to", "", "Upstream address to which to to proxy traffic")
|
||||||
|
fs.Bool("change-host-header", false, "Set upstream Host header to address of upstream")
|
||||||
return fs
|
return fs
|
||||||
}(),
|
}(),
|
||||||
})
|
})
|
||||||
|
@ -59,6 +64,7 @@ proxy over HTTPS with a certificate.
|
||||||
func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
||||||
from := fs.String("from")
|
from := fs.String("from")
|
||||||
to := fs.String("to")
|
to := fs.String("to")
|
||||||
|
changeHost := fs.Bool("change-host-header")
|
||||||
|
|
||||||
if from == "" {
|
if from == "" {
|
||||||
from = "localhost:" + httpcaddyfile.DefaultPort
|
from = "localhost:" + httpcaddyfile.DefaultPort
|
||||||
|
@ -97,13 +103,16 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
|
||||||
handler := Handler{
|
handler := Handler{
|
||||||
TransportRaw: caddyconfig.JSONModuleObject(ht, "protocol", "http", nil),
|
TransportRaw: caddyconfig.JSONModuleObject(ht, "protocol", "http", nil),
|
||||||
Upstreams: UpstreamPool{{Dial: toURL.Host}},
|
Upstreams: UpstreamPool{{Dial: toURL.Host}},
|
||||||
Headers: &headers.Handler{
|
}
|
||||||
|
|
||||||
|
if changeHost {
|
||||||
|
handler.Headers = &headers.Handler{
|
||||||
Request: &headers.HeaderOps{
|
Request: &headers.HeaderOps{
|
||||||
Set: http.Header{
|
Set: http.Header{
|
||||||
"Host": []string{"{http.reverse_proxy.upstream.host}"},
|
"Host": []string{"{http.reverse_proxy.upstream.hostport}"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
route := caddyhttp.Route{
|
route := caddyhttp.Route{
|
||||||
|
|
Loading…
Add table
Reference in a new issue