nginx - 子目录的php返回404

问题描述 投票:1回答:1

我从主机的子位置获取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个回答
1
投票

location块从周围区域的$document_root语句继承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

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.