Skip to main content
This tutorial walks you through creating a complete Caddy HTTP handler plugin that adds custom headers and logs request information.

What We’re Building

We’ll create a plugin called requestinfo that:
  • Adds custom headers to HTTP responses
  • Logs request details (method, path, client IP)
  • Supports Caddyfile configuration
  • Implements the full module lifecycle

Prerequisites

  • Go 1.25.0 or newer
  • Basic understanding of Go and HTTP
  • Familiarity with Caddy configuration

Step 1: Create the Module Structure

Create a new directory for your plugin:
Create the main file requestinfo.go:

Step 2: Define the Module Structure

Based on the pattern from modules/caddyhttp/staticresp.go:88, define your handler:

Step 3: Implement the Module Interface

Implement CaddyModule() from modules.go:54:
The module ID http.handlers.requestinfo places this in the HTTP handlers namespace.

Step 4: Implement the Lifecycle Methods

Provision

Implement Provision() based on modules/caddyhttp/tracing/module.go:48:

Validate

Step 5: Implement the HTTP Handler

Implement ServeHTTP() to handle requests:

Step 6: Add Caddyfile Support

Implement UnmarshalCaddyfile() based on modules/caddyhttp/staticresp.go:141:

Step 7: Register the Module

Add registration in the init() function from modules/caddyhttp/tracing/module.go:15:

Step 8: Add Interface Guards

Add compile-time interface checks:

Step 9: Build with xcaddy

Install xcaddy:
Build Caddy with your plugin:
For local development, use --with github.com/yourusername/caddy-requestinfo=./caddy-requestinfo

Step 10: Test Your Plugin

JSON Configuration

Create caddy.json:

Caddyfile Configuration

Create a Caddyfile:

Run and Test

You should see:
  • The custom header in the response
  • Request logs in the Caddy output (if log_enabled is true)

Complete Code

Next Steps

Module Development

Deep dive into module architecture

Testing Guide

Learn how to test your plugins

xcaddy Tool

Master the xcaddy build tool

Contributing

Share your plugin with the community