系统崩溃了,网站响应慢了,应该如何快速定位错误

小天天天天    服务器    999+ 次    2022-02-22 18:20:09


系统崩溃了,或者网站响应很慢,网站出现 502。。。这些问题在工作上,或者开发过程中经常出现吧。这些问题或许在面试的时候也会经常被问到。那么你是怎么在第一时间检查错误,定位错误信息的呢!
出现以上的错误的话,我们经常想到的是日志吧。是的,作为一名程序员,比码代码还重要那么一点点的东西就是日志的分析和查询。下面来看看一些常见日志及设置方法:Nginx 的日志设置与 php-fpm 的一些日志设置


Nginx 的 access.log 和 error.log

1. nginx 常用的配置文件有两种: access.log 和 error.log
access.log 的作用是:记录用户所有的访问请求,不论状态码,包括 200 ,404,500 等请求,404,500 的请求并不会出现在 error.log 中。
error.log 的作用是:记录 nginx 本身运行时的一些错误,不会记录用户访问的请求。比如记录模块错误信息日志,以及 nginx 配置文件的错误日志等,格式不支持自定义,可以设置级别。

2. 设置 access_log
访问日志主要用于记录客户端的请求。客户端向 nginx 服务器发起的每一次请求都会被记录到 access_log 中。包含请求 IP、时间、访问 url 等等,当然访问日志中具体记录哪些日志信息我们可以通过 log_format 设置.
查看日志存放地址

find / -name nginx.conf

根据查询出来地址,进入 nginx.conf 文件查找 access_log 和 error_log 文件的路径

access_log 的设置语法:

log_format  combined  '$remote_addr - $remote_user  [$time_local]'
                      ' "$request"  $status  $body_bytes_sent'
                      ' "$http_referer"  "$http_user_agent"';

#日志格式允许包含的变量注释如下:
$remote_addr, $http_x_forwarded_for  //记录客户端IP地址
$remote_user   //记录客户端用户名称
$request      //记录请求的URL和HTTP协议
$status    //记录请求状态
$body_bytes_sent    //发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
$bytes_sent   //发送给客户端的总字节数。
$connection   //连接的序列号。
$connection_requests   //当前通过一个连接获得的请求数量。
$msec        //日志写入时间。单位为秒,精度是毫秒。
$pipe       //如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer      //记录从哪个页面链接访问过来的
$http_user_agent   //记录客户端浏览器相关信息
$request_length   //请求的长度(包括请求行,请求头和请求正文)。
$request_time    //请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_iso8601    //ISO8601标准格式下的本地时间。
$time_local      //通用日志格式下的本地时间。


参考实例:

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                        '"$status" $body_bytes_sent "$http_referer" '
                                        '"$http_user_agent" "$http_x_forwarded_for" '
                                        '"$gzip_ratio" $request_time $bytes_sent $request_length';

    log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
                                '"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
                                '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

    open_log_file_cache max=1000 inactive=60s;

    server {
        server_name ~^(www.)?(.+)$;
        access_log logs/$2-access.log main;
        error_log logs/$2-error.log;

        location /srcache {
            access_log logs/access-srcache.log srcache_log;
        }
    }
}

3 .设置 error_log
配置错误日志文件的路径和级别

error_log file [level];
Default:   
error_log logs/error.log error;

第一个参数指写入错误日志的路径
第二个参数指日志的级别。level 可以是:debug、info、notice、warn、error、crit、alert、emerg 中的任意值。只有日志的错误级别大于等于 level 指定的值才会被写入错误日志中,默认值是 error.

error.log 配置示例:

#错误日志保存位置
#error_log logs/error.log;

#指定错误日志的位置和级别
#error_log logs/error.log notice;
#error_log logs/error.log info;

PHP 开发中需要了解的两种日志是什么

有这么一种情景
在测试环境多番测试没有遇到问题,但在一次上线过程中,在线上环境出现 20 秒的响应超时,这个显然是环境的问题了,尽管在线上数据量大,可 Mysql 也不至于慢到 20 秒,而且发现每次都是 20.01~20.04 秒之间,相差不到一秒钟,mysql 也不至于这么均匀,这个时候你可能会去查看 Mysql 慢查询日志,如果发现没有超时的 sql 日志,那这有可能就是 php 这边出现的问题,这个时候你就要看看 php 的慢日志了。

php-fpm 慢日志
php 慢日志需要在 php-fpm.conf 设置,如果使用源码包安装默认请执行下面命令:

cp php-fpm.conf.default php-fpm.conf

默认通过源码包编译安装 php 目录应在:

/usr/local/php

目录下,如果你通过 yum 或者其他方式安装,不清楚或不知道 php 具体安装目录,可以使用:

find / -name php-fpm.conf

#或者

php -i | grep Path
------------------------------------------
[root@xxxx etc]# php -i | grep Path
Configuration File (php.ini) Path => /usr/local/php/etc
XPath Support => enabled
Path to sendmail => /usr/sbin/sendmail -t -i
[root@xxxx etc]#

开启慢查询日志
php 旧的版本是在 php-fpm.conf 这里设置的,而 php7.x 版本源码包编译后需要 www.conf 修改慢查询配置:

vim /usr/local/php/etc/php-fpm.d/www.conf

配置项都是一样的,如果你在 php-fpm.conf 找不到,就去他的同级目录 php-fpm.d 下面找找。

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.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: 0
;request_slowlog_timeout = 0
  • slowlog 设置慢查询日志的生成目录
  • request_slowlog_timeout 设置慢查询的标准时间(打开此配置就相当于开启了慢查询日志),配置以秒为单位,一般设置 3s。


php-error 错误日志
在生产环境中是不允许 php 报错的,就算报错也是白屏或者 500,所以在生产环境中的日志收集是非常重要的。

开启错误日志
一般情况下,php 错误日志的配置都在 php.ini 文件中

/usr/local/php/etc/php.ini

---------------------------
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
log_errors = On
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog
error_log 错误日志的生成目录
error_reporting 生产环境错误级别应全开
display_errors 在页面上不显示错误
log_errors 开启错误日志

最终的结果是

error_log = /var/log/php_error.log
display_errors = Off
error_reporting = E_ALL
log_errors = On




如果你觉得本篇文章对您有帮助,请打赏作者

上一篇: 如何在 Laravel 中找到最慢的查询

下一篇: 10个常见的Redis面试"刁难"问题

最新评论

暂无评论

热门文章

最新评论

网站数据

网站文章数:481

今日UV/PV/IP:22/29/21

昨日UV/PV/IP:22/22 /22

TOP