How I Replaced Nginx Chaos with One Clean Caddyfile

How I Replaced Nginx Chaos with One Clean Caddyfile

Tired of Nginx config hell? Here's how I replaced multiple Nginx config files with one elegant Caddyfile and never looked back.

November 4, 2025 · 5 min read

Last week, I was deep in deployment hell. More than 10 apps. 10+ APIs. One overworked developer (me).

Every time I touched Nginx, something broke. Semicolons went missing, SSL expired, or ports collided. All I wanted was for report-api, report.app, and payments-demo.sksushil.info.np to just work.

Then I met Caddy — and my nights got quieter.

The Problem: Too Many Configs, Too Little Sanity

Each microservice meant another Nginx file. Another reload command. Another moment wondering, "Why isn't this proxying?"

And let's be honest: debugging Nginx inside Docker feels like fighting your own reflection.

I needed something that understood Docker — something clean, automatic, and smart.

The "Wait, That's It?" Moment

A friend told me:

Caddy is like Nginx, but with auto HTTPS and no pain.

I tried it. I wrote this one file called Caddyfile:

YAML
report-app.sksushil.info.np {
    reverse_proxy report-app:80
}
app1.sksushil.info.np {
    reverse_proxy app-1:80
}
app1-api.sksushil.info.np {
    reverse_proxy app1-api:8080
}
payments-demo.sksushil.info.np {
    reverse_proxy payments-app-demo:8080
}

One reload command later, everything was live. SSL worked. Routing worked. I didn't cry.

Note on SSL Conflicts (Cloudflare Users)

Sometimes Caddy's automatic HTTPS can conflict with Cloudflare Flexible SSL, causing redirect loops.

By default, Caddy tries to get a Let's Encrypt certificate. Even if your backend is HTTP, Caddy terminates HTTPS and proxies to your app. This can clash with Cloudflare's SSL and cause infinite redirects.

Solution: Explicitly serve plain HTTP in Caddy:

YAML
http://payments-demo.sksushil.info.np {
    reverse_proxy payments-app-demo:8080
}

This tells Caddy: "Do not get certificates — just serve HTTP." Now Cloudflare handles SSL entirely.

If you omit http:// or https://, Caddy defaults to HTTPS and tries to issue a certificate automatically.

SPAs in Caddy v2

For single-page apps (SPAs), use rewrite to handle client-side routing:

YAML
report-app.sksushil.info.np {
    reverse_proxy report-app:80

    # SPA fallback
    @notFound {
        not file
    }
    rewrite @notFound /index.html
}

If that doesn't work, try the explicit static file matcher approach:

YAML
report.com.np {
    reverse_proxy report-app:80

    @static {
        path *.js *.css *.png *.jpg *.svg *.ico *.json
    }
    @notStatic {
        not path *.js *.css *.png *.jpg *.svg *.ico *.json
    }
    handle @notStatic {
        rewrite * /index.html
    }
}

This ensures your SPA routes work correctly and fall back to index.html when a static file isn't found.

The Secret: Shared Docker Network

Caddy needs to see your app containers — like neighbors on the same street. That's what the shared Docker network does:

Terminal
docker network create shared-net

Now every app joins this same network and Caddy can reach them by container name.

Caddy Setup

/srv/caddy/docker-compose.yml

YAML
services:
  caddy:
    image: caddy:latest
    container_name: caddy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - shared-net

volumes:
  caddy_data:
  caddy_config:

networks:
  shared-net:
    external: true

Example App Service

/srv/report/docker-compose.yml

YAML
services:
  report-app:
    image: report-app:latest
    container_name: report-app
    expose:
      - "80"
    restart: always
    networks:
      - shared-net

networks:
  shared-net:
    external: true
Info: Notice the app uses expose instead of ports. The container is reachable only within the shared Docker network — Caddy is the only public entry point.

Caddyfile Reload + Format

Once deployed, reload your config anytime without downtime:

Terminal
docker exec -it caddy caddy reload --config /etc/caddy/Caddyfile

Caddy even formats its own config — just run:

Terminal
docker exec -it caddy caddy fmt --overwrite /etc/caddy/Caddyfile

The Ending I Wanted

Now my production stack runs three sites, two APIs, and one proxy — all behind a single elegant Caddyfile. No /etc/nginx/sites-enabled, no renewal scripts, no fragile reloads.

Just this:

Terminal
docker exec -it caddy caddy reload --config /etc/caddy/Caddyfile

…and a peaceful developer sipping coffee while SSL renews itself. ☕

CaddyDevOpsDockerNginxSelf-Hosted
Continues in the chat

Subscribe to Newsletter

Get notified about new articles