11 KiB
CapRover / Cloudflare Deployment Guide
This guide explains how to deploy CodeSnippet with separate frontend and backend domains using CapRover and Cloudflare.
Architecture
┌─────────────────────────────────────────────┐
│ Cloudflare DNS & Proxy │
│ https://frontend.example.com │
│ https://backend.example.com │
└─────────────┬───────────────────────────────┘
│
│ HTTPS (Cloudflare SSL)
│
┌─────────────▼───────────────────────────────┐
│ CapRover Server │
│ │
│ ┌──────────────┐ ┌─────────────────┐ │
│ │ Frontend │ │ Backend │ │
│ │ (Nginx) │ │ (Flask) │ │
│ │ Port 3000 │ │ Port 5000 │ │
│ └──────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────┘
Prerequisites
- CapRover server installed and configured
- Domain name configured in Cloudflare
- DNS records pointing to your CapRover server
- CapRover CLI installed:
npm install -g caprover
Step 1: Configure DNS in Cloudflare
Add these DNS records in Cloudflare:
| Type | Name | Content | Proxy Status |
|---|---|---|---|
| A | frontend | YOUR_CAPROVER_IP | Proxied |
| A | backend | YOUR_CAPROVER_IP | Proxied |
Important: Enable "Proxied" (orange cloud) to use Cloudflare's CDN and SSL.
Step 2: Deploy Backend to CapRover
Create Backend App
- Login to CapRover dashboard
- Go to "Apps" → "One-Click Apps/Databases"
- Create a new app named
codesnippet-backend - Enable "Has Persistent Data" and set persistent directory to
/app/data
Configure Backend Environment Variables
In the backend app settings, add these environment variables:
CORS_ALLOWED_ORIGINS=https://frontend.example.com
DATABASE_PATH=/app/data/snippets.db
Deploy Backend Code
From the backend directory:
cd backend
caprover deploy -a codesnippet-backend
Enable HTTPS for Backend
- In CapRover dashboard → Apps → codesnippet-backend
- Go to "HTTP Settings"
- Enable "HTTPS"
- Connect domain:
backend.example.com - Enable "Force HTTPS by redirecting all HTTP traffic to HTTPS"
- Wait for SSL certificate to be issued
Step 3: Deploy Frontend to CapRover
Create Frontend App
- In CapRover dashboard, create new app:
codesnippet-frontend - No persistent data needed for frontend
Configure Frontend Environment Variables
In the frontend app settings, add:
VITE_FLASK_BACKEND_URL=https://backend.example.com
Deploy Frontend Code
From the project root:
caprover deploy -a codesnippet-frontend
Enable HTTPS for Frontend
- In CapRover dashboard → Apps → codesnippet-frontend
- Go to "HTTP Settings"
- Enable "HTTPS"
- Connect domain:
frontend.example.com - Enable "Force HTTPS by redirecting all HTTP traffic to HTTPS"
- Wait for SSL certificate to be issued
Step 4: Configure Cloudflare Settings
SSL/TLS Settings
- Go to Cloudflare dashboard → SSL/TLS
- Set encryption mode to "Full (strict)"
- Enable "Always Use HTTPS"
- Enable "Automatic HTTPS Rewrites"
Security Settings
- Go to Security → WAF
- Consider enabling Bot Fight Mode for backend
- Set up rate limiting rules if needed
Speed Settings
- Enable "Auto Minify" for JavaScript, CSS, HTML
- Enable "Brotli" compression
- Set Browser Cache TTL appropriately
Step 5: Verify Deployment
Test Backend
curl https://backend.example.com/health
Expected response:
{
"status": "healthy",
"timestamp": "2024-01-01T12:00:00.000000"
}
Test CORS
curl -H "Origin: https://frontend.example.com" \
-H "Access-Control-Request-Method: GET" \
-X OPTIONS \
https://backend.example.com/api/snippets
Should return CORS headers:
Access-Control-Allow-Origin: https://frontend.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Test Frontend
Visit https://frontend.example.com and verify:
- Frontend loads correctly
- Backend indicator shows "Backend" status
- Can create, edit, and delete snippets
- No CORS errors in browser console
Environment Variables Reference
Backend Environment Variables
| Variable | Description | Example |
|---|---|---|
CORS_ALLOWED_ORIGINS |
Comma-separated list of allowed frontend URLs | https://frontend.example.com |
DATABASE_PATH |
Path to SQLite database file | /app/data/snippets.db |
Note: Use * for CORS_ALLOWED_ORIGINS only in development. In production, always specify exact origins.
Frontend Environment Variables
| Variable | Description | Example |
|---|---|---|
VITE_FLASK_BACKEND_URL |
Backend API URL | https://backend.example.com |
Alternative: Single Domain Setup
If you prefer a single domain (e.g., https://app.example.com), you can deploy frontend with nginx proxying to backend:
Deploy Both Services
- Deploy backend as before (internal only, no custom domain)
- Frontend proxies
/apirequests to backend via nginx
Frontend Configuration
# Frontend environment variables
VITE_FLASK_BACKEND_URL=/api
Nginx Configuration
The included nginx.conf already handles this:
location /api {
proxy_pass http://backend:5000;
# ... proxy headers
}
Benefits
- Simplified DNS (single domain)
- No CORS issues (same-origin requests)
- Easier SSL management
Drawbacks
- Frontend and backend tightly coupled
- Can't independently scale services
- Single point of failure
Troubleshooting
CORS Errors
Problem: Browser console shows CORS errors
Solutions:
- Verify
CORS_ALLOWED_ORIGINSin backend matches frontend URL exactly - Ensure both domains use HTTPS (not mixed HTTP/HTTPS)
- Check Cloudflare proxy status is enabled for both domains
- Clear browser cache and hard refresh
Backend Connection Failed
Problem: Frontend shows "Connection failed" error
Solutions:
- Verify backend is running:
curl https://backend.example.com/health - Check CapRover logs for backend app
- Verify
VITE_FLASK_BACKEND_URLin frontend matches backend URL - Test from command line:
curl -v https://backend.example.com/api/snippets
SSL Certificate Issues
Problem: SSL certificate not issued or invalid
Solutions:
- Wait 5-10 minutes for Let's Encrypt to issue certificate
- Verify DNS records are correct and propagated
- Check CapRover can reach Let's Encrypt (port 80 open)
- Try disabling and re-enabling HTTPS in CapRover
Data Persistence Issues
Problem: Backend loses data after restart
Solutions:
- Verify "Has Persistent Data" is enabled in CapRover
- Check persistent directory path is
/app/data - Verify
DATABASE_PATHenvironment variable is correct - Check CapRover volume is properly mounted
Multiple Origins
Problem: Need to allow multiple frontend domains
Solution: Set comma-separated origins:
CORS_ALLOWED_ORIGINS=https://frontend.example.com,https://app.example.com,https://staging.example.com
Security Best Practices
Production Checklist
- HTTPS enabled for both frontend and backend
CORS_ALLOWED_ORIGINSset to specific domains (not*)- Cloudflare proxy enabled (orange cloud)
- Rate limiting configured in Cloudflare
- Backend database backed up regularly
- Environment variables stored securely (not in code)
- Debug mode disabled in Flask (
debug=False) - Cloudflare WAF rules configured
- HTTPS-only cookies enabled
- Security headers configured in nginx
Recommended Cloudflare Rules
- Rate Limiting: Limit API requests to 100 per minute per IP
- Bot Protection: Challenge or block known bad bots
- Geographic Restrictions: Block countries you don't serve (optional)
- DDoS Protection: Enable automatic DDoS mitigation
Monitoring
CapRover Monitoring
- Enable app metrics in CapRover dashboard
- Monitor CPU and memory usage
- Set up alerts for app crashes
- Review logs regularly for errors
Cloudflare Analytics
- Monitor traffic patterns
- Check for unusual spikes or attacks
- Review security events
- Analyze performance metrics
Backup Strategy
Automated Backups
Set up a cron job in CapRover to backup database:
# In backend app settings, add a schedule:
0 2 * * * tar -czf /app/data/backup-$(date +%Y%m%d).tar.gz /app/data/snippets.db
Manual Backup
- SSH into CapRover server
- Copy database file:
docker cp captain--codesnippet-backend:/app/data/snippets.db ./backup.db
Restore from Backup
- Copy backup to container:
docker cp ./backup.db captain--codesnippet-backend:/app/data/snippets.db - Restart backend app in CapRover
Scaling Considerations
Vertical Scaling
Increase resources in CapRover:
- Go to app settings → "Resources"
- Increase CPU and memory limits
- Restart app
Horizontal Scaling
For high traffic:
- Deploy multiple backend instances in CapRover
- Use CapRover's load balancing
- Consider shared database (PostgreSQL instead of SQLite)
- Use Redis for session management
Cost Optimization
Cloudflare
- Free plan includes SSL, CDN, and basic DDoS protection
- Pro plan ($20/mo) adds WAF and additional performance features
CapRover
- Single VPS can run both frontend and backend
- Recommended: 2 CPU / 4GB RAM minimum
- Estimated cost: $10-20/month (DigitalOcean, Linode, Vultr)
Support
For issues specific to:
- CapRover: https://caprover.com/docs/
- Cloudflare: https://support.cloudflare.com/
- CodeSnippet: Check the main README.md and BACKEND-CONFIG.md