@ -38,7 +38,7 @@ type ServerType struct {
}
}
// Setup makes a config from the tokens.
// Setup makes a config from the tokens.
func ( st ServerType ) Setup ( original ServerBlocks [ ] caddyfile . ServerBlock ,
func ( st ServerType ) Setup ( input ServerBlocks [ ] caddyfile . ServerBlock ,
options map [ string ] interface { } ) ( * caddy . Config , [ ] caddyconfig . Warning , error ) {
options map [ string ] interface { } ) ( * caddy . Config , [ ] caddyconfig . Warning , error ) {
var warnings [ ] caddyconfig . Warning
var warnings [ ] caddyconfig . Warning
gc := counter { new ( int ) }
gc := counter { new ( int ) }
@ -50,15 +50,15 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
// chosen to handle a request - we actually will make each
// chosen to handle a request - we actually will make each
// server block's route terminal so that only one will run
// server block's route terminal so that only one will run
sbKeys := make ( map [ string ] struct { } )
sbKeys := make ( map [ string ] struct { } )
s erverBlocks := make ( [ ] serverBlock , 0 , len ( original ServerBlocks) )
originalS erverBlocks := make ( [ ] serverBlock , 0 , len ( input ServerBlocks) )
for i , sblock := range original ServerBlocks {
for i , sblock := range input ServerBlocks {
for j , k := range sblock . Keys {
for j , k := range sblock . Keys {
if _ , ok := sbKeys [ k ] ; ok {
if _ , ok := sbKeys [ k ] ; ok {
return nil , warnings , fmt . Errorf ( "duplicate site address not allowed: '%s' in %v (site block %d, key %d)" , k , sblock . Keys , i , j )
return nil , warnings , fmt . Errorf ( "duplicate site address not allowed: '%s' in %v (site block %d, key %d)" , k , sblock . Keys , i , j )
}
}
sbKeys [ k ] = struct { } { }
sbKeys [ k ] = struct { } { }
}
}
s erverBlocks = append ( s erverBlocks, serverBlock {
originalS erverBlocks = append ( originalS erverBlocks, serverBlock {
block : sblock ,
block : sblock ,
pile : make ( map [ string ] [ ] ConfigValue ) ,
pile : make ( map [ string ] [ ] ConfigValue ) ,
} )
} )
@ -66,12 +66,12 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
// apply any global options
// apply any global options
var err error
var err error
s erverBlocks, err = st . evaluateGlobalOptionsBlock ( s erverBlocks, options )
originalS erverBlocks, err = st . evaluateGlobalOptionsBlock ( originalS erverBlocks, options )
if err != nil {
if err != nil {
return nil , warnings , err
return nil , warnings , err
}
}
for _ , sb := range s erverBlocks {
for _ , sb := range originalS erverBlocks {
// replace shorthand placeholders (which are
// replace shorthand placeholders (which are
// convenient when writing a Caddyfile) with
// convenient when writing a Caddyfile) with
// their actual placeholder identifiers or
// their actual placeholder identifiers or
@ -155,7 +155,7 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
}
}
// map
// map
sbmap , err := st . mapAddressToServerBlocks ( s erverBlocks, options )
sbmap , err := st . mapAddressToServerBlocks ( originalS erverBlocks, options )
if err != nil {
if err != nil {
return nil , warnings , err
return nil , warnings , err
}
}
@ -193,7 +193,8 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
// extract any custom logs, and enforce configured levels
// extract any custom logs, and enforce configured levels
var customLogs [ ] namedCustomLog
var customLogs [ ] namedCustomLog
var hasDefaultLog bool
var hasDefaultLog bool
for _ , sb := range serverBlocks {
for _ , p := range pairings {
for _ , sb := range p . serverBlocks {
for _ , clVal := range sb . pile [ "custom_log" ] {
for _ , clVal := range sb . pile [ "custom_log" ] {
ncl := clVal . Value . ( namedCustomLog )
ncl := clVal . Value . ( namedCustomLog )
if ncl . name == "" {
if ncl . name == "" {
@ -208,6 +209,8 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
customLogs = append ( customLogs , ncl )
customLogs = append ( customLogs , ncl )
}
}
}
}
}
if ! hasDefaultLog {
if ! hasDefaultLog {
// if the default log was not customized, ensure we
// if the default log was not customized, ensure we
// configure it with any applicable options
// configure it with any applicable options
@ -250,6 +253,17 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
if ncl . name != "" {
if ncl . name != "" {
cfg . Logging . Logs [ ncl . name ] = ncl . log
cfg . Logging . Logs [ ncl . name ] = ncl . log
}
}
// most users seem to prefer not writing access logs
// to the default log when they are directed to a
// file or have any other special customization
if len ( ncl . log . Include ) > 0 {
defaultLog , ok := cfg . Logging . Logs [ "default" ]
if ! ok {
defaultLog = new ( caddy . CustomLog )
cfg . Logging . Logs [ "default" ] = defaultLog
}
defaultLog . Exclude = append ( defaultLog . Exclude , ncl . log . Include ... )
}
}
}
}
}
@ -451,23 +465,46 @@ func (st *ServerType) serversFromPairings(
}
}
// add log associations
// add log associations
// see https://github.com/caddyserver/caddy/issues/3310
sblockLogHosts := sblock . hostsFromKeys ( true )
for _ , cval := range sblock . pile [ "custom_log" ] {
for _ , cval := range sblock . pile [ "custom_log" ] {
ncl := cval . Value . ( namedCustomLog )
ncl := cval . Value . ( namedCustomLog )
if srv . Logs == nil {
if srv . Logs == nil {
srv . Logs = & caddyhttp . ServerLogConfig {
srv . Logs = new ( caddyhttp . ServerLogConfig )
LoggerNames : make ( map [ string ] string ) ,
}
}
}
if sblock . hasHostCatchAllKey ( ) {
if sblock . hasHostCatchAllKey ( ) {
srv . Logs . LoggerName = ncl . name
// all requests for hosts not able to be listed should use
// this log because it's a catch-all-hosts server block
srv . Logs . DefaultLoggerName = ncl . name
} else {
} else {
for _ , h := range sblock . hostsFromKeys ( true ) {
// map each host to the user's desired logger name
if ncl . 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 srv . Logs . LoggerNames == nil {
srv . Logs . LoggerNames = make ( map [ string ] string )
}
srv . Logs . LoggerNames [ h ] = ncl . name
srv . Logs . LoggerNames [ h ] = ncl . name
}
}
}
}
}
}
}
}
if srv . Logs != nil && len ( sblock . pile [ "custom_log" ] ) == 0 {
// server has access logs enabled, but this server block does not
// enable access logs; therefore, all hosts of this server block
// should not be access-logged
if len ( hosts ) == 0 {
// if the server block has a catch-all-hosts key, then we should
// not log reqs to any host unless it appears in the map
srv . Logs . SkipUnmappedHosts = true
}
srv . Logs . SkipHosts = append ( srv . Logs . SkipHosts , sblockLogHosts ... )
}
}
}
// a server cannot (natively) serve both HTTP and HTTPS at the
// a server cannot (natively) serve both HTTP and HTTPS at the