# ============================================================
# 万网 / 阿里云云虚拟主机 —— NGINX 极简伪静态（推荐）
#
# 位置：主机控制面板 → 高级环境设置 → NGINX 设置
# 用法：把面板里默认的那两段（location / {} 与 location ~ /\.ht {deny all;}）
#       整体替换成本文件内容，保存后生效。
#
# 为什么这么短：YikaiCMS 的 index.php 内置路由分发器（includes/Dispatcher.php），
# 只要把「不存在的文件」统一交给 index.php，剩下的路由由 PHP 完成——
# 等价于 WordPress 那条通用规则。需要显式 rewrite 全量规则时用
# 同目录的 aliyun-nginx.htaccess。
#
# 该面板仅支持 location/allow/deny/try_files/rewrite/return/if/set，
# 且后七条须写在 location 内，本文件已遵守。
# ============================================================

# ---- 1) 拦截敏感目录与文件 ----
location ~ ^/(config|storage|vendor|includes|bin|migrations|recipes)/ {
    deny all;
}

# 上传目录里的 PHP 一律不执行。位置有意排在最前：nginx 正则 location 按出现顺序匹配、
# 第一个命中即生效，排在平台自带的 PHP handler 之后等于没写。
# 由来：2026-08-23 实测本类主机 /uploads/x.php 会被执行；包内 uploads/.htaccess
# 只对 Apache 有效。下面 `location /` 的 `if (!-e ...)` 只把**不存在**的路径交给
# index.php，已存在的文件直接返回——所以上传目录落进一个 .php 就是可执行的。
location ~ ^/uploads/.*\.(php|phtml|phar|php[0-9])$ {
    deny all;
}

# 文档 / SQL / 备份 / 部署样例：不该被当静态文件下载
location ~* \.(md|sql|bak|example|dist|conf|lock|yml|yaml)$ {
    deny all;
}
location ~ ^/deploy/ {
    deny all;
}

# 云虚拟主机的 PHP handler 由平台托管，不能在自定义规则里安全重复 fastcgi_pass。
# 只放行安装入口，其余安装资源拒绝 HTTP 访问；安装完成后的锁检查由 index.php 执行。
location = /install/ {
    rewrite ^ /install/index.php last;
}
location ~ ^/install/(?!index\.php$) {
    deny all;
}

location ~ /\.(git|env|htaccess|htpasswd) {
    deny all;
}

location ~ ^/(composer\.(json|lock)|package(-lock)?\.json)$ {
    deny all;
}

# ---- 2) 主规则：真实文件直出，其余交给 index.php ----
location / {
    if (!-e $request_filename) {
        rewrite ^ /index.php last;
    }
}
