Nginx日志配置与切割

Nginx日志的指令主要有两条:

log_format,设置日志的格式
access_log,指定日志文件的存放路径、格式和缓存大小

1、log_format指令用来设置日志的记录格式,它的语法如下
log_format name format {format …}

其中name表示定义的格式名称,format表示定义的格式样式。

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request"'
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"';
 access_log  logs/access.log  main;

也可以自定义一份日志的记录格式,不过要注意,log_format指令设置的名称在配置文件中是不能重复的。

2、access_log 语法如下
access_log path [format [buffer=size | off ] ]
其中path表示日志文件的存放路径,format表示使用log_format指令设置的日志格式的名称,buffer=size表示设置内存缓冲区的大小,例如可以设置buffer=32k。
如果不想记录日志,可以使用以下指令关闭日志记录:

	access_log off

自定义的日志输出格式和目录:

	log_format  main  '"time_local":"$time_local",'
                      '"remote_addr":"$remote_addr",'
                      '"remote_port":"$remote_port",'
                      '"remote_user":"$remote_user",'
                      '"server_port":"$server_port",'
                      '"server_port":"$server_name",'
                      '"request_method":"$request_method",'
                      '"request":"$request",'
                      '"request_uri":"$request_uri",'
                      '"request_length":"$request_length",'
                      '"scheme":"$scheme",'
                      '"http_referer":"$http_referer",'
                      '"http_x_forwarded_for":"$http_x_forwarded_for",'
                      '"http_accept":"$http_accept",'
                      '"http_accept_encoding":"$http_accept_encoding",'
                      '"http_accept_language":"$http_accept_language",'
                      '"http_cache_control":"$http_cache_control",'
                      '"http_connection":"$http_connection",'
                      '"http_content_length":"$http_content_length",'
                      '"http_cookie":"$http_cookie",'
                      '"http_host":"$http_host",'
                      '"http_origin_host":"$http_origin_host",'
                      '"http_pragma":"$http_ragma",'
                      '"http_user_agent":"$http_user_agent",'
                      '"http_proxy_connection":"$http_proxy_connection",'
                      '"request_time":"$request_time",'
                     '"upstream_response_time":"$upstream_response_time",'
                      '"sent_http_connection":"$sent_http_connection",'
             '"sent_http_content_encoding":"$sent_http_content_encoding",'
 					'"sent_http_content_type":"$sent_http_content_type",'
                      '"sent_http_location":"$sent_http_location",'
					  '"status":"$status",'
'"sent_http_content_security_policy":"$sent_http_content_security_policy"'
                      '"gzip_ratio":"$gzip_ratio",'
                      '"body_bytes_sent":"$body_bytes_sent",'
                      '"upstream_addr":"$upstream_addr"';
					  
	access_log  /home/ec2-user/data/nginx-logs/access.log  main;

想要记录更详细的信息需要自定义设置log_format,具体可设置的参数格式及说明如下:

	参数                      说明                                         示例
$remote_addr             客户端地址                                    211.28.65.253
$remote_user             客户端用户名称                                --
$time_local              访问时间和时区                                18/Jul/2012:17:00:01 +0800
$request                 请求的URIHTTP协议                           "GET /article-10000.html HTTP/1.1"
$http_host               请求地址,即浏览器中你输入的地址(IP或域名)     www.wang.com 192.168.100.100
$status                  HTTP请求状态                                  200
$upstream_status         upstream状态                                  200
$body_bytes_sent         发送给客户端文件内容大小                        1547
$http_referer            url跳转来源                                   https://www.baidu.com/
$http_user_agent         用户终端浏览器等信息                           "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol            SSL协议版本                                   TLSv1
$ssl_cipher              交换数据中的算法                               RC4-SHA
$upstream_addr           后台upstream的地址,即真正提供服务的主机地址     10.10.10.100:80
$request_time            整个请求的总时间                               0.205
$upstream_response_time  请求过程中,upstream响应时间                    0.002
nginx日志文件的切割

nginx的日志文件没有rotate功能,过一段时间之后日志就会很大,故我们每天进行日志切割。
实现方案:
1 重命名日志文件,如更改为access_yyyyMMdd.log,需注意的是nginx通过文件描述符定位日志文件,因此在重命名之后还是能往该文件内写入内容。

2 向nginx主进程发送USR1信号。 nginx的master进程接到信号后:
重新从配置文件中读取日志文件名 -> 关闭重名日志文件 -> 创建并打开日志文件(原来的名称) -> 通过worker进程作出改变

实现步骤:
1、编写日志切割脚本,cut_nginx_log.sh

#author: Mr.Huang
#!/bin/bash
#日志文件存放目录
logs_path="/home/ec2-user/data/nginx-logs/"
# pid文件
pid_path="/home/ec2-user/program/nginx/logs/nginx.pid"
#重命名日志文件
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
#向nginx主进程发送信号以重新打开日志
kill -USR1 `cat ${pid_path}`

注意,最后一行的" ` "怎么打出来,就是英文输出时,tab键上面那个键。

执行该脚本,查看脚本是否有误:

sh -x cut_nginx_log.sh  #sh命令中-x:实现shell脚本逐条语句的跟踪。

脚本报错:
在这里插入图片描述解决方式:查看脚本第九行,发现没有权限执行该命令故第九行修改为(该问题大部分情况不会发生):

sudo kill -USR1 `cat ${pid_path}`

2、 设置定时任务

crontab -e #添加定时任务

# 在任务时添加定时时间及脚本路径
0 0 * * * bash /home/ec2-user/data/cut_nginx_log.sh
# 每天凌晨00分将nginx日志access.log重命名为昨天的日期格式,并重新生成今天的新日志
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值