Nginx更改日志格式为json
这里只是简单记录一下主配置文件中的format格式,因为在配置过程中一直不生效,最后发现是因为配置尾部没有加 ; 号导致,所以这里简单做一个记录。
#/etc/nginx/nginx.conf
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; #main只是一个引用名称,在access_log是引用的,名称可以改成任意 log_format main '{"nginx_timestamp":"$time_iso8601","clientip":"$remote_addr", ' '"nginx_host":"$server_addr","host":"$http_host","request":"$request","url":"$request_uri",' '"upstreamhost":"$upstream_addr","status":"$status","body_bytes_sent":"$body_bytes_sent",' '"request_time":"$request_time","upstream_response_time":"$upstream_response_time",' '"xff":"$http_x_forwarded_for","referer":"$http_referer","http_user_agent":"$http_user_agent",' '"request_length":"$request_length","request_method":"$request_method"}'; #配置文件尾部没有加上分号,nginx -t检测没有异常,但是format日志一直不生效,需注意。 access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
在Nginx的配置文件/etc/nginx/nginx.conf中,定义了日志格式:main,这个main只是一个引用名称,在配置日志的时候指定即可,当然使用这种方式可以配置几种不同的日志格式。
access_log /var/log/nginx/access.log main;