Nginx日志优化

一 日志轮训切割

[root@centos7 tools]# cat nginx_log.sh 
#!/bin/bash

cd /var/log/nginx/ &&
/bin/mv access.log access_%(date +%F -d -1day).log
systemctl reload nginx 

二 不记录不需要的访问日志

        location ~.*.(js|jpg|JPG|JPEG|css|bmp|gif|GIF)% {
          access_log off;
}

三 修改日志格式为json格式

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format access_json '{"@timestamp":"$time_iso8601",'
          '"host":"$server_addr",'
          '"clientip":"$remote_addr",'
          '"size":$body_bytes_sent,'
          '"responsetime":$request_time,'
          '"upstreamtime":"$upstream_response_time",'
          '"upstreamhost":"$upstream_addr",'
          '"http_host":"$host",'
          '"url":"$uri",'
          '"domain":"$host",'
          '"xff":"$http_x_forwarded_for",'
          '"referer":"$http_referer",'
          '"status":"$status"}';
  access_log  /var/log/nginx/access_json.log  access_json;
host":"$server_addr:服务端地址
clientip":"$remote_addr: 记录客户端IP地址
size":$body_bytes_sent:  发送给客户端的字节数,不包括响应头的大小
responsetime":$request_time:请求处理时间,单位为秒 从客户端请求到写入日志时间
upstreamtime":"$upstream_response_time:指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间。
upstreamhost":"$upstream_addr:upstream :属于handler,只是他不产生自己的内容,而是通过请求后端服务器得到内容
http_host":"$host:
url":"$uri:
domain":"$host:
xff":"$http_x_forwarded_for:
referer":"$http_referer: 记录从哪个页面链接访问过来的
status":"$status:记录请求状态码






原文地址:https://www.cnblogs.com/yanshicheng/p/9460407.html