一、访问日志

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

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

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,具体可设置的参数格式及说明如下:

参数说明示例
$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
$statusHTTP请求状态200
$upstream_statusupstream状态200
$body_bytes_sent发送给客户端文件内容大小1547
$http_refererurl跳转来源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_protocolSSL协议版本TLSv1
$ssl_cipher交换数据中的算法RC4-SHA
$upstream_addr后台upstream的地址,即真正提供服务的主机地址10.10.10.100:80
$request_time整个请求的总时间0.205
$upstream_response_time请求过程中,upstream响应时间0.002

举例说明如下: 
1、配置文件

#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;

2、vhost中配置文件

#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;
 }


二、错误日志

错误日志主要记录客户端访问Nginx出错时的日志格式不支持自定义。通过错误日志你可以得到系统某个服务或server的性能瓶颈等。因此将日志好好利用你可以得到很多有价值的信息。

错误日志由指令error_log来指定具体格式如下
        error_log path(存放路径) level(日志等级)


        path含义同access_loglevel表示日志等级具体如下
        [ debug | info | notice | warn | error | crit ]


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


        举例说明如下
        error_log  logs/error.log  info;
        需要注意的是error_log off并不能关闭错误日志而是会将错误日志记录到一个文件名为off的文件中。


        正确的关闭错误日志记录功能的方法如下
        error_log /dev/null;
        上面表示将存储日志的路径设置为“垃圾桶”。



-- 2018-06-13 新增 --

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

1、[$time_local]
展现形式:[13/Jun/2018:13:50:48 +0800]

2、[$time_iso8601]
展现形式:[2018-06-13T13:50:26+08:00]




本文转自:http://www.it300.com/article-15353.htmlhttps://blog.csdn.net/u013474436/article/details/51317099