|
1 | | -# Replace with your domain |
| 1 | +# API Subdomain - handles tracking and API routes |
| 2 | +{$API_DOMAIN:api.localhost} { |
| 3 | + # Logging |
| 4 | + log { |
| 5 | + output file /var/log/caddy/api-access.log |
| 6 | + level INFO |
| 7 | + } |
| 8 | + |
| 9 | + # Security headers |
| 10 | + header { |
| 11 | + # Enable HSTS |
| 12 | + Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" |
| 13 | + # Prevent XSS attacks |
| 14 | + X-XSS-Protection "1; mode=block" |
| 15 | + # Prevent MIME type sniffing |
| 16 | + X-Content-Type-Options "nosniff" |
| 17 | + # Referrer policy |
| 18 | + Referrer-Policy "strict-origin-when-cross-origin" |
| 19 | + # Remove Server header |
| 20 | + -Server |
| 21 | + } |
| 22 | + |
| 23 | + # CORS headers for API subdomain |
| 24 | + @options { |
| 25 | + method OPTIONS |
| 26 | + } |
| 27 | + handle @options { |
| 28 | + header { |
| 29 | + Access-Control-Allow-Origin "*" |
| 30 | + Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS, PATCH" |
| 31 | + Access-Control-Allow-Headers "Accept, Authorization, Content-Type, X-Api-Key" |
| 32 | + Access-Control-Max-Age "3600" |
| 33 | + } |
| 34 | + respond 204 |
| 35 | + } |
| 36 | + |
| 37 | + # Health check endpoint |
| 38 | + handle /health { |
| 39 | + reverse_proxy app:4000 { |
| 40 | + header_up Host {host} |
| 41 | + header_up X-Real-IP {remote_host} |
| 42 | + header_up X-Forwarded-For {remote_host} |
| 43 | + header_up X-Forwarded-Proto {scheme} |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + # Track endpoint - public API |
| 48 | + handle /track { |
| 49 | + reverse_proxy app:4000 { |
| 50 | + header_up Host {host} |
| 51 | + header_up X-Real-IP {remote_host} |
| 52 | + header_up X-Forwarded-For {remote_host} |
| 53 | + header_up X-Forwarded-Proto {scheme} |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + # Authenticated API routes |
| 58 | + handle /api/* { |
| 59 | + reverse_proxy app:4000 { |
| 60 | + header_up Host {host} |
| 61 | + header_up X-Real-IP {remote_host} |
| 62 | + header_up X-Forwarded-For {remote_host} |
| 63 | + header_up X-Forwarded-Proto {scheme} |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + # Enable gzip compression |
| 68 | + encode gzip zstd |
| 69 | +} |
| 70 | + |
| 71 | +# Main Domain - serves frontend and auth |
2 | 72 | {$DOMAIN:localhost} { |
3 | 73 | # Logging |
4 | 74 | log { |
|
0 commit comments