diff --git a/config/directives.go b/config/directives.go index 679ea5ea..4f7c5b8a 100644 --- a/config/directives.go +++ b/config/directives.go @@ -43,6 +43,7 @@ var directiveOrder = []directive{ // Essential directives that initialize vital configuration settings {"root", setup.Root}, {"tls", setup.TLS}, + {"bindaddr", setup.BindAddr}, // Other directives that don't create HTTP handlers {"startup", setup.Startup}, diff --git a/config/setup/bindaddr.go b/config/setup/bindaddr.go new file mode 100644 index 00000000..23b1eb2e --- /dev/null +++ b/config/setup/bindaddr.go @@ -0,0 +1,12 @@ +package setup + +import "github.com/mholt/caddy/middleware" + +func BindAddr(c *Controller) (middleware.Middleware, error) { + for c.Next() { + if !c.Args(&c.BindAddress) { + return nil, c.ArgErr() + } + } + return nil, nil +} diff --git a/server/config.go b/server/config.go index 591f9a48..a45e4797 100644 --- a/server/config.go +++ b/server/config.go @@ -11,6 +11,9 @@ type Config struct { // The hostname or IP on which to serve Host string + // The address to bind on - defaults to Host if empty + BindAddress string + // The port to listen on Port string @@ -44,6 +47,9 @@ type Config struct { // Address returns the host:port of c as a string. func (c Config) Address() string { + if c.BindAddress != "" { + return net.JoinHostPort(c.BindAddress, c.Port) + } return net.JoinHostPort(c.Host, c.Port) }