【问题标题】:nginx - php of subdirectory is returning 404nginx - 子目录的 php 返回 404
【发布时间】:2019-07-13 05:55:49
【问题描述】:

我在主机的子位置的 php 页面上收到 404 错误。 我的配置是:

server {
    listen 80;
    root /var/www/html/nisite;
    index  index.php index.html index.htm;
    server_name  www2.company.com wp-newsite-stg-02.company.com;

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

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

  # =================================

    location /office {
        root /var/www/html/oldsite;
    }

}

当我转到 http://www2.company.com/office 时,我可以看到该页面,所有静态资产也都已提供,但是当我尝试访问 http://www2.company.com/office/php/form-process.php 时,我收到 404 错误。

为什么location ~ \.php$ 不能正确处理这个请求?

谢谢。

【问题讨论】:

    标签: php nginx nginx-config


    【解决方案1】:

    location 块从周围块中的 root 语句继承 $document_root 的值。您正在从两个单独的根目录运行 PHP 脚本,因此您需要两个单独的 location 块来处理它们。

    解决方案是使用嵌套的location 块。

    例如:

    location ^~ /office {
        root /var/www/html/oldsite;
    
        location ~ \.php$ {
            try_files      $uri =404;
            fastcgi_pass   unix:/var/run/php/php7.2-fpm.sock;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
        }
    }
    

    使用^~ 修饰符确保正确的location 处理.php 文件。有关详细信息,请参阅this document。使用try_files 语句避免passing uncontrolled requests to php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 2018-04-29
      • 1970-01-01
      • 2014-09-03
      相关资源
      最近更新 更多