caddy配置文件Caddyfile示例 发布时间:2026/9/24 14:01:09 尧图网站建设 { # 这块是全局配置 # http不要自动转跳https auto_https disable_redirects # 内部私有证书,不要自动安装到certs系统目录里 skip_install_trust # 在线核对证书状态间隔时间, ocsp_interval 12h # 全局监听配置 #servers { # http请求头最大字节大小 # max_header_size 5MB # # tcp keepalive保活时间 # keepalive_interval 30s # listener_wrappers { # # http自动转跳https # http_redirect # tls # } #} # 全局日志 log { level ERROR # based on hostname # output file /var/log/caddy/{host}.log format json } # 全局设置信任来自 Cloudflare 节点的请求头 servers { trusted_proxies static 173.245.48.0/20 103.21.244.0/22 103.22.200.0/22 103.31.4.0/22 141.101.64.0/18 108.162.192.0/18 190.93.240.0/20 188.114.96.0/20 197.234.240.0/22 198.41.128.0/17 162.158.0.0/15 104.16.0.0/13 104.24.0.0/14 172.64.0.0/13 131.0.72.0/22 } } # ------ # 举例使用 import block__handle_check_slash_end_args /test (block__handle_check_slash_end_args) { #route { # noSlash { # path_regexp [^/]$ # Match paths that do not end with a slash # } # redir noSlash {http.request.scheme}://{http.request.host}{http.request.uri}/ 302 # } append_slash { path_regexp /({args[0]}|{args[1]}|{args[2]}|{args[3]}|{args[4]}|{args[5]}|{args[6]}|{args[7]}|{args[8]}|{args[9]})[^/]*$ } redir append_slash {path} {path}/ 302 } # 举例使用 import block__sub_header (block__sub_header) { header { -server server IIS/10.0 x-aspnet-version 9.0 x-server-version 9.0 frame-options SAMRORIGIN -proxy-connection } } # 举例使用 import block__logger_args /tmp/logs/caddylogs/xxx.log (block__logger_args) { log { output file /home/datum/logs/caddylogs/{args[0]}.log { roll_size 1gb roll_uncompressed } } } # 举例使用 import block__cors (block__cors) { route { CheckOptions { method OPTIONS } # 跨域预请求响应 respond CheckOptions 200 { #header Access-Control-Allow-Headers * #这个有问题,不能是通配符 #header Access-Control-Allow-Headers X-XSRF-TOKEN,XSRF-TOKEN,language,token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization header Access-Control-Allow-Methods GET,POST,PUT,OPTIONS header Access-Control-Allow-Origin {http.request.header.Origin} header Access-Control-Allow-Headers {http.request.header.Access-Control-Request-Headers} header Access-Control-Allow-Credentials true #respond 200 } notOptions { not method OPTIONS } # 默认常规请求方式响应(非options) header notOptions { # Echo Access-Control-Allow-Methods GET, POST, PUT, OPTIONS Access-Control-Allow-Origin {http.request.header.Origin} Access-Control-Allow-Headers {http.request.header.Access-Control-Request-Headers} Access-Control-Allow-Credentials true } } } # 举例使用 import block__proxy_upstream_cors_headers_strip (block__proxy_upstream_cors_headers_strip) { #请求上游重写替换请求头,Strip header_up Access-Control-Allow-Origin header_up Access-Control-Allow-Methods header_up Access-Control-Allow-Headers # 响应给下游(浏览器)的响应头maybe duplicated header_down Access-Control-Allow-Methods GET,POST,PUT,OPTIONS header_down Access-Control-Allow-Origin {http.request.header.Origin} header_down Access-Control-Allow-Headers {http.request.header.Access-Control-Request-Headers} header_down Access-Control-Allow-Credentials true } # 举例使用 import block__html_no_cache (block__html_no_cache) { # Apply no-cache headers only to .html files htmlFiles { path *.html } header htmlFiles { Cache-Control no-cache, no-store, must-revalidate, max-age0 Pragma no-cache Expires 0 } } # 代码块使用举例:import block__http2https (block__http2https) { CheckHttp { protocol http } redir CheckHttp https://{hostport}{uri} 302 } # 代码块使用举例: import block__proxy_upstream_args * https://www.baidu.com (block__proxy_upstream_args) { reverse_proxy {args[:]} { header_down -proxy-connection flush_interval 300ms transport http { dial_timeout 10s keepalive 10m keepalive_idle_conns 10240 } } } # 代码块使用举例: import block__proxy_upstream_start_with_host_args www.baidu.com 8.8.8.8 (block__proxy_upstream_start_with_host_args) { reverse_proxy {args[1:]} { header_up Host {args[0]} header_down -proxy-connection flush_interval 300ms transport http { dial_timeout 10s keepalive 10m keepalive_idle_conns 10240 } } } # 举例使用 import block__hide_git (block__handle_hide_git) { denyGit { path (/.*)?/.git(/.*)? } respond denyGit 404 } # 举例使用 import block__compress (block__compress) { encode zstd gzip } # 举例使用 import block__handle_strip_path_and_proxy_args test/* http://baidu.com (block__handle_strip_path_and_proxy_args) { handle /{args[0]} { uri strip_prefix /{args[1]} reverse_proxy {args[2]} { header_down -proxy-connection flush_interval 300ms transport http { dial_timeout 10s keepalive 10m keepalive_idle_conns 10240 } } } } # 举例使用 import block__handle_strip_path_and_file_server_vue3_history_mode_args /test/* /path/to/html/static/file/dir (block__handle_strip_path_and_file_server_vue3_history_mode_args) { handle_path /{args[0]} { file_server { hide .git index index.html } root * {args[1]} try_files {path} /index.html } # endif } # 举例使用 import block__handle_strip_path_and_file_server_only_args /test/* /path/to/html/static/file/dir (block__handle_strip_path_and_file_server_only_args) { handle_path /{args[0]} { file_server { index index.html hide .git } root * {args[1]} } # endif } # 公共 (block__common) { import block__sub_header #import block__logger #import block__cors import block__html_no_cache import block__handle_hide_git import block__compress } # ---------- import ./conf/*.conf 网站建设 企业官网 运营推广 返回列表