Nginx Error: File not found [Use php-fpm to parse PHP]

Use php-fpm to parse PHP. “No input file specified” and “File not found” are common errors that make nginx novices headaches. The reason is that the php-fpm process cannot find the .php file to be executed configured by SCRIPT_FILENAME, php-fpm The default 404 error message returned to nginx.

For example, there is no test.php under my website doucument_root. When accessing this file, you can see the returned content by capturing packets.

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 do not want users to see this default 404 error message directly, but want to customize the 404 error.

 

Before giving a solution, let’s analyze how to avoid this kind of 404 error, and then tell us what to do when we really encounter this situation (for example, the user enters a path that does not exist), so that a custom 404 error can be displayed page.

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

Of these types of errors, nine out of ten are because the back-end fastcgi process received the wrong path (SCRIPT_FILENAME), and the back-end fastcgi received the wrong path because of configuration errors.

The configuration of common nginx.conf 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 places in this configuration. One of the obvious problems is that the root instruction is placed in the location / block. If the root instruction is defined in the location block, then the root instruction can only take effect for the location where it is located. There is no root instruction in other locaionts. For example, the location /images block will not match any request. You need to configure the root instruction repeatedly in each request to solve this problem. Therefore, we need to put the root instruction in the server block, so that each location will inherit the definition of the parent server blockdocument_root, if a location needs to define a differentdocument_root, you can define a root instruction separately in location.

Another problem is that the fastCGI parameter SCRIPT_FILENAME is hard-coded. If you modify the value of the root command or move the file to another directory, php-fpm will return a “No input file specified” error, because SCRIPT_FILENAME is hard-coded in the configuration and has not changed with the change of $doucument_root, we can modify SCRIPT_FILENAME is configured as follows:

fastcgi_param SCRIPT_FILENAME  document_rootfastcgi_script_name;

 

So we must not forget to configure the root instruction in the server block, otherwiseThe value of document_root is empty, it will only passfastcgi_script_name to php-fpm, this will cause the “No input file specified” error.

 

2. The requested file really does not exist

When nginx receives a request for a .php file that is not present, because nginx will only check whether $uri is at the end of .php, and will not judge whether the file exists. The request for the end of .php will be sent directly to php-fpm for processing. . When php-fpm cannot find the file, 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, and if it does not exist, it will return a 404 page.

Similar Posts: