Home > Operation and Maintenance > Nginx > How to solve nginx not prompting php error

How to solve nginx not prompting php error

PHPz
Release: 2023-05-26 13:03:46
forward
1844 people have browsed it

1. Understanding error reporting and collection methods

In the process of deploying the server, we habitually turn off the error output of PHP. This is because PHP error messages may expose security vulnerabilities or lead to information leakage. But during the development process, we need these error messages to locate problems and debug the program.

One way to solve this problem is to turn on PHP's error output. In PHP we can set the error log level or report errors immediately. We can add some options to the Nginx configuration file to display PHP error messages.

2. PHP error settings in Nginx configuration file

Open the Nginx server configuration file, usually /etc/nginx/nginx.conf, find the http{} block, and add the following configuration:

server {
    # server settings
    ...
    
    # server block location rules
    ...

    # php-fpm status check
    location ~ ^/(status|ping)$ {
        access_log off;
        # php-fpm settings
        fastcgi_param PHP_VALUE "error_reporting=E_ALL";
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    # php error logs
    location ~ \.php$ {
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_error.log";
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    # error pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    # static files
    location ~* \.(jpg|jpeg|gif|png|css|js|ico)$ {
        expires 7d;
        access_log off;
    }

    # disable direct access to .ht files
    location ~ /\.ht {
        deny all;
    }
}
Copy after login

Among them, fastcgi_param PHP_VALUE is used to pass error information to the PHP process requested by the Nginx server. error_reporting=E_ALLIndicates that all error level information is output. fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_error.log"; means outputting PHP error information to the /var/log/nginx/php_error.log file.

After changing the configuration file, reload the Nginx server:

sudo systemctl reload nginx
Copy after login

3. Wrong settings in the PHP configuration file

The PHP configuration file is general For /etc/php/7.4/fpm/php.ini, find the line error_reporting and set it to display all error messages:

error_reporting = E_ALL
Copy after login

Then find display_errorsThis line, set it to On, so that all PHP error messages can be displayed on the web page:

display_errors = On
Copy after login

Then save the file and restart PHP- FPM:

sudo systemctl restart php7.4-fpm
Copy after login

The above is the detailed content of How to solve nginx not prompting php error. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template