Nginx日志格式设置

nginx 服务器日志相关指令主要有两条,一条是log_format,用来设置日志格式,另一条是access_log,用来指定日志文件的存放路径、格式和缓存大小,通常在 nginx 的配置文件中日记配置(/usr/local/nginx/conf/nginx.conf)。php

nginx的log_format有不少可选的参数用于指示服务器的活动状态,默认的是:css

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';

想要记录更详细的信息须要本身设置log_format,具体可设置的参数格式及说明以下:html

参数 说明 示例
$remote_addr 客户端地址 211.28.65.253
$remote_user 客户端用户名称
$time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
$request 请求的URI和HTTP协议 “GET /article-10000.html HTTP/1.1”
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.it300.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

举例说明以下: 
一、配置文件python

#vim /usr/local/nginx/conf/nginx.conf

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for '
    '"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';

    include /usr/local/nginx/conf/vhost/*.conf;

二、vhost中配置文件linux

#vim /usr/local/nginx/conf/vhost/web.confserver

 {
  listen 80 default;
  server_name www.it300.com;
  index index.html index.htm index.php;
  root /data/httpd/it300.com;
  location ~ .*\.php?$
  {
   include fastcgi.conf;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
  expires 30d;
  }
  location ~ .*\.(js|css)?$
  {
  expires 1h;
  }
  access_log /data/logs/it300.com.log access;
 }

 

-- 2018-06-13 新增 --nginx

nginx 源码提供两种形式的时间格式web

一、[$time_local]

展示形式:[13/Jun/2018:13:50:48 +0800]


二、[$time_iso8601]

展示形式:[2018-06-13T13:50:26+08:00]

 

-- 2018-06-28 新增 --算法

一、access_logvim

access_log 指令用来指定日志文件的存放路径包含日志文件名、格式和缓存大小具体以下浏览器

access_log   path(存放路径)  [format(自定义日志格式名称)  [buffer=size | off]]

举例说明以下

access_log  logs/access.log  main;

若是想关闭日志能够以下

access_log off;

可以使用 access_log 指令的字段包括 http、server、location。

须要注意的是 Nginx 进程设置的用户和组必须对日志路径有建立文件的权限不然会报错。

小技巧

若是须要在 access_log 中记录 post 请求的参数能够参考 如何在access log中记录post请求的参数 。

Nginx 支持为每一个 location 指定强大的日志记录。

一样的链接能够在同一时间输出到不止一个的日志中更多信息请查看 Nginx模块参考手册:日志模块(Log) 。

 

二、错误日志

错误日志主要记录客户端访问 Nginx 出错时的日志格式不支持自定义。经过错误日志你能够获得系统某个服务或 server 的性能瓶颈等。

所以将日志好好利用你能够获得不少有价值的信息。错误日志由指令 error_log 来指定具体格式以下

error_log   path(存放路径)   level(日志等级)

path 含义同 access_log level 表示日志等级具体以下

[ debug | info | notice | warn | error | crit ]

# 从左至右日志详细程度逐级递减,即 debug 最详细 crit 最少

举例说明以下

error_log  logs/error.log  info;

须要注意的是 error_log  off 并不能关闭错误日志而是会将错误日志记录到一个文件名为 off 的文件中。

正确的关闭错误日志记录功能的方法以下

error_log  /dev/null;

# 上面表示将存储日志的路径设置为“垃圾桶”

 

 

参考资料

1. Nginx 日志各项参数含义

2. 如何在access log中记录post请求的参数

3. Nginx模块参考手册:日志模块(Log) 

相关文章
相关标签/搜索