Home  >  Article  >  Backend Development  >  What should I do if nginx cannot find the php file?

What should I do if nginx cannot find the php file?

coldplay.xixi
coldplay.xixiOriginal
2020-07-22 10:08:474185browse

Solution to the problem that nginx cannot find the php file: 1. Place the root instruction in the server block so that each location inherits the documentroot defined by the parent server block; 2. nginx intercepts non-existent files and uses [try_files] Catches non-existent urls and returns an error.

What should I do if nginx cannot find the php file?

nginx cannot find the solution to the php file:

1. The wrong path is sent Go to the php-fpm process

. When such errors occur, nine out of ten are caused by the back-end fastcgi process receiving the wrong path (SCRIPT_FILENAME). The reason why the back-end fastcgi process receives the wrong path is mostly due to configuration errors. .

The common nginx.conf configuration is as follows:

server {
    listen   [::]:80;
    server_name  example.com www.example.com;
    access_log  /var/www/logs/example.com.access.log;  
    location / {
        root   /var/www/example.com;
        index  index.html index.htm index.pl;
    }
    location /images {
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
        include fastcgi_params;
    }
}

There are many unreasonable things in this configuration. One of the obvious problems is that the root directive is placed in location / piece. If the root directive is defined in a location block, the root directive can only take effect in the location where it is located. There is no root directive in other locaionts. For example, the location /images block will not match any request. You need to configure the root directive repeatedly in each request to solve this problem.

So we need to put the root directive in the server block, so that each location will inherit the documentroot defined by the parent server block. If a location needs to define a different documentroot, if a location If the location needs to define a different document_root, you can define a separate root directive in the location.

Another problem is that the fastCGI parameter SCRIPT_FILENAME is hard-coded. If you modify the value of the root directive or move the file to another directory, php-fpm will return the "No input file specified" error because SCRIPT_FILENAME is hard-coded in the configuration and does not follow $document_root changes with changes. We can modify the SCRIPT_FILENAME configuration as follows:

fastcgi_param  SCRIPT_FILENAME  documentrootdocumentrootfastcgi_script_name;

So we cannot forget to configure the root directive in the server block, otherwise the value of documentroot will be empty and it will only If the value passed to documentroot is empty, only fastcgi_script_name will be passed to php-fpm, which will cause a "No input file specified" error.

2. The requested file really does not exist

When nginx receives a request for a .php file that does not exist, because nginx only It will check whether $uri ends with .php and will not judge whether the file exists. Requests that end with .php nginx will directly send to php-fpm for processing. If the file cannot be found during php-fpm processing, it will return "No input file specified" with a "404 Not Found" header.

Solution

We intercept non-existent files in nginx, request and return a custom 404 error

Use try_files to capture non-existent urls and Return an error.

location ~ .php$ {
 try_files $uri =404;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME ....
 ...................................
 ...................................
}

The above configuration will check whether the .php file exists. If it does not exist, a 404 page will be returned.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What should I do if nginx cannot find the php file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn