Nginx Log Log_format format

Source: Internet
Author: User

Official documents:

Http://nginx.org/en/docs/http/ngx_http_log_module.html

The ngx_http_log_module module writes request logs in the specified format.

Requests is logged in the context of a location where processing ends. It May is different from the original location, if an internal redirect happens during request processing.

Example Configuration

Log_format compression ' $remote _addr-$remote _user [$time _local] "                       " $request "$status $bytes _sent"                       "$http _ Referer "" $http _user_agent "" $gzip _ratio "access_log/spool/logs/nginx-access.log compression buffer=32k;

Nginx has roughly three types of variables that can be recorded in Log_format

    • HTTP request variable-arg_parameter http_header send_http_header (server return)
    • Built-in variables-nginx built-in
    • Custom variables-defined by yourself

Let's take a look at the default Log_format

    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘                      ‘$status $body_bytes_sent "$http_referer" ‘                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;    access_log  /var/log/nginx/access.log  main;

We see the default format, which is basically single quotes ' wrapped around some variables, and also includes an underscore-square bracket [] printed as a delimiter.
Each variable has this meaning:

remote_addr:对应客户端的地址remote_user:是请求客户端请求认证的用户名,如果没有开启认证模块的话是值为空。time_local:表示nginx服务器时间request:表示request请求头的行status:表示response的返回状态body_bytes_sent:表示从服务端返回给客户端的body数据大小http_referer:表示请求的上一级页面http_user_agent:表示agent信息http_x_forwarded_for:会记录每一级请求中信息

1. Log_format Normal format
123 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" "$upstream_response_time"‘;
2. Log_format JSON Format

To facilitate the use of the Elastic Stack log platform to collect the display Nginx log, you can change the Nginx log into JSON format. The modified JSON log format is as follows:

In Nginx configuration file nginx.conf , we define two kinds of log formats: main and log_json , where, main for normal text format, log_json JSON format. It log_json is actually a JSON string that is constructed by hand. Once you have defined the log format for JSON, you can specify that access log is in JSON format:

123456789101112131415 log_format logJson ‘{                         "@timestamp""$time_local", ‘                         ‘"@fields": { ‘                         ‘"remote_addr": "$remote_addr", ‘                         ‘"remote_user": "$remote_user", ‘                         ‘"body_bytes_sent": "$body_bytes_sent", ‘                         ‘"request_time": "$request_time", ‘                         ‘"status": "$status", ‘                         ‘"request": "$request", ‘                         ‘"request_method": "$request_method", ‘                         ‘"http_referrer": "$http_referer", ‘                         ‘"body_bytes_sent":"$body_bytes_sent", ‘                         ‘"http_x_forwarded_for": "$http_x_forwarded_for", ‘                         "http_user_agent""$http_user_agent"}                         }‘;

nginx的log是没有自动分割功能的。需要自己写shell脚本分割


Nginx Log Log_format format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.