nginx日志文件配置

nginx日志文件

错误日志

错误日志级别分为:debug(调试),info(基本信息),notice(基本信息),warn(警告),error(错误),crit(更严重),默认为crit级别

语法格式:
    Syntax:error_log file [level];
    示例:
    error_log logs/www_error.log error;

日志访问日志

官方链接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
#定义日志出输格式
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;

参数说明:
$remote_addr:客户端访问的源IP信息
$remote_user:客户端用户认证信息
[$time_local]:显示访问时间
$request:请求行信息
$status:状态码信息
$body_bytes_sent:服务端响应给客户端的数据大小信息
$http_referer:记录链接到网站的域名信息
$http_user_agent:用户访问网站客户端软件标识信息
$http_x_forwarded_for:反向代理
示例:
#编写配置文件,添加访问日志
[root@web01 conf]# cat nginx.conf
worker_processes  1;
error_log logs/www_error.log error;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include       extra/www.conf;
    include       extra/bbs.conf;
    include       extra/blog.conf;
    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/www_access.log  main;
}
#重启服务之后,测试访问
[root@web01 conf]# ../sbin/nginx -s reload
[root@web01 conf]# curl bbs.etiantian.org
10.0.0.7 bbs.etiantian.org
#日志输出
10.0.0.7 - - [25/Feb/2019:11:58:30 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/7.19.7 
(x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"

因日志文件数据过大,需进行日志分割

使用sehll脚本进行日志分割
[root@web01 scripts]# vim cut_log.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)
mv /application/nginx/logs/www_access.log  /application/nginx/logs/access.log.$data_info

/application/nginx/sbin/nginx -s reload

#设置定时任务
# cut nginx log cron
* */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
posted @ 2019-03-11 09:30  游走在边缘的人  阅读(1385)  评论(0编辑  收藏  举报