【问题标题】:Nginx config: route all .php files via index except if it contains a path to the file managerNginx 配置:通过索引路由所有 .php 文件,除非它包含文件管理器的路径
【发布时间】:2018-04-11 00:17:10
【问题描述】:

我正在开发新项目,该项目将有许多来自旧网站 uri 的重定向。许多旧的 uri 在 uri 中包含 .php 扩展名,Nginx 尝试将它们作为文件加载,而不是回退到我们的重定向控制器方法。

为了让它更复杂一点,我们使用带有文件管理器的所见即所得编辑器,当调用它时 - 使用 .php 扩展名,因此需要排除这个。

实际上,我希望它以这样的方式工作,这样当我调用旧的uri(例如/old-page/path/file.php)时,它将通过index.php 路由它,但是当我调用/vendor/ckfinder/plugins/filemanager/dialog.php?... 时,它将加载实际文件。

我看过这篇文章,它并没有真正解决我所追求的,但我认为这是一个很好的起点How to remove both .php and .html extensions from url using NGINX?

我现有的设置是

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

任何帮助将不胜感激。

更新

我尝试了@Richard Smith 的建议,在阅读了我认为应该可行的文档后,但不幸的是,无论出于何种原因 - 它没有 - 这是我尝试过的:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    try_files $uri /index.php?$query_string;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    include fastcgi_params;
}

所以try_files 检查$uri 是否存在——如果不存在,它会退回到/index.php?$query_string;,它应该将请求指向index.php。知道我在这里可能缺少什么吗?

【问题讨论】:

    标签: php nginx server nginx-config


    【解决方案1】:

    如果新服务器上不存在 PHP 文件,最简单的解决方案是将任何不存在的 PHP 文件重定向到 /index.php - 就像您对非 PHP URI 所做的那样。

    例如:

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        try_files $uri /index.php?$query_string;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        include fastcgi_params;
    }
    

    fastcgi_split_path_infofastcgi_index 语句在这个特定的 location 块中不是必需的。

    请参阅this document 了解更多信息。

    【讨论】:

    • 我添加了指向try_files 手册页的链接。简短的回答是否定的 - 它会重定向那些在此服务器上不存在.php文件。
    • 谢谢 - 我会试试看。
    • 恐怕它不起作用 - 并没有真正做任何事情 - 事情的工作方式和以前一样。我在应用更改等后重新启动了 Nginx。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 2014-10-09
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多