1. 程式人生 > 其它 >Nginx更改日誌格式為json

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;

作者:小家電維修

相見有時,後會無期。