ywg 发表于 2018-11-12 12:08:41

Nginx_PHP的LOG设计

  作者:diege
  时间:2012-0-22
  一、nginx
  1、nginx错误日志
  1)       配置位置
  Nginx.conf顶层,第三行。
  error_log               /data/logs/nginx_error.log;
  2)       存放路径
  /data/logs/
  3)       备份切割:无
  如果要定位到站点的错误,可以按access.log设计
  在站点配置文件server{}块中
  error_log       /data/logs/$hostname_error.log;
  2、nginx访问日志
  1)       配置access.log格式和说明
  http{}顶层
  #log_format 有一个默认的,无需设置的combined日志格式设置
  #其格式为
  log_formatcombined'$remote_addr - $remote_user [$time_local]'
  '$request "$status" $body_bytes_sent '
  '"$http_referer" "$http_user_agent" '
  #默认LOG中有'$remote_addr和$remote_user中间 有-
  #其他自定义的LOG
  log_formatmain'$remote_addr - $remote_user [$time_local] $request '
  '"$status" $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  #后端记录真实访问IP
  log_format download'$remote_addr - $remote_user [$time_local] '
  '"$request" $status $bytes_sent '
  '"$http_referer" "$http_user_agent" '
  '"$http_range" "$sent_http_content_range"';
  log_formattimelog'$remote_addr - $remote_user [$time_local] $request '
  '"$status" $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"'
  '$request_time $upstream_response_time
  说明
  $remote_addr 远程地址
  $remote_user远程用户
  $time_local 写入普通日志格式的当地时间(服务器时间)
  $request请求的方法和连接
  $request_length 请求主体的长度。
  $request_time nginx处理请求的时间,单位为毫秒(0.5.19版本后可以使用秒为单位)
  $status 应答的状态(代码)
  $upstream_response_time 后端处理时间,代理的响应时间
  $upstream_status 后端服务器的状态
  $upstream_addr : 后端服务器的IP地址和端口
  $body_bytes_sent 减去应答头后传送给客户端的字节数,这个变量兼容apache模块mod_log_config的%B参数(在0.3.10前这个变量为$apache_bytes_sent)。
  $bytes_sent 传送给客户端的字节数。
  $http_referer 跳转$request的链接,就是从那里访问到$request
  $http_user_agent 浏览器操作系统信息等
  $http_x_forwarded_for 访问用户真实服务器的IP
  $http_range ?
  $sent_http_content_range ?
  $connection,连接数。
  $msec,正在写入日志条目的当前时间(精确到百万分之一秒)
  $pipe,如果请求为管道的。
  $status$upstream_status :响应的状态码。前端服务器响应状态,后端服务器响应状态
  $request_time$upstream_response_time :关注性能必备!看看前端传输文件的时间,以及后台处理的时间。前者对了解网络状态有益,后者对了解服务性能有益。
  $remote_addr$upstream_addr :用户的IP地址,后端服务的IP地址
  $bytes_sent $body_bytes_sent:发送给用户端的数据量(包含HTTP头和不包含HTTP头)
  $host $request :最重要的,搞定具体域名具体URL,不抓瞎。
  LOG测试
  log_format timelog'$remote_addr - $remote_user [$time_local]'
  '"$request" $status $bytes_sent '
  '"$http_referer" "$http_user_agent" '
  '"$http_range" "$sent_http_content_range" '
  '"$request_time nginx" "$upstream_response_time" '
  '"$upstream_status" "$connection"';
  访问后LOG内容
  172.17.2.160 - - "GET /getinfo.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2710 "http://www.example.com/getinfo.php" "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0" "-" "-" "0.004" "0.004" "200" "1"
  172.17.2.160 - - "GET /getinfo.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2332 "http://www.example.com/getinfo.php" "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0" "-" "-" "0.005" "0.005" "200" "5"
  172.17.2.160 - - "GET /getinfo.php?=SUHO8567F54-D428-14d2-A769-00DA302A5F18 HTTP/1.1" 200 3000 "http://www.example.com/getinfo.php" "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0" "-" "-" "0.005" "0.005" "200" "4"
  '"$http_range" "$sent_http_content_range" 这个字段是空的。
  代理(包括php-fpm代理)LOG
  设计LOG
  log_format proxylog'$remote_addr $remote_user [$time_local]'
  '$request" $status $bytes_sent '
  '$http_referer $http_user_agent '
  '$request_time $upstream_response_time '
  '$upstream_addr $upstream_status ';
  172.17.2.160 - GET /getinfo.php HTTP/1.1" 200 10496 - Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 0.030 0.030 172.18.17.223:9000 200
  172.17.2.160 - GET /getinfo.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2710 http://www.example.com/getinfo.php Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 0.003 0.003 172.18.17.223:9000 200
  172.17.2.160 - GET /getinfo.php?=SUHO8567F54-D428-14d2-A769-00DA302A5F18 HTTP/1.1" 200 3000 http://www.example.com/getinfo.php Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 0.004 0.004 172.18.17.223:9000 200
  172.17.2.160 - GET /getinfo.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2332 http://www.example.com/getinfo.php Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 0.003 0.003 172.18.17.223:9000 200
  压缩测试 错误 gzip是格式。不是压缩日志、
  2)       配置位置
  各个虚拟站点server{}块下
  access_log/data/logs/sitelogs/$servername.cn.access.logmain;
  access_log /data/logs/sitelogs/$servername.cn.access.loggzipbuffer=32k;
  nginx直接调用gzip压缩,不知道对性能印象有多大。所以,还不压缩。到凌晨再切割压缩。
  3)       存放路径
  /data/logs/sitelog
  4)       备份切割:
  凌晨0点对日志切割,压缩备份到/data/backup/httplogs,如果有需要再同步到其他服务器进行分析或导入数据库
  脚本
  #!/bin/sh
  # Name :            gzip_allvhosts_log.sh
  # Date created :    2012-4-22
  # Whom :            diege
  # Version :         V1.1,2012-4-22
  # Description :   处理所有nginx vhost的access log和error.log(如果有)
  yesterday=`/bin/date -v -1d +%Y-%m-%d`
  logbak_dir=/data/backup/httplogs
  httplog_dir=/data/logs/sitelogs
  allvhost=`ls -1 /data/logs/sitelogs/ | awk -F'-' '{print$1}' | uniq`
  #move all vhost log to backup dir
  for vhost in $allvhost
  do
  src_access_log=${httplog_dir}/${vhost}-access.log
  dst_access_log=${logbak_dir}/${vhost}/${vhost}_${yesterday}_access.log
  src_error_log=${httplog_dir}/${vhost}-error.log
  dst_error_log=${logbak_dir}/${vhost}/${vhost}_${yesterday}_error.log
  if [ ! -d ${logbak_dir}/${vhost} ];then
  mkdir -p ${logbak_dir}/${vhost}
  fi
  if [ -f $src_access_log ];then
  /bin/mv$src_access_log $dst_access_log
  fi
  if [ -f $src_error_log ];then
  /bin/mv$src_error_log $dst_error_log
  fi
  done
  # nginx log rotate
  kill -USR1 `cat/var/run/nginx.pid`
  #compress back log
  for vhost in $allvhost
  do
  cd ${logbak_dir}/${vhost}/ && /usr/bin/gzip *.log
  done
  二、php LOG
  1、错误日志error_log
  # vim /usr/local/etc/php.ini
  648 error_log = /data/logs/php_errors.log
  三、php-fpm 日志
  php的所有日志都存放在/data/logs/php-fpm/目录下
  # vim /usr/local/etc/php-fpm.conf
  1、配置php-fpm_error.log
  
  error_log =/data/logs/php-fpm/php-fpm_error.log
  2、配置php-fpm slowlog 慢日志
  
  slowlog = /data/logs/php-fpm/$pool.log.slow
  3、配置php-fpm access.log 访问日志 一般是nginx调用php-fpm处理php文件的日志
  
  access.log = /data/logs/php-fpm/$pool.access.log

页: [1]
查看完整版本: Nginx_PHP的LOG设计