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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值