返回顶部

nginx 的日志服务(错误日志及访问日志)

一.错误日志

Syntax: error_log file [level];
Default:	
error_log logs/error.log error;                    #格式
Context:	main, http, mail, stream, server, location     #可以配置的环境

  

 

可以自己命名错误日志的名字

参考官网链接

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

 

 

vim nginx.conf
error_log /tmp/error.log error;

[root@web01 extra1]# head -5 /application/nginx/conf/nginx.conf
worker_processes  1;
    error_log logs/test_error.log  error;
events {
    worker_connections  1024;
}

 

error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit
该级别在日志名后边定义格式如下:
error_log /your/path/error.log crit;

crit 记录的日志最少,而debug记录的日志最多。
如果nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,
那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富

 

二.访问日志

 

 错误日志的的格式

 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;

 

           

 access_log  logs/access.log  main;  --- 定义日志信息要记录的内容格式

 

 

 Configuration

 解释
$remote_addr
 访问客户端的源地址信息
$remote_user
 访问客户端认证用户信息
[$time_local]
 显示访问时间
$request
 请求行信息
$status
 状态码信息(304状态码利用缓存显示页面信息)
$body_bytes_sent
 服务端响应客户端的数据大小信息
$http_referer
 服务端响应客户端的数据大小信息
$http_user_agent
 用户访问网站客户端软件标识信息;用户利用客户端浏览器测试访问时,win10默认浏览器会有异常问
$http_x_forwarded_for
 ???  反向代理

 

 官方链接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

 

Directives
Syntax:    access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default:    
access_log logs/access.log combined;
Context:    http, server, location, if in location, limit_except

 

 

[root@web01 conf]# head -15  nginx.conf
worker_processes  1;
    error_log logs/test_error.log  error;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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;
    include  extra1/www.conf;

 

 

日志样例

172.16.1.7 - - [11/Dec/2018:18:49:24 +0800] "GET / HTTP/1.1" 200 28 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

 

 

三.日志切割

生产环境中需要根据需求来切割日志

例如:

#!/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
00 */6 * * * /bin/sh /server/scripts/cut_log.sh  >dev/null 2>&1

 

posted on 2018-12-11 18:13  augustyang  阅读(651)  评论(0编辑  收藏  举报

导航