| name | cloudflare |
| description | Deploy and manage Cloudflare services including Workers, Pages, R2, D1, and KV. Configure DNS, CDN, security rules, and edge computing. Use for edge deployments, CDN, and Cloudflare infrastructure. |
Cloudflare Skill
Complete guide for managing Cloudflare services - DNS, Tunnels, Zero Trust, and more.
Quick Reference
Cloudflare Services
| Service | Purpose |
|---|
| DNS | Domain name resolution with proxy |
| CDN | Content delivery and caching |
| Tunnels | Expose local services securely |
| Zero Trust | Identity-based access control |
| WAF | Web application firewall |
| Workers | Serverless edge computing |
| Pages | Static site hosting |
CLI Installation
brew install cloudflared
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o cloudflared
chmod +x cloudflared
sudo mv cloudflared /usr/local/bin/
winget install Cloudflare.cloudflared
npm install -g wrangler
1. DNS Management
Add DNS Records
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" \
-H "Authorization: Bearer {api_token}" \
-H "Content-Type: application/json" \
--data '{
"type": "A",
"name": "www",
"content": "192.0.2.1",
"ttl": 1,
"proxied": true
}'
Common Record Types
Type: A
Name: www
Content: 192.0.2.1
Proxied: Yes
Type: AAAA
Name: www
Content: 2001:db8::1
Proxied: Yes
Type: CNAME
Name: blog
Content: www.example.com
Proxied: Yes
Type: MX
Name: @
Content: mail.example.com
Priority: 10
Proxied: No
Type: TXT
Name: @
Content: "v=spf1 include:_spf.google.com ~all"
Proxy Status
- Traffic goes through Cloudflare
- DDoS protection enabled
- CDN caching enabled
- SSL/TLS termination at edge
- Real IP hidden
- Direct connection to origin
- No Cloudflare protection
- Required for: MX, non-HTTP services
2. Cloudflare Tunnels
Create Tunnel
cloudflared tunnel login
cloudflared tunnel create my-tunnel
cloudflared tunnel list
cloudflared tunnel delete my-tunnel
Configure Tunnel
tunnel: <tunnel-id>
credentials-file: /root/.cloudflared/<tunnel-id>.json
ingress:
- hostname: app.example.com
service: http://localhost:8080
- hostname: ha.example.com
service: http://localhost:8123
originRequest:
noTLSVerify: true
- hostname: ssh.example.com
service: ssh://localhost:22
- service: http_status:404
Run Tunnel
cloudflared tunnel run my-tunnel
cloudflared tunnel --config ~/.cloudflared/config.yml run my-tunnel
sudo cloudflared service install
sudo systemctl start cloudflared
sudo systemctl enable cloudflared
Docker Tunnel
services:
cloudflared:
image: cloudflare/cloudflared:latest
container_name: cloudflared
restart: unless-stopped
command: tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}
environment:
- TUNNEL_TOKEN=your-tunnel-token
Quick Tunnel (Temporary)
cloudflared tunnel --url http://localhost:3000
3. Zero Trust / Access
Create Access Application
curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/access/apps" \
-H "Authorization: Bearer {api_token}" \
-H "Content-Type: application/json" \
--data '{
"name": "Internal App",
"domain": "app.example.com",
"type": "self_hosted",
"session_duration": "24h"
}'
Access Policies
Policy Name: Allowed Users
Decision: Allow
Include:
- Emails ending in: @company.com
Policy Name: Admin Group
Decision: Allow
Include:
- Access Groups: Administrators
Require:
- Country: United States
Policy Name: Contractors
Decision: Allow
Include:
- Emails: contractor@external.com
Authentication Method: One-time PIN
Service Tokens
curl -H "CF-Access-Client-Id: {client_id}" \
-H "CF-Access-Client-Secret: {client_secret}" \
https://app.example.com/api
WARP Client
- Require disk encryption
- Require firewall enabled
- Require specific OS version
4. WAF (Web Application Firewall)
Managed Rulesets
Rulesets:
- Cloudflare Managed Ruleset (OWASP)
- Cloudflare OWASP Core Ruleset
- Exposed Credentials Check
Custom Rules
(ip.geoip.country in {"CN" "RU" "KP"})
Action: Block
(http.request.uri.path contains "/api/")
Rate: 100 requests per minute
Action: Challenge
(cf.client.bot and not cf.verified_bot_category in {"Search Engine Crawler"})
Action: Block
(http.request.uri.path contains "/admin" and not ip.src in {192.168.1.0/24})
Action: Block
Firewall Rules
(not ip.src in {192.168.1.100 10.0.0.0/8})
Action: Block
(cf.threat_score gt 30)
Action: Managed Challenge
(http.request.uri.path eq "/health")
Action: Skip (all remaining rules)
5. Page Rules & Cache
Page Rules
URL: http://*example.com/*
Setting: Always Use HTTPS
URL: *example.com/static/*
Settings:
- Cache Level: Cache Everything
- Edge Cache TTL: 1 month
URL: *example.com/api/*
Settings:
- Cache Level: Bypass
URL: old.example.com/*
Setting: Forwarding URL (301)
Destination: https://new.example.com/$1
Cache Settings
Respect Existing Headers / Override with: 4 hours
2 hours (default) to 1 month
Mobile, Desktop, Tablet variations
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer {api_token}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
6. Workers
Create Worker
wrangler init my-worker
cd my-worker
wrangler login
wrangler deploy
Basic Worker
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === "/") {
return new Response("Hello from Cloudflare Workers!");
}
if (url.pathname.startsWith("/api/")) {
const apiUrl = "https://api.backend.com" + url.pathname;
return fetch(apiUrl, request);
}
return new Response("Not Found", { status: 404 });
},
};
Worker with KV Storage
export default {
async fetch(request, env) {
const value = await env.MY_KV.get("key");
await env.MY_KV.put("key", "value");
return new Response(value);
},
};
wrangler.toml
name = "my-worker"
main = "src/index.js"
compatibility_date = "2024-01-01"
[vars]
API_KEY = "secret"
[[kv_namespaces]]
binding = "MY_KV"
id = "your-kv-namespace-id"
[[r2_buckets]]
binding = "MY_BUCKET"
bucket_name = "my-bucket"
7. Pages (Static Sites)
Deploy Static Site
wrangler pages deploy ./dist
wrangler pages deploy ./dist --project-name my-site
Build Configuration
- Next.js
- Nuxt
- SvelteKit
- Astro
- Hugo
- Jekyll
- Gatsby
Build command: npm run build
Build output directory: dist
Root directory: /
Environment Variables
[env.production.vars]
API_URL = "https://api.example.com"
[env.preview.vars]
API_URL = "https://staging-api.example.com"
8. SSL/TLS
SSL Modes
Off: No encryption (not recommended)
Flexible: HTTPS to Cloudflare, HTTP to origin
Full: HTTPS end-to-end (self-signed OK)
Full (Strict): HTTPS end-to-end (valid cert required)
Origin Certificates
Edge Certificates
Universal SSL - covers *.example.com and example.com
- Custom hostnames
- Dedicated certificates
- Total TLS
9. API Usage
Authentication
curl -H "Authorization: Bearer {api_token}" \
"https://api.cloudflare.com/client/v4/user/tokens/verify"
curl -H "X-Auth-Email: {email}" \
-H "X-Auth-Key: {api_key}" \
"https://api.cloudflare.com/client/v4/user"
Common API Calls
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
-H "Authorization: Bearer {token}"
curl -X GET "https://api.cloudflare.com/client/v4/zones/{zone_id}" \
-H "Authorization: Bearer {token}"
curl -X GET "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" \
-H "Authorization: Bearer {token}"
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}" \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"www","content":"192.0.2.2","ttl":1,"proxied":true}'
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
10. Troubleshooting
Common Issues
DNS not propagating:
dig +short example.com @1.1.1.1
dig NS example.com
Tunnel not connecting:
cloudflared tunnel info my-tunnel
cloudflared tunnel --loglevel debug run my-tunnel
ls ~/.cloudflared/
cloudflared tunnel login
SSL errors:
- Ensure origin has valid SSL certificate
- Check SSL mode (try Full instead of Full Strict)
- Origin certificate expired or invalid
- Use Cloudflare Origin Certificate
- Ensure all resources use HTTPS
- Enable Automatic HTTPS Rewrites
5xx errors:
- Check origin server is running
- Verify origin responds on correct port
- Origin server not responding
- Check firewall allows Cloudflare IPs
- Origin server overloaded
- Check origin firewall
- Origin took too long (>100s)
- Optimize origin response time
Best Practices
- Use API tokens with minimal permissions (not global API key)
- Enable 2FA on Cloudflare account
- Use Full (Strict) SSL mode with valid origin certificates
- Whitelist Cloudflare IPs at origin firewall
- Enable Under Attack Mode during DDoS
- Use Page Rules sparingly (3 free, use Transform Rules instead)
- Monitor analytics for unusual traffic patterns
- Keep tunnels updated with automatic updates or regular manual updates
- Use Zero Trust for internal applications
- Cache static assets aggressively at edge