Nginx官方日志文档

https://docs.nginx.com/nginx/admin-guide/monitoring/logging/

Nginx日志级别

设置访问日志的系统日志消息的严重性级别。可能的值按严重性递增的顺序为:debuginfonoticewarnerror(默认)、critalertemerg

error_log  /var/log/nginx/error.log 「可选 文件名」 debug;

指定日志记录规则

Nginx变量如下:

  • $remote_addr:客户端 IP 地址
  • $remote_user:客户端用户名(如果启用了基本身份验证)
  • [$time_local]:请求时间(使用本地时间格式)
  • "$request":请求方法和 URI
  • $status:服务器响应状态码
  • $body_bytes_sent:响应正文的字节数
  • "$http_referer":引荐来源 URL
  • "$http_user_agent":客户端 User-Agent 头部信息

定义一个日志输出信息

在http块定一个日志格式叫:mylog

http {

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

}

使用指定日志格式

可写在server块,也可以写在http块

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        access_log  /Users/zanglikun/Desktop/nginxlog.log  mylog;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

输出演示

192.168.0.101 - - [22/Aug/2023:15:46:51 +0800] "GET /download/ HTTP/1.1" 200 397 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
192.168.0.101 - - [22/Aug/2023:15:46:51 +0800] "GET /download/ HTTP/1.1" 200 397 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
192.168.0.101 - - [22/Aug/2023:15:46:51 +0800] "GET /download/ HTTP/1.1" 200 397 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
192.168.0.101 - - [22/Aug/2023:15:46:51 +0800] "GET /download/ HTTP/1.1" 200 397 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
192.168.0.101 - - [22/Aug/2023:15:46:52 +0800] "GET /download/ HTTP/1.1" 200 397 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"

Nginx自带分割示例 不建议使用

不做详细解说了

access_log /var/log/nginx/access.log combined buffer=64k flush=5m if=$loggable;
access_log /var/log/nginx/access.log combined buffer=64k flush=5m if=$loggable if=$loggable max_size=10m;

access_log /var/log/nginx/access_$time_local.log mylog buffer=64k flush=5m if=$loggable if=$loggable max_size=10m;
特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取最新全部资料 ❤