Mac nginx File not found error

Keywords: PHP Nginx

Using php-fpm to parse PHP, "No input file specified" and "File not found" are common errors that make nginx novices headache. The reason is that the php-fpm process cannot find the. PHP file to execute for SCRIPT_FILENAME configuration, and php-fpm returns the default 404 error prompt 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 grabbing the package.

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. They want to customize 404 error.

Before giving a solution, let's first analyze how to avoid such 404 errors, and then say what we should do when we really encounter such a situation (such as user input a path where the error does not exist) in order to display a custom 404 error page.

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

Nine out of ten of these errors occur when the back-end fastcgi process receives the error path (SCRIPT_FILENAME), while the back-end fastcgi receives the error path mostly because of configuration errors.

The common configuration of 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;
        }
    }

    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 aspects to 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, the root instruction can only take effect on its location. There are no root instructions in other locaiont s, such as location /images blocks, which do not match any requests. The root instructions need to be reconfigured repeatedly in each request to solve this problem. So we need to place the root instruction in the server block so that each location inherits the $document_root defined by the parent server block, and if a location needs to define a different $document_root, we can define a root instruction separately in location.

Another problem is that the fastCGI parameter SCRIPT_FILENAME is written to death. If you change the value of the root instruction or move the file to another directory, php-fpm returns a "No input file specified" error, because SCRIPT_FILENAME is written to death in the configuration and does not change with the change of $doucument_root. We can modify the SCRIPT_FILENAME configuration as follows:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

So we can't forget to configure the root instruction in the server block. Otherwise, the value of $document_root is empty and only $fastcgi_script_name will be passed to php-fpm, which will result in a "No input file specified" error.

2. The requested document does not really exist

When nginx receives a request for an absent. PHP file, because nginx only checks whether the $uri is the end of. PHP and does not judge whether the file exists, the request for the end of. PHP nginx will be sent directly to php-fpm for processing. When php-fpm cannot find the file, it will return "No input file specified" with "404 Not Found" header.

Solution

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

Use try_files to capture urls that do not exist and return errors.

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

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

The above configuration checks for the existence of the. php file and, if not, returns 404 pages.

Posted by philweb on Mon, 07 Jan 2019 21:39:10 -0800