【问题标题】:Nginx is downloading PHP files when trying to remove the extension尝试删除扩展时 Nginx 正在下载 PHP 文件
【发布时间】:2018-05-06 20:08:33
【问题描述】:

所以,基本上我目前的配置如下。每当我尝试请求没有 PHP 扩展的 URL 时,它就会下载它?

server {

      listen [::]:80;

      root myDirectory;

      index index.php index.html;

      server_name myDomain;

      location / {
        try_files $uri $uri/ $uri.php $uri.php$is_args$query_string =404;

      }

      location ~\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
      }

      location ~ /\ ht {
        deny all;
      }

    }

我已经完成了其他问题中许多其他答案所建议的内容,例如编辑 php7.1-fpm php.ini 文件:

cgi.fix_pathinfo=0

有什么想法吗?

【问题讨论】:

  • 是的,检查你的 nginx 错误日志,在启动或请求 php 文件时生成的任何内容?
  • @hanshenrik 即使在调试模式下也没有错误,扩展也可以正常工作,这很奇怪......

标签: php nginx web server


【解决方案1】:

您当前使用的try_files 将不起作用。 try_files 语句的 file 元素在同一 location 块中处理,这是 PHP 文件的错误位置。请参阅this document 了解更多信息。

有多种解决方案,但如果发现脚本文件存在,您可以使用命名位置执行内部重写

例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    if (-f $document_root$uri.php) { rewrite ^ $uri.php last; }
    return 404;
}
location ~ \.php$ {
    try_files $uri =404;

    include snippets/fastcgi-php.conf;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}

请参阅this caution 关于 if 的使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2016-11-03
    • 2015-10-16
    • 2014-09-19
    • 2020-11-08
    相关资源
    最近更新 更多