Agent Skillsnixopus/nixopus › caddyfile-generation

caddyfile-generation

GitHub

生成 Caddyfile 配置,支持静态站点、SPA 路由回退、缓存压缩、重定向及安全头设置,适用于前端部署与反向代理场景。

api/skills/caddyfile-generation/SKILL.md nixopus/nixopus

Trigger Scenarios

需要为静态网站生成 Caddy 配置 需要 SPA 应用的路由回退规则 需要配置自定义缓存或压缩策略

Install

npx skills add nixopus/nixopus --skill caddyfile-generation -g -y
More Options

Non-standard path

npx skills add https://github.com/nixopus/nixopus/tree/master/api/skills/caddyfile-generation -g -y

Use without installing

npx skills use nixopus/nixopus@caddyfile-generation

指定 Agent (Claude Code)

npx skills add nixopus/nixopus --skill caddyfile-generation -a claude-code -g -y

安装 repo 全部 skill

npx skills add nixopus/nixopus --all -g -y

预览 repo 内 skill

npx skills add nixopus/nixopus --list

SKILL.md

Frontmatter
{
    "name": "caddyfile-generation",
    "metadata": {
        "version": "1.0"
    },
    "description": "Generate Caddyfile configurations for static sites and reverse proxies — SPA fallback routing, cache headers, compression, redirects, and error pages. Use when deploying a static site that needs custom Caddy configuration, or when the user needs SPA routing, caching, or redirect rules."
}

Caddyfile Generation

When to Generate

  • Static sites served by Caddy (from static-deploy or frontend builds)
  • SPA applications that need fallback routing (React, Vue, Angular, SvelteKit static)
  • Sites needing custom cache headers, compression, or redirects

Base Template

Minimal Caddyfile for a static site:

:80 {
	root * /srv
	file_server
}

SPA Fallback Routing

Single-page apps need all non-file routes to return index.html:

:80 {
	root * /srv
	try_files {path} /index.html
	file_server
}

Frameworks that need this: React (CRA, Vite), Vue, Angular, SvelteKit (static adapter), Remix (SPA mode).

Frameworks that do NOT need this: Next.js (has its own server), Nuxt (SSR), Astro (generates individual HTML files for each route).

Cache Headers for Hashed Assets

Frontend build tools (Vite, Webpack) produce files with content hashes (e.g. main.a1b2c3.js). These can be cached aggressively:

:80 {
	root * /srv

	@hashed path_regexp hashed \.(js|css|woff2?|ttf|eot|svg|png|jpg|jpeg|gif|ico|webp)$
	header @hashed Cache-Control "public, max-age=31536000, immutable"

	@html path *.html /
	header @html Cache-Control "no-cache, no-store, must-revalidate"

	try_files {path} /index.html
	file_server
}

HTML files must NOT be cached (they reference the hashed assets).

Compression

Enable gzip and zstd compression:

:80 {
	root * /srv
	encode zstd gzip
	try_files {path} /index.html
	file_server
}

Redirects

www to non-www

www.example.com {
	redir https://example.com{uri} permanent
}

example.com {
	root * /srv
	file_server
}

HTTP to HTTPS

Caddy handles this automatically when using domain names. Only needed for explicit port-based configs:

:80 {
	redir https://{host}{uri} permanent
}

Note: In Nixopus deployments, the proxy layer already handles TLS termination. Do NOT add HTTPS redirects in the app's Caddyfile — the proxy handles this.

Custom Error Pages

:80 {
	root * /srv

	handle_errors {
		@404 expression {err.status_code} == 404
		rewrite @404 /404.html
		file_server
	}

	try_files {path} /index.html
	file_server
}

Reverse Proxy (API backend)

When a static frontend needs to proxy API requests to a backend:

