Logging per subdomain reverse proxies

1. The problem I’m having:

I’d like to have log files for each subdomain that is proxied. I haven’t figured out how to do this. I want the Caddy log file for the Caddy-only stuff, then subdomain logs for the subdomain-specific stuff.

I’ve tried using a logging function that creates a log file based on a passed in filename, and I tried a log statement in the server block, but neither creates any specific log file.

2. Error messages and/or full log output:

na

3. Caddy version:

2.6.4

4. How I installed and ran Caddy:

a. System environment:

Win10 Pro x64

b. Command:

caddy run

d. My complete Caddy config:

{
	email myemail@email.net
	default_sni justinsdomain.com
	servers :8443 {
		protocols h2 h1
	}
	#log stdout
	log caddy-log {
		output file caddy.log	
	}
}
(headers) {
	header {
		Strict-Transport-Security "max-age=31536000; includeSubDomains;"
    		X-Xss-Protection "1; mode=block"
    		X-Content-Type-Options "nosniff"
    		X-Frame-Options "SAMEORIGIN"
    		Content-Security-Policy "upgrade-insecure-requests"
    		Referrer-Policy "strict-origin-when-cross-origin"
    		Cache-Control "public, max-age=15, must-revalidate"
	}
}
(logging) {
	log {
		output file "{args.0}.log"
	}
}
#file server 1
serv1.justinsdomain.com:8443 {
	log {
		output file caddy-Serv1.log
	}
	reverse_proxy :8080
	encode zstd gzip
	import headers
}
#file server 2
serv2.justinsdomain.com:8443 {
	import logging Serv2
	reverse_proxy :8081
	encode zstd gzip
	import headers
}

You didn’t specify a path to the file, so Caddy would try to write it to the current working directory. Where that is depends on how you started Caddy.

Try using a full path, and not just a filename.

1 Like

I actually want the log files in the caddy.exe folder (where the current default log is written). I tried the below and it still won’t work.

I changed the logging snippet:

log {
		output file "{args.0}.log"
	}

to

log {
		output file "d:\\BIN\\caddy\\{args.0}.log"
	}

and likewise for the server block log directive example, but all the logging still goes into the default log file.

What do you mean by “all the logging”?

The log directive only configures access logs, not all general server logs.

1 Like

Maybe I should ask this way - is logging per subdomain (server) possible? Caddy doesn’t complain about the config file. Is my config valid? All I know is it doesn’t produce anything new or different than w/o the server block directives.

Yes, absolutely.

The Caddy process probably doesn’t have permission to write to that location. I can’t really help with that part, permissions on Windows is not something I have much experience with.

Try without double backslashes, maybe.

1 Like

Write permissions is not the problem. (If Caddy couldn’t write, I’d hope it would show some kind of error.)

I had to escape the backslash because Caddy would error if I didn’t. Even Caddy knows to do it:
redirected default logger {"from": "stderr", "to": "D:\\BIN\\caddy\\caddy.log"}

Then it sounds like something is broken, at least on Windows. Or maybe my expectations are incorrect. What would be logged in the per server log file?

Only access logs, i.e. the shape of each request that your site receives. See How Logging Works — Caddy Documentation

I’m on vacation but I can test things out on my Windows machine

1 Like

I see in another post you said " {args.0} is deprecated, you should use {args[0]}". I’ll update for that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.