2.7 KiB
title |
---|
bind (Caddyfile directive) |
bind
Overrides the interface to which the server's socket should bind.
Normally, the listener binds to the empty (wildcard) interface. However, you may force the listener to bind to another hostname or IP instead. This directive accepts only a host, not a port. The port is determined by the site address (defaulting to 443
).
Note that binding sites inconsistently may result in unintended consequences. For example, if two sites on the same port resolve to 127.0.0.1
and only one of those sites is configured with bind 127.0.0.1
, then only one site will be accessible since the other will bind to the port without a specific host; the OS will choose the more specific matching socket. (Virtual hosts are not shared across different listeners.)
bind
accepts network addresses, but may not include a port.
Syntax
bind <hosts...> {
protocols <protocol> ...
}
- <hosts...> is the list of host interfaces to bind which to bind the listener.
- <protocol> is an optional override of the HTTP protocols to enable for the listener, see the server options for its accepted values and their meanings.
Examples
To make a socket accessible only on the current machine, bind to the loopback interface (localhost):
example.com {
bind 127.0.0.1
}
To include IPv6:
example.com {
bind 127.0.0.1 [::1]
}
To bind to 10.0.0.1:8080
:
example.com:8080 {
bind 10.0.0.1
}
To bind to a Unix domain socket at /run/caddy
:
example.com {
bind unix//run/caddy
}
To change the file permission to be writable by all users (defaults to 0200
, which is only writable by the owner):
example.com {
bind unix//run/caddy|0222
}
To bind to a Unix domain socket at /run/caddy/stream.sock
that serves h1 and h2, and another at /run/caddy/dgram.sock
that serves h3:
example.com {
bind unix//run/caddy/stream.sock {
protocols h1 h2
}
bind unixgram//run/caddy/dgram.sock {
protocols h3
}
}
To bind to inherited file descriptors specified with environment placeholders:
http://example.com {
bind fd/{env.CADDY_HTTP_FD} {
protocols h1
}
redir https://example.com{uri} permanent
}
https://example.com {
bind fd/{env.CADDY_HTTPS_FD} {
protocols h1 h2
}
bind fdgram/{env.CADDY_HTTP3_FD} {
protocols h3
}
}
To bind one domain to two different interfaces, with different responses:
example.com {
bind 10.0.0.1
respond "One"
}
example.com {
bind 10.0.0.2
respond "Two"
}