Caddy server does not write any log file

Hello,
I hope that you can help me. This is my first time using a caddy server. Unfortunately there are problems with the server’s communication. That’s why I wanted to have the communication via the caddy written to a log file.
Unfortunately the caddy doesn’t write a log.
Here is the contents of the caddy file.

(logs) {
               log {
                              output file C:/Caddy/caddy.log

                              format json

               }

}

http://*:8080 {
               handle /api/* {
                              reverse_proxy {
                                            to localhost:58154

                                            lb_policy least_conn

                              }

               }

               handle {
                              root * C:\Caddy\www

                              try_files {path} /index.html

                              file_server

               }

               header /index.html {
                              Cache-Control "no-cache, no-store, must-revalidate"

               }

               header / {
                              Cache-Control "max-age=604800, must-revalidate"

               }

               import logs

}

Can you tell me why the traffic is not logged via port 8080?

Greeting
Matthias

You’re using * as your host matcher here. This will only match a domain like localhost but not example.com because * only matches a single label (labels are parts between . in a domain).

Change this to simply :8080 { to match all hosts on that port.

It’s likely because the request didn’t actually reach that site block because of the above.

You didn’t show what kind of request you tried, so I have to make an assumption.

Hello francislavoie
Thank you for your help. I have now come a big step further. A log file is now being written.
I think I still have an error in my Caddy file.
When I access port 8080 locally I get a status 200 via Postman. But when the request comes from the VPN, I get an error status 400.
Do you have any idea where the error could come from?

Caddy File

(logs) {
	log {
		output file C:/Caddy/caddy.log
		format json
	}
}
:8080 {
	handle /api/* {
		reverse_proxy {
			to localhost:58154
			lb_policy least_conn
		}
	}
	handle {
		root * C:\Caddy\www
		try_files {path} /index.html
		file_server
	}
	header /index.html {
		Cache-Control "no-cache, no-store, must-revalidate"
	}
	header / {
		Cache-Control "max-age=604800, must-revalidate"
	}
	import logs
}

Log File:

{"level":"info","ts":1707639288.5554435,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"::1","remote_port":"55849","proto":"HTTP/1.1","method":"POST","host":"localhost:8080","uri":"/api/mdm/gwa/nli/v1/import/import-measured-values","headers":{"User-Agent":["PostmanRuntime/7.31.0"],"Content-Length":["700"],"Accept-Encoding":["gzip, deflate, br"],"Accept":["*/*"],"Connection":["keep-alive"],"Content-Type":["application/json"],"Postman-Token":["83847dd9-37bb-432d-9ab6-e3b45861d4de"],"Authorization":[]}},"user_id":"","duration":0.232998,"size":0,"status":200,"resp_headers":{"Server":["Caddy","Microsoft-HTTPAPI/2.0"],"Content-Length":["0"],"Date":["Sun, 11 Feb 2024 08:14:48 GMT"]}}
{"level":"info","ts":1707639459.5919504,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"::1","remote_port":"55849","proto":"HTTP/1.1","method":"POST","host":"localhost:8080","uri":"/api/mdm/gwa/nli/v1/import/import-measured-values","headers":{"Accept":["*/*"],"User-Agent":["PostmanRuntime/7.31.0"],"Accept-Encoding":["gzip, deflate, br"],"Content-Length":["700"],"Authorization":[],"Postman-Token":["5049fd24-0518-4366-b5c7-a2299c295046"],"Connection":["keep-alive"],"Content-Type":["application/json"]}},"user_id":"","duration":0.2470007,"size":0,"status":200,"resp_headers":{"Server":["Caddy","Microsoft-HTTPAPI/2.0"],"Content-Length":["0"],"Date":["Sun, 11 Feb 2024 08:17:39 GMT"]}}
{"level":"info","ts":1707639574.2269154,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"::1","remote_port":"56023","proto":"HTTP/1.1","method":"GET","host":"localhost:8080","uri":"/","headers":{"Dnt":["1"],"Upgrade-Insecure-Requests":["1"],"Sec-Fetch-Site":["none"],"Connection":["keep-alive"],"Sec-Ch-Ua":["\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\""],"Sec-Ch-Ua-Mobile":["?0"],"Sec-Ch-Ua-Platform":["\"Windows\""],"Sec-Fetch-Dest":["document"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-User":["?1"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Accept-Language":["de,de-DE;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0"]}},"user_id":"","duration":0.0279995,"size":19,"status":200,"resp_headers":{"Etag":["\"rqqpx7j\""],"Content-Type":["text/html; charset=utf-8"],"Last-Modified":["Mon, 27 Feb 2023 13:10:19 GMT"],"Accept-Ranges":["bytes"],"Content-Length":["19"],"Server":["Caddy"],"Cache-Control":["max-age=604800, must-revalidate"]}}
{"level":"error","ts":1707640226.5078242,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"10.6.31.11","remote_port":"44434","proto":"HTTP/1.1","method":"POST","host":"10.255.100.29:8080","uri":"/api/mdm/gwa/nli/v1/import/import-measured-values","headers":{"Connection":["Keep-Alive"],"User-Agent":["okhttp/3.9.1"],"Accept-Encoding":["gzip"],"Content-Length":["554"],"Content-Type":["application/json; charset=utf-8"],"Authorization":[]}},"user_id":"","duration":0.0009975,"size":334,"status":400,"resp_headers":{"Server":["Caddy","Microsoft-HTTPAPI/2.0"],"Content-Length":["334"],"Content-Type":["text/html; charset=us-ascii"],"Date":["Sun, 11 Feb 2024 08:30:26 GMT"]}}
{"level":"error","ts":1707644629.3952718,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"10.6.31.11","remote_port":"46334","proto":"HTTP/1.1","method":"POST","host":"10.255.100.29:8080","uri":"/api/mdm/gwa/nli/v1/import/import-measured-values","headers":{"Authorization":[],"Accept-Encoding":["gzip"],"Content-Length":["554"],"Content-Type":["application/json; charset=utf-8"],"Connection":["Keep-Alive"],"User-Agent":["okhttp/3.9.1"]}},"user_id":"","duration":0.0156289,"size":334,"status":400,"resp_headers":{"Server":["Caddy","Microsoft-HTTPAPI/2.0"],"Date":["Sun, 11 Feb 2024 09:43:49 GMT"],"Content-Length":["334"],"Content-Type":["text/html; charset=us-ascii"]}}
{"level":"error","ts":1707645627.431538,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"10.6.31.11","remote_port":"46755","proto":"HTTP/1.1","method":"POST","host":"10.255.100.29:8080","uri":"/api/mdm/gwa/nli/v1/import/import-measured-values","headers":{"Authorization":[],"User-Agent":["okhttp/3.9.1"],"Content-Type":["application/json; charset=utf-8"],"Accept-Encoding":["gzip"],"Content-Length":["554"],"Connection":["Keep-Alive"]}},"user_id":"","duration":0.0012315,"size":334,"status":400,"resp_headers":{"Date":["Sun, 11 Feb 2024 10:00:27 GMT"],"Content-Type":["text/html; charset=us-ascii"],"Content-Length":["334"],"Server":["Caddy","Microsoft-HTTPAPI/2.0"]}}
{"level":"error","ts":1707645627.666502,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"10.6.31.11","remote_port":"46756","proto":"HTTP/1.1","method":"POST","host":"10.255.100.29:8080","uri":"/api/mdm/gwa/nli/v1/import/import-measured-values","headers":{"Content-Length":["554"],"Content-Type":["application/json; charset=utf-8"],"Accept-Encoding":["gzip"],"User-Agent":["okhttp/3.9.1"],"Authorization":[],"Connection":["Keep-Alive"]}},"user_id":"","duration":0.0009967,"size":334,"status":400,"resp_headers":{"Server":["Caddy","Microsoft-HTTPAPI/2.0"],"Content-Length":["334"],"Content-Type":["text/html; charset=us-ascii"],"Date":["Sun, 11 Feb 2024 10:00:27 GMT"]}}

greeting
Matthias

The 400 status is coming from your upstream app. Caddy is not producing the error.

That’s not something I can help you with, you need to figure out why your upstream app is returning an error.

I found the mistake. It was due to the caddy configuration. I had to enter the IP of the server instead of localhost.
Old:
:8080 {
handle /api/* {
reverse_proxy {
to localhost:58154

New:
:8080 {
handle /api/* {
reverse_proxy {
to 192.168.1.20:58154

Thanks for your help
Matthias

1 Like

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