Nginx日志输出配置json格式

 更新时间:2024年07月15日 11:45:46   作者:富士康质检员张全蛋  
本文主要介绍了Nginx日志输出配置json格式,包含log_format和access_log两种命令,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

nginx服务器日志相关指令主要有两条:

(1) 一条是log_format,用来设置日志格式

(2) 另外一条是access_log,用来指定日志文件的存放路径、格式和缓存大小。

log_format指令用来设置日志的记录格式,它的语法如下:
log_format name format {format ...} 其中name表示定义的格式名称,format表示定义的格式样

网上统一方法: 修改nginx.conf配置文件

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    
    # 原有日志格式,不能注释或者去掉
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_time';
    # json日志格式
    log_format log_json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"x_forwarded": "$http_x_forwarded_for", '
                        '"up_addr": "$upstream_addr",'
                        '"up_host": "$upstream_http_host",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        ' }';
 
    access_log  logs/access.log log_json; # 引用日志格式名称
 
    (省略内容)
}

在 Nginx 的配置文件nginx.conf中,我们定义了两种的日志格式:main和log_json,其中main为普通的文本格式,log_json为 json 格式。

log_json其实就是手工构造一个 json 字符串。定义了 json 的日志格式后,便可以指定 access log 为 json 格式,修改 Nginx 的配置,重启 Nginx ,便可以看到 json 格式的日志,重启 Nginx。

附加:可能遇到的问题

