nginx日志自定配置、调用、详解
1.首先介绍一下nginx支持输出的日志内容:$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
$remote_user :用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区; $request:用来记录请求的url与http协议; $status :用来记录请求状态;成功是200, $body_bytes_s ent:记录发送给客户端文件主体内容大小; $http_referer:用来记录从那个页面链接访问过来的; $http_user_agent:记录客户端浏览器的相关信息; 2.nginx配置文件内可以定制多种日志输出格式,只要根据需要调用配置即可:以下是我摘录的我们服务器上的一段配置,我定制了三种类型!http{ include mime.types; default_typeapplication/octet-stream; #第一种默认输出日志输出; log_format combined ' $remote_addr - $remote_user [$time_local] ' '"$request"$status $body_bytes_sent ' '"$http_referer""$http_user_agent"'; access_logoff;#第二种日志输出,去掉了客户端相关信息; log_formataccess'$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_logoff; #第三种日志输出,只输出一些基本的内容; log_formataalog'$remote_addr - $remote_user [$time_local] $request ' '"$status" "$http_referer"'; access_log off; 3.日志输出胡格式和内容要根据生产系统的需要进行定制,只记录必要内容。 4.介绍一下日志调用,下面展示一组nginx虚拟主机调用日志配置; server { listen 80; server_name www.aa.com ; access_log /data/nginxlog/aa.com.log aalog buffer=64k;# /data/nginxlog/ #存放日志文件的位置,注意权限!# aa.com.log #生成日志文件名称; # aalog #调用定制日志输出格式,上面介绍了三种(combined、access、aalog)在这里定义上面配置的日志输出模板 ; # buffer=64k #设置日志输出缓冲区,就是日志写满64k空间往磁盘写一次; location / { root /data/js; indexindex.html index.htm; } } 以上就是就是nginx日志输出自定义配置方法的简要介绍,大家可以根据需要自由定制日志输出内容。 6.nginx日志切割,有些nginx服务器上配置很多虚拟主机,日志切割是个问题,特针对这种情况整理了一个多域名日志切割脚本。more nginxcutlog.sh #!/bin/bash ##该脚本作用是切割nginx日志## ##把nginx日志路径赋值给nginx_log_path变量##把nginx日志文件名赋值给log_filenames变量##把nginx备份分区路径赋给nginx_oldlog_path变量 nginx_log_path="/data/nginxlog/"nginx_oldlog_path="/date/backup/"log_filenames=`/bin/ls $nginx_log_path`##调试前一部分程序是否运行正常#echo $log_filenames#exit for log_name in $log_filenamesdo#/bin/mv $nginx_log_path/$log_name$nginx_oldlog_path/`date +%F`-$log_name/bin/mv /data/nginxlog/$log_name/date/backup/`date +%F`-$log_namedone #nginx restart#/usr/local/nginx/sbin/nginx -s>kill -HUP `cat /usr/local/nginx/logs/nginx.pid`find /date/backup/ -name "*.log" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1
页:
[1]