config-sample/caddy

78 lines
2.6 KiB
Plaintext

nano /etc/caddy/Caddyfile
domain2.com {
reverse_proxy 0.0.0.0:
}
yourdomain.com {
@allowed_origins {
origin_regexp ^(https?://(app\.example\.com|localhost:3000|dev\.site\.org))$
}
header @allowed_origins Access-Control-Allow-Origin "{http.request.header.origin}"
header @allowed_origins Access-Control-Allow-Methods "GET, POST, OPTIONS"
header @allowed_origins Access-Control-Allow-Credentials "true"
reverse_proxy localhost:8055
}
yourdomain.com {
# Handle OPTIONS preflight requests
@preflight method OPTIONS
# Allow both HTTP and HTTPS origins (including localhost)
@allowed_origins {
origin http://localhost:3000
origin https://localhost:3000
origin https://*.example.com
origin https://your-production-domain.com
}
# CORS headers for actual requests
header @allowed_origins Access-Control-Allow-Origin "{http.request.header.origin}"
header @allowed_origins Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
header @allowed_origins Access-Control-Allow-Headers "*"
header @allowed_origins Access-Control-Allow-Credentials "true"
header @allowed_origins Vary Origin
# Special handling for preflight requests
header @preflight Access-Control-Allow-Origin "{http.request.header.origin}"
header @preflight Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
header @preflight Access-Control-Allow-Headers "*"
header @preflight Access-Control-Max-Age "3600"
# Proxy to your backend
reverse_proxy localhost:8055 {
header_up Host {host}
header_up X-Real-IP {remote}
header_up X-Forwarded-Proto {scheme}
}
# Ensure OPTIONS requests get 204 responses
respond @preflight 204
}
# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace ":80" below with your
# domain name.
:80 {
# Set this path to your site's directory.
root * /usr/share/caddy
# Enable the static file server.
file_server
# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080
# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000
}
# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile