【问题标题】:nginx multiple PHP files in try_files instructionnginx 在 try_files 指令中有多个 PHP 文件
【发布时间】:2018-12-19 15:01:59
【问题描述】:

早上好, 我想制作可以与不同 php 应用程序(symfony 和 Thelia)一起使用的相同 nginx 虚拟主机。 我的问题是 try_files 指令。在 symfony 中,try_files 必须以 app.php 为目标,但在 Thelia 中,它必须以 index.php 为目标。 所以想修改try_files语句如下:

server {
    listen 80;
    server_name *.tld;

    root /var/www/web;

    location / {
        try_files $uri /app.php$is_args$args /index.php$is_args$args;
    }

    location ~ ^/(app|app_dev|config|index|index_dev)\.php(/|$) {
        fastcgi_pass php_alias:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param APP_ENV dev;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 16k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

}

不幸的是,它不起作用。不再解释 PHP。那么如何在try_files语句中注册多个php文件呢?

【问题讨论】:

  • 您是否有特定的理由将给定的正则表达式用于 PHP 文件而不是标准的 location ~ \.php(/|$)
  • @IVOGELOV 是的,我需要这个特定的配置,但我看不到与 try_files 的关系。

标签: php nginx vhosts


【解决方案1】:

try_files 指令只能有一个 默认 URI,它是最后一个元素,位于所有 file 元素之后。您遇到的问题是 file 元素导致在当前location 中处理请求。请参阅this document 了解更多信息。

您可以使用命名location 来处理可选的默认 URI,例如:

location / {
    try_files $uri @rewrite;
}
location @rewrite {
    if (-f $document_root/app.php ) {
        rewrite ^ /app.php last;
    }
    rewrite ^ /index.php last;
}

请参阅this caution 了解if 的使用。

【讨论】:

    猜你喜欢
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2018-03-01
    • 2013-06-25
    • 1970-01-01
    相关资源
    最近更新 更多