httpcaddyfile: Proper log config with catch-all blocks (fix #3878)

This commit is contained in:
Matthew Holt 2020-11-24 16:36:58 -07:00
parent 2d5498ee6f
commit 63afffc2e3
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 96 additions and 7 deletions

View file

@ -450,6 +450,15 @@ func (st *ServerType) serversFromPairings(
var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
autoHTTPSWillAddConnPolicy := autoHTTPS != "off" 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 // create a subroute for each site in the server block
for _, sblock := range p.serverBlocks { for _, sblock := range p.serverBlocks {
matcherSetsEnc, err := st.compileEncodedMatcherSets(sblock) matcherSetsEnc, err := st.compileEncodedMatcherSets(sblock)
@ -563,13 +572,13 @@ func (st *ServerType) serversFromPairings(
} else { } else {
// map each host to the user's desired logger name // map each host to the user's desired logger name
for _, h := range sblockLogHosts { for _, h := range sblockLogHosts {
// if the custom logger name is non-empty, add it to // if the custom logger name is non-empty, add it to the map;
// the map; otherwise, only map to an empty logger // otherwise, only map to an empty logger name if this or
// name if the server block has a catch-all host (in // another site block on this server has a catch-all host (in
// which case only requests with mapped hostnames will // which case only requests with mapped hostnames will be
// be access-logged, so it'll be necessary to add them // access-logged, so it'll be necessary to add them to the
// to the map even if they use default logger) // map even if they use default logger)
if ncl.name != "" || len(hosts) == 0 { if ncl.name != "" || catchAllSblockExists {
if srv.Logs.LoggerNames == nil { if srv.Logs.LoggerNames == nil {
srv.Logs.LoggerNames = make(map[string]string) srv.Logs.LoggerNames = make(map[string]string)
} }

View file

@ -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
}
}
}
}
}
}