背景:
  因为我这边是通过nginx去代理了很多域名,所以会有很多平台访问的日志,通过在nginx.conf主配置文件增加  include vhosts/*.conf; 导入主机配置,所以会存在一个server段中只能有一个名称的问题。 

正确操作:

修改nginx.conf配置文件

[root@elk-nginx-01 conf]# cd /data/services/nginx/conf/
[root@elk-nginx-01 conf]# pwd
/data/services/nginx/conf
[root@elk-nginx-01 conf]# ll
总用量 72
-rw-r--r-- 1 root root 1077 9月   3 09:45 fastcgi.conf
-rw-r--r-- 1 root root 1077 9月   3 09:45 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 9月   3 09:45 fastcgi_params
-rw-r--r-- 1 root root 1007 9月   3 09:45 fastcgi_params.default
-rw-r--r-- 1 root root 2837 9月   3 09:45 koi-utf
-rw-r--r-- 1 root root 2223 9月   3 09:45 koi-win
-rw-r--r-- 1 root root 5231 9月   3 09:45 mime.types
-rw-r--r-- 1 root root 5231 9月   3 09:45 mime.types.default
-rw-r--r-- 1 root root 3729 9月   3 15:49 nginx.conf
-rw-r--r-- 1 root root 2656 9月   3 13:59 nginx.conf-bak0903
-rw-r--r-- 1 root root 2656 9月   3 09:45 nginx.conf.default
-rw-r--r-- 1 root root  636 9月   3 09:45 scgi_params
-rw-r--r-- 1 root root  636 9月   3 09:45 scgi_params.default
drwxr-xr-x 2 root root  134 9月   3 11:34 sslkeys
-rw-r--r-- 1 root root  664 9月   3 09:45 uwsgi_params
-rw-r--r-- 1 root root  664 9月   3 09:45 uwsgi_params.default
drwxr-xr-x 2 root root   87 9月   3 16:53 vhosts
-rw-r--r-- 1 root root 3610 9月   3 09:45 win-utf
[root@elk-nginx-01 conf]# vim nginx.conf
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    
    # 原有日志格式,不能注释
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_time';
    # json日志格式
    log_format json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"x_forwarded": "$http_x_forwarded_for", '
                        '"up_addr": "$upstream_addr",'
                        '"up_host": "$upstream_http_host",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        ' }';
 
    #导入主机配置
    include vhosts/*.conf;
 
    (省略内容)
}
保存退出!
 
[root@elk-nginx-01 conf]# pwd
/data/services/nginx/conf
[root@elk-nginx-01 conf]# cd vhosts/
[root@elk-nginx-01 vhosts]# ll
总用量 12
-rw-r--r-- 1 yfbkf yfbkf  921 9月   3 15:37 gonggao.conf
-rw-r--r-- 1 yfbkf yfbkf 1482 9月   3 16:50 gongdan.conf
-rw-r--r-- 1 yfbkf yfbkf 1151 9月   3 14:42 gongmo.conf

然后重启nginx

总结:
1、原有的日志格式不能注释或者去掉,只能新增一个log_format
2、新增自定义一份日志记录格式,需要注意,log_format指令设置的名称在配置文件中是不能重复的(比如我json日志格式名称 json)
3、原有的日志格式如果调整了,需要在log后加上名称才生效

Nginx日志常用参数详解

    log_format json '{"@timestamp":"$time_iso8601",'
                     '"scheme":"$scheme",'
                     '"http_referer":"$http_referer",'
                     '"args":"$args",'
                     '"http_user_agent":"$http_user_agent",'
                     '"remote_addr":"$remote_addr",'
                     '"hoste":"$host",'
                     '"server_name":"$server_name",'
                     '"server_protocol":"$server_protocol",'
                     '"request_method":"$request_method",'
                     '"request_uri":"$request_uri",'
                     '"uri":"$uri",'
                     '"request_length":"$request_length",'
                     '"body_byte_sent": "$body_bytes_sent",'
                     '"request_time":"$request_time",'
                     '"server_addr":"$server_addr",'
                     '"status": $status,'
                     '"bytes_sent":"$bytes_sent",'
                     '"upstream_addr":"$upstream_addr",'
                     '"upstream_status":"$upstream_status",'
                     '"upstream_connect_time":"$upstream_connect_time",'
                     '"upstream_response_time":"$upstream_response_time",'
                     '"request_id":"$request_id"'
                     '}';

可以加上这些:
$request_filename:当前请求的文件路径,由root或alias指令与URI请求生成。
$http_cookie:客户端cookie信息
$http_host #请求地址,即浏览器中你输入的地址(IP或域名)
$server_port:请求到达服务器的端口号。
$connection_requests 当前通过一个连接获得的请求数量。
  • $remote_addr:记录访问网站的客户端地址

$remote_user:远程客户端用户名称
$time_local:记录访问时间与时区
$request:表示request请求头的行

  • $status:http状态码,记录请求返回的状态,例如:200、404、301等
  • $body_bytes_sent:服务器发送给客户端的响应body字节数,发送给客户端的文件主体内容的大小,比如899,可以将日志每条记录中的这个值累加起来以粗略估计服务器吞吐量。

$http_referer:记录此次请求是从哪个链接访问过来的,可以根据refer进行防盗链设置
$http_user_agent:记录客户端访问信息,例如:浏览器,手机客户端等

  • $http_x_forwarded_for:客户端的真实ip,通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

$http_host 请求地址,即浏览器中你输入的地址(IP或域名)
$ssl_protocol:SSL协议版本
$ssl_cipher:交换数据中的算法
$upstream_status:upstream状态,比如成功是200
$upstream_addr:当ngnix做负载均衡时,可以查看后台提供真实服务的设备
$upstream_response_time:请求过程中,upstream响应时间

  • $request_time:整个请求的总时间,请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。

$args:这个变量等于请求行中的参数,同$query_string
$content_length:请求头中的Content-length字段。
$content_type:请求头中的Content-Type字段。
$document_root:当前请求在root指令中指定的值。
$host:请求主机头字段,否则为服务器名称。
$http_user_agent:客户端agent信息
$http_cookie:客户端cookie信息
$limit_rate:这个变量可以限制连接速率。
$request_method:客户端请求的动作,通常为GET或POST。
$remote_addr:客户端的IP地址。
$remote_port:客户端的端口。
$remote_user:已经经过Auth Basic Module验证的用户名。
$request_filename:当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme:HTTP方法(如http,https)。
$server_protocol:请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr:服务器地址,在完成一次系统调用后可以确定这个值。
$server_name:服务器名称。
$server_port:请求到达服务器的端口号。
$request_uri:包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri:不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri:与$uri相同。 

[apps@ngx-ali01 conf]$ cat nginx.conf
user  apps apps;
worker_processes  auto;

error_log /apps/svr/tengine/logs/nginx_error.log error;
pid /apps/svr/tengine/logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  65535;
}


http {
      log_format main '{"@timestamp":"$time_iso8601",'
                        '"@source":"$server_addr",'
                        '"idc":"huzhou",'
                        '"http_cookie":"$http_cookie",'
                        '"hostname":"$hostname",'
                        '"ip":"$http_x_forwarded_for",'
                        '"client":"$remote_addr",'
                        '"request_method":"$request_method",'
                        '"scheme":"$scheme",'
                        '"domain":"$server_name",'
                        '"referer":"$http_referer",'
                        '"request":"$request_uri",'
                        '"args":"$args",'
                        '"size":$body_bytes_sent,'
                        '"request_body":"$request_body",'
                        '"status": $status,'
                        '"responsetime":$request_time,'
                        '"upstreamtime":"$upstream_response_time",'
                        '"upstreamaddr":"$upstream_addr",'
                        '"http_user_agent":"$http_user_agent",'
                        '"https":"$https"'
                        '}';

        access_log /apps/svr/tengine/logs/access.log main;

        limit_conn_zone $server_name zone=perserver:10m;
        limit_conn_zone $binary_remote_addr zone=perip:20m;
#       limit_req_zone $rt_filtered_ip zone=qps:10m rate=30r/s;

        include mime.types;
        default_type    application/octet-stram;
        server_tokens   off;
        server_tag      off;
        server_info     off;
        server_names_hash_bucket_size 128;
        sendfile        on;
        keepalive_timeout 65;
        gzip  on;
        gzip_http_version 1.1;
        gzip_vary on;
        gzip_comp_level 6;
        gzip_proxied any;
        gzip_types text/plain  text/css application/json  application/x-javascript text/xml application/xml application/xml+rss text/javascript application/x-shockwave-flash image/png image/x-icon image/gif image/jpeg;
        gzip_buffers 16 8k;
        proxy_temp_path /dev/shm/temp;
        proxy_cache_path /dev/shm/cache levels=2:2:2 keys_zone=cache_go:20m inactive=1d max_size=2g;

        vhost_traffic_status_zone;
        vhost_traffic_status_filter_by_host on;
        include conf.d/*.conf;
        include ztoky_cn/*.conf;
}

到此这篇关于Nginx日志输出配置json格式的文章就介绍到这了,更多相关Nginx日志输出json内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • 教你利用Nginx 服务搭建子域环境提升二维地图加载性能的步骤

    教你利用Nginx 服务搭建子域环境提升二维地图加载性能的步骤

    这篇文章主要介绍了利用 Nginx 服务搭建子域环境提升二维地图加载性能,本文分步骤通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2021-09-09
  • Nginx启用GZIP压缩网页传输方法(推荐)

    Nginx启用GZIP压缩网页传输方法(推荐)

    Gzip压缩我很早已经就启用了,不过从未与大家分享过。今天小编给大家分享Nginx启用GZIP压缩网页传输方法,需要的朋友参考下吧
    2017-01-01
  • Nginx使用自签ssl证书实现https连接的方法

    Nginx使用自签ssl证书实现https连接的方法

    本文主要介绍了Nginx使用自签ssl证书实现https连接的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • nginx反向代理文件下载失败问题及解决

    nginx反向代理文件下载失败问题及解决

    这篇文章主要介绍了nginx反向代理文件下载失败问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Nginx中配置WebSocket代理的详细步骤

    Nginx中配置WebSocket代理的详细步骤

    Nginx 可以配置为 WebSocket 代理,将 WebSocket 连接从客户端转发到后端服务器,以下是如何在 Nginx 中配置 WebSocket 代理的详细步骤和示例配置,需要的朋友可以参考下
    2025-02-02
  • Nginx的一些常用配置汇总

    Nginx的一些常用配置汇总

    nginx配置说简单也简单,说复杂也复杂,入门简单,精通难,下面这篇文章主要给大家介绍了关于Nginx的一些常用配置,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • Nginx禁止指定UA访问的方法

    Nginx禁止指定UA访问的方法

    这篇文章主要介绍了Nginx禁止指定UA访问的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Ubuntu上安装Nginx服务器程序及简单的环境配置小结

    Ubuntu上安装Nginx服务器程序及简单的环境配置小结

    Nginx是一款高性能的异步非阻塞服务器应用程序,人气相当高,这里我们就来看一下在Ubuntu上安装Nginx服务器程序及简单的环境配置小结:
    2016-07-07
  • nginx中斜杠(‘/‘)的具体使用

    nginx中斜杠(‘/‘)的具体使用

    在Nginx配置的过程中,斜杠(/)经常使用到,它们不仅可以区分不同的路径,还有其他的作用,本文就详细的介绍了nginx中斜杠(‘/‘)的具体使用,感兴趣的可以了解一下,感兴趣的可以了解一下
    2023-10-10
  • Nginx增添api接口的实现方法

    Nginx增添api接口的实现方法

    这篇文章给大家介绍了Nginx增添api接口的方法,文章通过代码示例介绍的非常详细,对大家的学习或工作有一定的帮助,具有一定的参考价值,需要的朋友可以参考下
    2023-10-10

最新评论