【问题标题】:try_files directive being ignored when used with FastCGI与 FastCGI 一起使用时,try_files 指令被忽略
【发布时间】:2018-10-23 09:17:59
【问题描述】:

我使用 nginx 作为 PHP 和 PHP-FPM 的反向代理。我正在使用以下配置:

server {
  listen 80;

  root /var/www/html;
  index index.php;
  error_page 403 /error-403;
  error_page 404 /error-404;

  location ~ \.php$ {
    if ($http_x_forwarded_proto = 'https') {
      set $fe_https 'on';
    }

    try_files $uri =400;

    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS $fe_https;
    fastcgi_pass php:9000;
  }
}

我正在使用 try_files 指令 as recommended 来防止对 PHP 的不受控制的请求。

但是,经过一些测试,try_files 似乎没有做任何事情。如果我请求一个不存在的 .php 文件(如 /no-file.php)的 URL,我会收到来自 PHP 的 404 错误。相反,我预计 nginx 会出现 400 错误。

我错过了什么吗? try_files 似乎什么也没做。

【问题讨论】:

    标签: nginx fastcgi nginx-location


    【解决方案1】:

    我发现了问题所在。该问题是由if 子句引起的:

    if ($http_x_forwarded_proto = 'https') {
      set $fe_https 'on';
    }
    

    我不确定具体原因,但删除它会使try_files 工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      相关资源
      最近更新 更多