Analysis and solution of Nginx File not found error

Source: Internet
Author: User

Using PHP-FPM to parse PHP, the error prompt is as follows: "No input file specified", "File not found" because PHP-FPM process could not find Script_ FileName is configured to execute the. php file, PHP-FPM returns the default 404 error prompt to Nginx.

For example, there is no test.php under Doucument_root, and when you access this file, you can see what is returned by grabbing the packet.

http/1.1 404 Not Found
Date:fri, Dec 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.

You do not want the user to see this default 404 error message and want to customize the 404 error.

Let's start by understanding how this 404 error is generated and how to display the custom 404 error page.

One, the wrong path is sent to the PHP-FPM process
Most of these errors occur when the back-end fastcgi process receives the error path (Script_filename), and the backend fastcgi receives the wrong path, mostly because of a configuration error.

Common configuration of the nginx.conf:

Copy Code code example:

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;
}
}

The above configuration problem analysis:
The root command is placed in the location/block. If the root command is defined in the location block, then the root command can only be applied to where it resides. There are no root commands in other locaiont, such as the location/images block does not match any requests and needs to be repeated in each request to configure the root command to resolve this issue. So we need to place the root command in the server block so that each location inherits the $document_root of the parent server block definition, and if a location needs to define a different $document_root, You can define a root command separately at location.

The problem 2,fastcgi parameter script_filename is written dead. If you modify the value of the root instruction or move the file to another directory, PHP-FPM will return the "no input file specified" error because script_filename in the configuration is dead and does not change with $doucument_root.

You can modify the Script_filename configuration as follows:

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Therefore, do not forget to configure the root command in the server block, otherwise the value of $document_root is empty, will only pass $fastcgi_script_name to PHP-FPM, will cause "No input file specified" error.

Second, the requested file does not exist physically

When Nginx receives a request that is not in the. php file, Nginx will only check if the $uri is. PHP ends, does not determine whether the file exists, or not. The PHP end of the request will be sent directly to PHP-FPM processing. PHP-FPM processing when the file is not found will return "No input file specified" with "404 Not Found" header.

Workaround:
Block non-existent files in Nginx, request and return custom 404 error

Use Try_files to catch non-existent URLs and return an error.

Copy Code code example:Location ~. php$ {
Try_files $uri = 404;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename ....
...................................
...................................
}

The configuration above checks to see if the. php file exists and returns a 404 page if it does not exist.

Introduce these, about Nginx 404 error content, we recommend that you master, combined with 404 custom error page, to achieve friendly error prompts it.

Analysis and solution of Nginx File not found error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.