Why does Caddyfile require such strict indentation

“As the title suggests, I feel that the indentation requirements of Caddyfile are too strict. I just wrote a configuration for the first time using Caddyfile, but it kept failing. After checking the configuration file multiple times, I found that one indentation was incorrect, and I missed one indentation. Will this be resolved in the future, or is it just a requirement of Caddyfile?”

It doesn’t fail for improper indentation. Can you share your Caddyfile before the supposed fix? The one that’s supposedly invalid

2 Likes

sure. let me show u bro.

This is the wrong configuration of caddy validate

# This is the wrong configuration of caddy validate
# Caddyfile

# 域名/泛域名
1Example.com, *.Example.com {
	# # 日志
	log {
		# 记录到指定文件中
        output file /root/caddy2/Example.log {
		# 使用 JSON 格式
        format json
		# 滚动日志大小
		roll_size 100MiB
		# 保留最近3个备份日志
		roll_keep 3
		# 日志备份文件保留7天
		roll_keep_for 7d
    	}
	}
	# # 安全和TLS
	# 启用HTTPS
	tls {											
		# 启用HTTP2 HTTP3
		protocols h2 h3
		# 使用cloudflare DNS API申请域名证书
		dns cloudflare {env.CF_API_TOKEN}
	}

	# # 根目录和文件服务器
	# root /var/www/example.com
	# respond "Hello, world!"
	templates
	file_server browse

	# # 下面是关于一些优化的设置	
	# 启用gzip zstd压缩减少传输数据的大小,缺点增加CPU负载
	encode gzip	zstd
	
	# # 头部 所有路径 /
	header / {

	# 强制HSTS一年,包括子域名
	Strict-Transport-Security max-age=31536000; includeSubdomains

	# 禁止进行 MIME 类型猜测
	X-Content-Type-Options nosniff

	# 点击劫持保护,只允许加载同源资源
	Content-Security-Policy default-src

	# 阻止XSS攻击注入
	X-XSS-Protection 1; mode=block

	# 来源设置
	Referrer-Policy strict-origin-when-cross-origin
}

}

This is the configuration after caddy fmt fix

# This is the configuration after caddy fmt fix
# Caddy 2 配置文件
# Caddyfile

# 域名/泛域名
1Example.com, *.1Example.com {
	# # 日志
	log {
		# 记录到指定文件中
        output file /root/caddy2/Example.log {
			# 使用 JSON 格式
			format json
			# 滚动日志大小
			roll_size 100MiB
			# 保留最近3个备份日志
			roll_keep 3
			# 日志备份文件保留7天
			roll_keep_for 7d
		}
	}
	# # 安全和TLS
	# 启用HTTPS
	tls {
		# 启用HTTP2 HTTP3
		protocols h2 h3
		# 使用cloudflare DNS API申请域名证书
		dns cloudflare {env.CF_API_TOKEN}
	}

	# # 根目录和文件服务器
	# root /var/www/example.com
	# respond "Hello, world!"
	templates
	file_server browse

	# # 下面是关于一些优化的设置	
	# 启用gzip zstd压缩减少传输数据的大小,缺点增加CPU负载
	encode gzip zstd

	# # 头部 所有路径 /
	header / {
		# 强制HSTS一年,包括子域名
		Strict-Transport-Security max-age=31536000; includeSubdomains

		# 禁止进行 MIME 类型猜测
		X-Content-Type-Options nosniff

		# 点击劫持保护,只允许加载同源资源
		Content-Security-Policy default-src

		# 阻止XSS攻击注入
		X-XSS-Protection 1; mode=block

		# 来源设置
		Referrer-Policy strict-origin-when-cross-origin
	}
}

Check correctness with caddy validate

2023/03/27 21:15:04.933        INFO     using provided configuration    {"config_file": "/root/caddy2/Caddyfile", "config_adapter": ""}
2023/03/27 21:15:04.936 WARN     Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies    {"adapter": "caddyfile", "file": "/root/caddy2/Caddyfile", "line": 9}
2023/03/27 21:15:04.937 INFO     http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2023/03/27 21:15:04.938 INFO     http    enabling automatic HTTP->HTTPS redirects        {"server_name": "srv0"}
2023/03/27 21:15:04.937 INFO     tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc0002ab8f0"}
2023/03/27 21:15:04.938 INFO     tls.cache.maintenance   stopped background certificate maintenance      {"cache": "0xc0002ab8f0"}
Valid configuration

Apparently, the error message says there is a misconfiguration on line 9.
when i use caddy fmt Caddyfile --overwrite
That’s what the configuration looks like after the fmt fix above, and you’ll see that the indentation on line 9 has been fixed, but nothing else has changed.

That’s not an error. It’s a warning. The same file will work regardless. It even tells you “Valid configuration”.

2 Likes

I also encountered an error with the same configuration file before.

caddy validate --config /root/caddy2/Caddyfile

2023/03/27 21:15:54.138        INFO     using provided configuration    {"config_file": "/root/caddy2/Caddyfile", "config_adapter": ""}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /root/caddy2/Caddyfile:24 - Error during parsing: Wrong protocol name or protocol not supported: 'h2'

how i solved it

1# Comment out the ‘protocols h2 h3’ in the ‘tls’ parameters.
2# Remove an indentation at the beginning of line 9 in the configuration file.

Please note that either of the two methods mentioned above can be used to fix this error.
Is this my issue or a problem with Caddy 2?
I don’t think it’s an issue with my configuration file. I habitually check if the configuration file is correct before starting the service, but the problem still occurred due to the indentation on line 9. Although the service can be started normally, the warning or error still appears when using the ‘caddy validate’ or ‘caddy adapt’ command to check the file Checking the file will result in this warning or error

At this point, I will have to ask you to fill out the help template because the feedback isn’t consistent with the reported messages.

The protocols is not part of the tls option. See here:

And about this:

As I said earlier, it is not an error. The file runs fine. Please fill the help template with full details of how you’re running Caddy to understand why is this a blocker for you.

2 Likes

Let’s go back to the title. Whether the ‘protocols h2 h3’ parameter exists or is removed, checking the file still prompts an indentation problem on line 9

Warnings are not errors.

Yes, Caddy emits a warning if your Caddyfile is not formatted according to caddy fmt, which expects tabs instead of spaces, etc.

But a warning is just that, a warning. It’s just a message to tell you “maybe you should run caddy fmt”. But it’s not a requirement. You can ignore warnings if you don’t feel the need to fix them.

1 Like

ok thanks.
It seems that I can ignore it. Maybe I just had the urge to solve it when I saw the warning message. lol
@francislavoie @Mohammed90

:face_with_peeking_eye:

The problem is that you have spaces instead of tabs on this line. That’s a simple fix.

Or you can just run caddy fmt --overwrite to have it fix your config automatically.

3 Likes

This topic was automatically closed after 90 days. New replies are no longer allowed.