How to solve nginx not prompting php error

PHPz
Release: 2023-03-28 17:07:15
Original
1299 people have browsed it

In daily website development, we often use Nginx as the web server and PHP as the back-end language. However, sometimes we find that even if there is an error in PHP, Nginx will not prompt any information, which makes it very difficult for us to debug the program. This article will introduce some solutions to enable Nginx to prompt PHP error messages.

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 of PHP errors. Information may expose security risks 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. At the same time, we can also 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_ALL means outputting all error level information. 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

4. Summary

The trick to configure the Nginx web server to prompt PHP error message hiding is to set it from both the Nginx server and the PHP process. This can help developers quickly identify errors in the code, help quickly fix problems, and improve code quality.

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:php.cn
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