Home  >  Article  >  Operation and Maintenance  >  How to configure Nginx log format

How to configure Nginx log format

WBOY
WBOYforward
2023-05-15 22:43:122321browse

A small reservation program is launched, and the access portal is configured through Nginx. The default log does not have request time, so it needs to be configured to record the access response time of each request for future reference and optimized use.

1. Default log format

The default log format is as follows (the default is commented out, and the system will automatically use it):

    #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  logs/access.log  main;

2. The log format I use

I only added two parameters of response time that I am more concerned about on the basis of the default: request_time and upstream_response_time

Open and modify the following configuration (I used format 2 later, Time is in front, easy to view):

Log format with time data parameters 1

    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"';

    access_log  logs/access.log  main;

Adjusted the display order of time parameters format 2:

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

    access_log  logs/access.log  main;

Note Yes: Both log_format and access_log comments must be released. Only releasing log_format will not take effect.

#After setting it to format 2, you can use the corresponding regular expression to view logs longer than 1 second. The two steps are as follows:

##1.高亮时间数据的正则表达式
tail -f access.log |grep "\[[0-9]\.[0-9][0-9][0-9] [0-9]\.[0-9][0-9][0-9]\]"

##2.大于1秒的日志的正则表达式,即将第一个数字改成[1-9]即可
tail -f access.log |grep "\[[1-9]\.[0-9][0-9][0-9] [0-9]\.[0-9][0-9][0-9]\]"

How to configure Nginx log format

##3. Parameters

Description

  • $remote_addr : Client address

  • $remote_user: Client user name

  • $time_local: Access time and time zone

  • $request: Requested URI and HTTP protocol

  • $status: HTTP request status

  • $body_bytes_sent: File content size sent to the client

  • $http_referer : URL jump source

  • $http_user_agent: User terminal browser and other information

  • $http_host: Request address, that is, the address (IP or domain name) you enter in the browser

  • $request_time: The total time to process the request, including User data reception time

  • $upstream_response_time: The time between establishing the connection and receiving the last byte of the response body from the upstream server

  • $upstream_connect_time: The time spent establishing a connection to the upstream server

  • $upstream_header_time: The time spent establishing the connection and from The time between the upstream server receiving the first byte of the response header

4. Test effect

Default log before modification

127.0.0.1 - - [03/May/2022:12:02:51 0800] "GET /byhsyyfront/byPages/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; /verifyCode/getVerifyCode HTTP/1.1" 200 2553 "http://localhost:8881/byhsyyfront/byPages/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0. 4844.51 Safari/537.36"


Modified log

127.0.0.1 - - [03/May/2022:12:00:47 0800 ] "GET /byhsyyfront/byPages/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36" "- " "0.025" "0.025"
127.0.0.1 - - [03/May/2022:12:00:47 0800] "GET /byhsyyGateway/byhsyySystem/verifyCode/getVerifyCode HTTP/1.1" 200 2178 "http:// localhost:8881/byhsyyfront/byPages/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36" "-" "0.037" "0.037"


You can see that after the modification, there are two more parameter data about time, which can be used for response time analysis.

The above is the detailed content of How to configure Nginx log format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete