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

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

藏色散人
藏色散人Original
2021-07-19 10:56:212355browse

Solution to the problem that nginx cannot find the php process: 1. Modify the configuration of nginx.conf; 2. Use try_files to capture non-existent urls and return an error.

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

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

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

nginx cannot find php file

Use php-fpm to parse PHP. "No input file specified" and "File not found" are common errors that cause headaches for new nginx users. The reason The php-fpm process cannot find the .php file configured by SCRIPT_FILENAME to be executed, and php-fpm returns the default 404 error prompt to nginx.

For example, my website does not have test.php under document_root. When accessing this file, you can see the returned content by capturing the packet.

HTTP/1.1 404 Not Found
Date: Fri, 21 Dec 2012 08:15:28 GMT
Content-Type: text/html
Proxy-Connection: close
Server: nginx/1.2.5
X-Powered-By: PHP/5.4.7
Via: 1.1 c3300 (NetCache NetApp/6.0.7)
Content-Length: 16
File not found.

Many people don’t want users to see this default 404 error message directly, and want to customize the 404 error.

Before giving a solution Let's first analyze how to avoid this type of 404 error, and then talk about what to do when this happens (for example, the user enters a path that does not exist by mistake) so that a custom 404 error page can be displayed.

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

When this kind of error occurs, nine out of ten it is the back-end fastcgi process that receives the wrong path (SCRIPT_FILENAME), and the back-end fastcgi Most of the reasons for receiving error paths are 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. Therefore, 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 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 change with the change of $document_root. We can modify it. SCRIPT_FILENAME is configured as follows:

fastcgi_param  SCRIPT_FILENAME  documentrootfastcgi_script_name;

So we cannot forget to configure the root directive in the server block, otherwise the value of documentroot will be empty and only fastcgi_script_name will be passed to php-fpm, which will result in " 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 will only check whether $uri is If the file ends with .php, it will not judge whether the file exists. Requests that end with .php will be sent directly to php-fpm by nginx 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 errors.

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.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if nginx cannot find the php process?. 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