search
HomeBackend DevelopmentPHP ProblemWhat should I do if nginx cannot find the php file?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment