You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Apache的.htaccess转换为Nginx配置遇到问题求助

Apache .htaccess 转 Nginx 规则解决方案

转换后的Nginx配置

server {
    # 替换为你的站点根目录实际路径
    root /path/to/your/site;
    index index.php index.html;

    # 处理/system或/public开头的请求(注:原Apache规则逻辑矛盾,已修正为OR匹配)
    location ~ ^/(system|public) {
        rewrite ^/(.*)$ /index.php?/$1 last;
    }

    # 处理文件/目录不存在的请求,转发到入口文件
    location / {
        try_files $uri $uri/ /index.php?/$uri;
    }

    # PHP请求处理规则(需根据Azure环境调整fastcgi_pass)
    location ~ \.php$ {
        # Azure PHP 8.x环境通常使用TCP端口,例如127.0.0.1:9000,按需修改
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

关键说明

  1. 原Apache规则逻辑修正:原.htaccess第一条规则的两个RewriteCond为AND关系,一个请求URI不可能同时以/system和/public开头,因此这条规则永远不会生效。上述Nginx配置已将其修正为匹配/system或/public开头的请求,若需保留原无意义逻辑,可删除对应的location ~ ^/(system|public)块。
  2. 核心路由规则:try_files $uri $uri/ /index.php?/$uri; 完全对应原规则中“请求文件/目录不存在则转发到index.php”的逻辑,是Nginx实现伪静态的标准写法。
  3. Azure环境适配:需根据Azure PHP 8.x服务计划的实际配置调整fastcgi_pass地址,Azure默认通常使用127.0.0.1:9000,不确定可查看Azure门户中的PHP运行时配置。

内容的提问来源于stack exchange,提问作者Pecana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:17:25