Caddy V2 Caddyfile for ProcessWire + Procache
这是来源于社区的基础版本:
mysite.fi, www.mysite.fi { encode gzip tls [email protected] root * /var/www/mysite/pw file_server php_fastcgi unix//var/run/php-fpm/php-fpm.sock { health_timeout 10000s } @deny_hidden path_regexp /\. @deny_root path_regexp /(CONTRIBUTING|COPYRIGHT|LICENSE|README|htaccess)\.txt @deny_assets path_regexp ^/site(-[^/]+)?/assets/(.*\.php|backups|cache|config|install|logs|sessions) @deny_install path_regexp ^/site(-[^/]+)?/install @deny_config path_regexp ^/(site(-[^/]+)?|wire)/(config(-dev)?|index\.config)\.php @deny_modules path_regexp ^/((site(-[^/]+)?|wire)/modules|wire/core)/.*\.(inc|module|php|tpl) @deny_templates path_regexp ^/(site(-[^/]+)?|wire)/templates(-admin)?/.*\.(inc|html?|php|tpl) rewrite @deny_hidden /denyaccess rewrite @deny_root /denyaccess rewrite @deny_assets /denyaccess rewrite @deny_install /denyaccess rewrite @deny_config /denyaccess rewrite @deny_modules /denyaccess rewrite @deny_templates /denyaccess # global rule try_files {path} {path}/ /index.php?it={path}&{query} log { format single_field common_log output file /var/log/www/access.log { roll_size 50MiB roll_keep 5 roll_keep_for 168h } } }
这是我修改后兼容Procache的版本
mysite.com { ########################################################################### # 1) 基本设置 ########################################################################### # 开启 gzip 压缩 encode gzip # 自动申请 TLS 证书(将下面的邮箱改成你自己的,用于 Let's Encrypt 通知) tls [email protected] # 设置站点根目录(所有相对路径均基于此目录) root * /mnt/www/mysite.com/public_html ########################################################################### # 2) 屏蔽敏感目录 /platform(直接返回 403) ########################################################################### @blockPlatform path /platform* respond @blockPlatform 403 ########################################################################### # 3) ProCache 缓存处理 # 此规则仅针对以下请求: # - 请求方法必须为 GET 或 HEAD(排除 POST、PUT 等) # - 请求 URL 中无任何 query string(通过 expression 判断) # - Cookie 中不包含 wire_challenge 或 wires_challenge # - 请求路径必须匹配指定正则:^/~?[-_.a-zA-Z0-9/]*$ # - 缓存目标文件在指定 ProCache 目录下存在(使用 file 模块判断) ########################################################################### @procache { # 3.1) 请求方法要求 GET 或 HEAD method GET HEAD # 3.2) 请求 URL 无 query string(使用 expression 判断为空) expression {http.request.uri.query} == "" # 3.3) Cookie 中不含 "wire_challenge" 或 "wires_challenge" not header Cookie wire_challenge not header Cookie wires_challenge # 3.4) 请求路径限定(此正则可根据需要修改或移除) path_regexp pcpath ^/~?[-_.a-zA-Z0-9/]*$ # 3.5) 判断缓存文件是否存在: # 将请求的 {path} 拼接到自定义 ProCache 目录下,构成完整缓存文件路径(最后附加 /index.html) file { try_files /site/assets/ProCache-xxxxxxxxxx{path}/index.html } } # 命中 ProCache 条件则直接 rewrite 到对应的静态文件 rewrite @procache /site/assets/ProCache-xxxxxxxxxx{path}/index.html # 为命中 ProCache 规则的请求添加自定义响应头(可帮助调试或标识缓存来源) header @procache { X-PWPC "ProCache" X-Powered-By "TOWAIT.COM" } ########################################################################### # 4) 静态文件服务 # 提供站点目录下的静态资源,如图片、CSS、JS 等 ########################################################################### file_server ########################################################################### # 5) PHP FastCGI 配置 # 将 PHP 请求转发给指定的 PHP-FPM 套接字进行处理 ########################################################################### php_fastcgi unix//run/php/php8.2-fpm.sock { # 可选项:设置 health_timeout,确保长时间运行的 PHP 脚本不会因超时中断 health_timeout 10000s } ########################################################################### # 6) 安全访问限制:阻止访问敏感文件和目录 # 以下规则使用多个匹配器来拦截不应直接访问的文件, # 如隐藏文件、站点配置、安装目录以及部分模块文件。 ########################################################################### # 6.1) 禁止访问以 . 开头的隐藏文件/目录(但允许 /.well-known 路径) @deny_hidden { path_regexp hidden ^/\. not path /.well-known* } # 6.2) 阻止访问根目录下的敏感文本文件(如 CONTRIBUTING、COPYRIGHT、LICENSE、README、htaccess.txt) @deny_root path_regexp rootfiles /(CONTRIBUTING|COPYRIGHT|LICENSE|README|htaccess)\.txt # 6.3) 阻止访问 site 或 site-xxx 目录中 assets 下的 PHP 文件、备份、缓存、配置、安装、日志、会话等目录 @deny_assets path_regexp denyAssets ^/site(-[^/]+)?/assets/(.*\.php|backups|cache|config|install|logs|sessions) # 6.4) 阻止访问 site 或 site-xxx 目录中的 install 目录 @deny_install path_regexp denyInstall ^/site(-[^/]+)?/install # 6.5) 阻止访问 site 或 wire 目录中的配置文件(如 config.php、index.config.php) @deny_config path_regexp denyConfig ^/(site(-[^/]+)?|wire)/(config(-dev)?|index\.config)\.php # 6.6) 阻止访问 modules 相关文件,防止直接调用核心或模块文件 @deny_modules path_regexp denyModules ^/((site(-[^/]+)?|wire)/modules|wire/core)/.*\.(inc|module|php|tpl) # 6.7) 阻止访问模板文件(包含 admin 模板)中的 PHP、HTML、TPL 或 INC 文件 @deny_templates path_regexp denyTemplates ^/(site(-[^/]+)?|wire)/templates(-admin)?/.*\.(inc|html?|php|tpl) # 对于以上匹配到的敏感内容,直接返回 403 Forbidden respond @deny_hidden 403 respond @deny_root 403 respond @deny_assets 403 respond @deny_install 403 respond @deny_config 403 respond @deny_modules 403 respond @deny_templates 403 ########################################################################### # 7) 动态请求重写 # 对于不存在的文件,将请求交给 ProcessWire 进行动态处理。 # 该规则相当于 Apache 的 try_files {path} {path}/ /index.php?it={path}&{query} ########################################################################### @notStatic { not file # 排除常见文件及特殊路径(favicon、robots.txt、webhook、platform 等) not path /favicon.ico not path /robots.txt not path /webhook* # 如有其它需要排除的路径,可继续添加 not path 规则 } rewrite @notStatic /index.php?it={path}&{query} ########################################################################### # 8) 日志配置 ########################################################################### # 8.1) 主日志记录,格式为 JSON,可改为 common_log 形式。记录所有访问日志。 log { format json output file /mnt/www/mysite.com/access.log { roll_size 50MiB roll_keep 5 roll_keep_for 168h } } # 8.2) 错误请求日志(记录 4xx 至 5xx 状态码的请求),以 JSON 格式存入 error.log @errors expression {http.response.status_code} >= 400 log @errors { output file /mnt/www/mysite.com/error.log { roll_size 50MiB roll_keep 5 } format json level ERROR } }