summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-03-24 13:21:18 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-03-24 13:21:18 -0600
commit2acb208e325c16a0f73a81957f9094198e97809c (patch)
tree7fa6a1ff5adf87f5b5a12f15e7a74536e8b933ee
parente02117cb8a2b0b6dbd3dbb1de4d1569ff63ca617 (diff)
caddyhttp: Specify default access log for a server (fix #3185)
-rw-r--r--caddyconfig/httpcaddyfile/httptype.go20
-rw-r--r--caddyconfig/httpcaddyfile/tlsapp.go2
-rw-r--r--modules/caddyhttp/server.go7
3 files changed, 17 insertions, 12 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index 50f3252..0a22807 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -319,11 +319,13 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
}
// hostsFromServerBlockKeys returns a list of all the non-empty hostnames
-// found in the keys of the server block sb. If sb has a key that omits
-// the hostname (i.e. is a catch-all/empty host), then the returned list
-// is empty, because the server block effectively matches ALL hosts.
+// found in the keys of the server block sb, unless allowEmpty is true, in
+// which case a key with no host (e.g. ":443") will be added to the list as
+// an empty string. Otherwise, if allowEmpty is false, and if sb has a key
+// that omits the hostname (i.e. is a catch-all/empty host), then the returned
+// list is empty, because the server block effectively matches ALL hosts.
// The list may not be in a consistent order.
-func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock) ([]string, error) {
+func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock, allowEmpty bool) ([]string, error) {
// first get each unique hostname
hostMap := make(map[string]struct{})
for _, sblockKey := range sb.Keys {
@@ -332,7 +334,7 @@ func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock) ([]stri
return nil, fmt.Errorf("parsing server block key: %v", err)
}
addr = addr.Normalize()
- if addr.Host == "" {
+ if addr.Host == "" && !allowEmpty {
// server block contains a key like ":443", i.e. the host portion
// is empty / catch-all, which means to match all hosts
return []string{}, nil
@@ -408,7 +410,7 @@ func (st *ServerType) serversFromPairings(
return nil, fmt.Errorf("server block %v: compiling matcher sets: %v", sblock.block.Keys, err)
}
- hosts, err := st.hostsFromServerBlockKeys(sblock.block)
+ hosts, err := st.hostsFromServerBlockKeys(sblock.block, false)
if err != nil {
return nil, err
}
@@ -488,14 +490,12 @@ func (st *ServerType) serversFromPairings(
LoggerNames: make(map[string]string),
}
}
- hosts, err := st.hostsFromServerBlockKeys(sblock.block)
+ hosts, err := st.hostsFromServerBlockKeys(sblock.block, true)
if err != nil {
return nil, err
}
for _, h := range hosts {
- if ncl.name != "" {
- srv.Logs.LoggerNames[h] = ncl.name
- }
+ srv.Logs.LoggerNames[h] = ncl.name
}
}
}
diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go
index 4f72a4a..3b3963f 100644
--- a/caddyconfig/httpcaddyfile/tlsapp.go
+++ b/caddyconfig/httpcaddyfile/tlsapp.go
@@ -82,7 +82,7 @@ func (st ServerType) buildTLSApp(
// get values that populate an automation policy for this block
var ap *caddytls.AutomationPolicy
- sblockHosts, err := st.hostsFromServerBlockKeys(sblock.block)
+ sblockHosts, err := st.hostsFromServerBlockKeys(sblock.block, false)
if err != nil {
return nil, warnings, err
}
diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go
index 461865c..c7780b0 100644
--- a/modules/caddyhttp/server.go
+++ b/modules/caddyhttp/server.go
@@ -172,7 +172,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := accLog
if s.Logs != nil && s.Logs.LoggerNames != nil {
- logger = logger.Named(s.Logs.LoggerNames[r.Host])
+ if loggerName, ok := s.Logs.LoggerNames[r.Host]; ok {
+ logger = logger.Named(loggerName)
+ } else {
+ // see if there's a default log name to attach to
+ logger = logger.Named(s.Logs.LoggerNames[""])
+ }
}
log := logger.Info