diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-11-24 16:36:58 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-11-24 16:36:58 -0700 |
commit | 63afffc2e317579a5bc618d5441ac238127114a0 (patch) | |
tree | 984ba3d0a53be656913dfb7fe599aba8e1adf694 | |
parent | 2d5498ee6f298703b5934a8f8889fa02778d08aa (diff) |
httpcaddyfile: Proper log config with catch-all blocks (fix #3878)
-rw-r--r-- | caddyconfig/httpcaddyfile/httptype.go | 23 | ||||
-rw-r--r-- | caddytest/integration/caddyfile_adapt/log_except_catchall_blocks.txt | 80 |
2 files changed, 96 insertions, 7 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index e4e40b2..406d8b9 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -450,6 +450,15 @@ func (st *ServerType) serversFromPairings( var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool autoHTTPSWillAddConnPolicy := autoHTTPS != "off" + // if a catch-all server block (one which accepts all hostnames) exists in this pairing, + // we need to know that so that we can configure logs properly (see #3878) + var catchAllSblockExists bool + for _, sblock := range p.serverBlocks { + if len(sblock.hostsFromKeys(false)) == 0 { + catchAllSblockExists = true + } + } + // create a subroute for each site in the server block for _, sblock := range p.serverBlocks { matcherSetsEnc, err := st.compileEncodedMatcherSets(sblock) @@ -563,13 +572,13 @@ func (st *ServerType) serversFromPairings( } else { // map each host to the user's desired logger name for _, h := range sblockLogHosts { - // if the custom logger name is non-empty, add it to - // the map; otherwise, only map to an empty logger - // name if the server block has a catch-all host (in - // which case only requests with mapped hostnames will - // be access-logged, so it'll be necessary to add them - // to the map even if they use default logger) - if ncl.name != "" || len(hosts) == 0 { + // if the custom logger name is non-empty, add it to the map; + // otherwise, only map to an empty logger name if this or + // another site block on this server has a catch-all host (in + // which case only requests with mapped hostnames will be + // access-logged, so it'll be necessary to add them to the + // map even if they use default logger) + if ncl.name != "" || catchAllSblockExists { if srv.Logs.LoggerNames == nil { srv.Logs.LoggerNames = make(map[string]string) } diff --git a/caddytest/integration/caddyfile_adapt/log_except_catchall_blocks.txt b/caddytest/integration/caddyfile_adapt/log_except_catchall_blocks.txt new file mode 100644 index 0000000..a6fd051 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/log_except_catchall_blocks.txt @@ -0,0 +1,80 @@ +http://localhost:2020 { + log + file_server +} + +:2020 { + respond 418 +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":2020" + ], + "routes": [ + { + "match": [ + { + "host": [ + "localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "file_server", + "hide": [ + "./Caddyfile" + ] + } + ] + } + ] + } + ], + "terminal": true + }, + { + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "static_response", + "status_code": 418 + } + ] + } + ] + } + ], + "terminal": true + } + ], + "automatic_https": { + "skip": [ + "localhost" + ] + }, + "logs": { + "logger_names": { + "localhost:2020": "" + }, + "skip_unmapped_hosts": true + } + } + } + } + } +}
\ No newline at end of file |