【问题标题】:NGINX Problem with Subdir with PHP Framework带有 PHP 框架的 Subdir 的 NGINX 问题
【发布时间】:2019-07-05 12:04:00
【问题描述】:

我有一个 PHP 框架,其中包含如下文件夹:

/index.php : front controller
/controllers/* : page controllers
/models/* : models
/views/* : views

我设置了 NGINX 规则,如果没有找到明确的文件或文件夹路径,它会将请求重写为 index.php。我这样做:

location / {
    try_files $uri $uri/ @php;
}

location @php {
    rewrite ^/(.*)$ /index.php?$1 last;
}

一切都很好,直到我想将此框架重复到一个名为“keys”的子目录中以用于单独的 Web 应用程序。现在发生的情况是,如果我调用https://example.com/keys/,它会调用/home/example.com/www/keys/index.php,这是正确的,因为它是子目录的前端控制器,而不是父文件夹的前端控制器( /home/example.com/www/index.php)。但问题是,如果我调用 /keys/login,它应该将此请求发送到 keys/index.php 前端控制器。相反,它将请求发送到 /index.php(根前端控制器)。 如何解决此问题,以便对包含子目录的路径的请求将调用子目录的正确前端控制器?

【问题讨论】:

    标签: php nginx url-rewriting


    【解决方案1】:

    在您的 location @php 块中添加另一个重写规则,使其看起来像这样:

    location @php {
        rewrite ^/keys/(.*)$ /keys/index.php?$1 last;
        rewrite ^/(.*)$ /index.php?$1 last;
    }
    

    last 指令表示运行该规则并停止在该块中进行进一步的重写。因此,当您检测到起始文件夹路径为 /keys/ 的 URL 并将其发送到正确的前端控制器文件时进行拦截,否则将请求发送到根前端控制器文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2017-12-02
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多