Nginx + FPM records PHP Error Log

Source: Internet
Author: User
Tags php error php error log syslog
After migrating the architecture from Apache + mod_php to nginx + FPM, you will find that if a PHP script error occurs, no record is recorded in eror_log of nginx, and only 500 status code is recorded in access_log. Cause: nginx transfers the PHP script to FPM through FastCGI, so the error information will not be recorded in the error_log of nginx, however, debugging by programmers in the testing environment is a tragedy. Therefore, we need to configure the FPM Error Log nginx + FastCGI + FPM configuration. For details, see: Configure:
; Global options ;;;;;;;;;;;;;;;;;;; [Global]; PID file; note: the default prefix is/var; default value: none; pid = run/php-fpm.pidpid =/var/run/; error log file; if it's set to "syslog", log is sent to syslogd instead of being written; in a local file .; note: The default prefix is/var; default value: log/php-fpm.log; error_log = log/php-fpm.log # configure error_log, set error_log storage location error_log =/var/log/FPM/php5-fpm.error.log; syslog_facility is used to specify what type of program is logging the; message. this lets syslogd specify that messages from different facilities; will be handled differently .; see syslog (3) for possible values (ex daemon equiv log_daemon); default value: Daemon; syslog. facility = daemon; syslog_ident is prepended to every message. if you have multiple FPM; instances running on the same server, you can change the default value; which must suit common needs .; default Value: PHP-FPM; syslog. ident = PHP-FPM; log level; possible values: Alert, error, warning, notice, debug; default value: notice # error level (the error level should be improved in the production environment, recommended error) log_level = notice # indicates the number of PHP-CGI processes with SIGSEGV or sigbus errors within the time set by emergency_restart_interval. If the number of PHP-CGI processes exceeds emergency_restart_threshold, php-FPM will gracefully restart emergency_restart_threshold = 60emergency_restart_interval = 60 s; Time Limit for child processes to wait for a reaction on signals from Master .; available units: S (econds), m (inutes), H (ours), or D (ays); default unit: seconds; default value: 0; process_control_timeout = 0; the maximum number of processes FPM will fork. this has been design to control; the global number of processes when using dynamic PM within a lot of pools .; use it with caution .; note: A value of 0 indicates no limit; default value: 0; process. max = 128; send FPM to background. set to 'no' to keep FPM in foreground for debugging .; default Value: yes # Run FPM in the background and set it to yesdaemonize = Yes

The configuration file (for example, www. conf) in the pool. d directory (1) must be enabled for a parameter in the configuration file. The parameter definitions are clearly described in the English Annotations:

; Redirect worker stdout and stderr into main error log. If not set, stdout and; stderr will be redirected to /dev/null according to FastCGI specs.; Note: on highloaded environement, this can cause some delay in the page; process time (several ms).; Default Value: nocatch_workers_output = yes
(2) Both access_log and slow_log can be enabled.
access.log = /var/log/fpm/php5-fpm.access.log; The access log format.; The following syntax is allowed;  %%: the '%' character;  %C: %CPU used by the request;      it can accept the following format:;      - %{user}C for user CPU only;      - %{system}C for system CPU only;      - %{total}C  for user + system CPU (default);  %d: time taken to serve the request;      it can accept the following format:;      - %{seconds}d (default);      - %{miliseconds}d;      - %{mili}d;      - %{microseconds}d;      - %{micro}d;  %e: an environment variable (same as $_ENV or $_SERVER);      it must be associated with embraces to specify the name of the env;      variable. Some exemples:;      - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e;      - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e;  %f: script filename;  %l: content-length of the request (for POST request only);  %m: request method;  %M: peak of memory allocated by PHP;      it can accept the following format:;      - %{bytes}M (default);      - %{kilobytes}M;      - %{kilo}M;      - %{megabytes}M;      - %{mega}M;  %n: pool name;  %o: ouput header;      it must be associated with embraces to specify the name of the header:;      - %{Content-Type}o;      - %{X-Powered-By}o;      - %{Transfert-Encoding}o;      - ....;  %p: PID of the child that serviced the request;  %P: PID of the parent of the child that serviced the request;  %q: the query string ;  %Q: the '?' character if query string exists;  %r: the request URI (without the query string, see %q and %Q);  %R: remote IP address;  %s: status (response code);  %t: server time the request was received;      it can accept a strftime(3) format:;      %d/%b/%Y:%H:%M:%S %z (default);  %T: time the log has been written (the request has finished);      it can accept a strftime(3) format:;      %d/%b/%Y:%H:%M:%S %z (default);  %u: remote user;; Default: "%R - %u %t \"%m %r\" %s";access.format = %R - %u %t "%m %r%Q%q" %s %f %{mili}d %{kilo}M %C%% ; The log file for slow requests; Default Value: not set; Note: slowlog is mandatory if request_slowlog_timeout is setslowlog = /var/log/fpm/php5-fpm.log.slow ; The timeout for serving a single request after which a PHP backtrace will be; dumped to the 'slowlog' file. A value of '0s' means 'off'.; Available units: s(econds)(default), m(inutes), h(ours), or d(ays); Default Value: 0request_slowlog_timeout = 3

FPM logs are split by day

#! /Bin/bash-#1. FPM log storage path php5_fpm_path = "/var/log/php5-fpm" path_array = ($ php5_fpm_path) #2. log ID prefix array icontact_fpm_sign = "test" test_fpm_sign = "php5-fpm" prefix_array = ($ icontact_fpm_sign $ test_fpm_sign) #3. log success or slow identifies the suffix array bool_array = ("access" "slow" "error") #4. nginx log cut backup suffix Postfix = 'date + % Y % m % d '". log "#5. back up the current log file, rename it to the date suffix for Path in $ {path_array [*]} dofor prefix in $ {prefix_array [*]} dofor bool in $ {bool_array [*]} dofile = $ path/$ prefix. $ bool. logbackfile = $ path/$ prefix. $ bool. $ postfixif [-e $ file] thenmv $ File $ backfilefidonedonedone #6. find the nginx process number and generate a new log file fpm_pid = 'ps-Aux | grep-e 'PHP-FPM: master Process '| grep-V 'grep' | awk' {print $2} ''# usr1: reopen log files, refresh the nginx log file kill-usr1 $ fpm_pid
Experience and bug sharing

To reduce the output of error messages on the online serverLog_level = Error for the php-fpm.confBut after this configuration, I can no longer see the error message in the error log of the php5-fpm.

Locate the cause

Let's take a look at the error log information after I set log_level = notice:

It can be found that FPM positions all errors of PHP, whether it is warning or error, as the notice level. Therefore, the error log for online viewing of FPM can only be set to notice.

Related Article

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