:80 {
	root * /srv

	handle /api/* {
		reverse_proxy backend:8080
	}

	try_files {path} /index.html
	file_server
}

Security Headers

:80 {
	root * /srv

	header {
		X-Content-Type-Options "nosniff"
		X-Frame-Options "DENY"
		Referrer-Policy "strict-origin-when-cross-origin"
		-Server
	}

	file_server
}

Complete Production Template

Combines SPA routing, caching, compression, and security headers:

:80 {
	root * /srv
	encode zstd gzip

	header {
		X-Content-Type-Options "nosniff"
		X-Frame-Options "DENY"
		Referrer-Policy "strict-origin-when-cross-origin"
		-Server
	}

	@hashed path_regexp hashed \.(js|css|woff2?|ttf|eot|svg|png|jpg|jpeg|gif|ico|webp)$
	header @hashed Cache-Control "public, max-age=31536000, immutable"

	@html path *.html /
	header @html Cache-Control "no-cache, no-store, must-revalidate"

	try_files {path} /index.html
	file_server
}

Generation Logic

  1. Determine if the app is a SPA (needs try_files fallback) or multi-page (doesn't)
  2. Start with the base template
  3. Add try_files {path} /index.html if SPA
  4. Add encode zstd gzip for compression
  5. Add hashed asset cache headers if the build tool produces hashed filenames
  6. Add security headers
  7. Write to Caddyfile at the project root

Gotchas

  • Caddy listens on :80 inside Docker — the proxy layer handles external port mapping and TLS
  • try_files must come BEFORE file_server in the Caddyfile
  • root * /srv must match the COPY destination in the Dockerfile
  • Do NOT configure HTTPS/TLS in the Caddyfile for Nixopus deployments — the edge proxy handles TLS termination
  • Caddy's file_server serves directory listings by default — add file_server browse only if intentional

Related Skills

  • static-deploy — Dockerfile patterns that use Caddy as the web server
  • dockerfile-generation — Generate the Caddyfile alongside the Dockerfile

Version History

  • cf05d97 Current 2026-08-20 14:51

Same Skill Collection

api/skills/api-catalog/SKILL.md
api/skills/compose-setup/SKILL.md
api/skills/container-resource-tuning/SKILL.md
api/skills/cpp-deploy/SKILL.md
api/skills/database-migration/SKILL.md
api/skills/deno-deploy/SKILL.md
api/skills/deploy-delegation/SKILL.md
api/skills/deploy-flow/SKILL.md
api/skills/deployment-analysis/SKILL.md
api/skills/diagnostic-workflow/SKILL.md
api/skills/dockerfile-generation/SKILL.md
api/skills/dockerignore-generation/SKILL.md
api/skills/domain-attachment/SKILL.md
api/skills/domain-tls-routing/SKILL.md
api/skills/dotnet-deploy/SKILL.md
api/skills/elixir-deploy/SKILL.md
api/skills/failure-diagnosis/SKILL.md
api/skills/github-onboarding/SKILL.md
api/skills/github-workflow/SKILL.md
api/skills/gleam-deploy/SKILL.md
api/skills/go-deploy/SKILL.md
api/skills/incident-response/SKILL.md
api/skills/java-deploy/SKILL.md
api/skills/machine-ops/SKILL.md
api/skills/mcp-integrations/SKILL.md
api/skills/monorepo-strategy/SKILL.md
api/skills/nixopus-docs/SKILL.md
api/skills/node-deploy/SKILL.md
api/skills/onboarding/SKILL.md
api/skills/php-deploy/SKILL.md
api/skills/post-deploy-verification/SKILL.md
api/skills/pre-deploy-checklist/SKILL.md
api/skills/python-deploy/SKILL.md
api/skills/rollback-strategy/SKILL.md
api/skills/ruby-deploy/SKILL.md
api/skills/rust-deploy/SKILL.md
api/skills/self-heal/SKILL.md
api/skills/shell-deploy/SKILL.md
api/skills/static-deploy/SKILL.md

Metadata

Files
0
Version
cf05d97
Hash
98104418
Indexed
2026-08-20 14:51

- 위키
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-30 04:06
浙ICP备14020137号-1 $방문자$