Nginx access log access_log Configuration and information (recommended)_nginx_ Script home

Nginx access log access_log Configuration and information (Recommended)

Updated: Apr 01, 2024 at 10:01:58 by strggle_bin
When you set the log level to debug, if you are debugging an online high-traffic website, your error log may record a lot of messages per request, which will become meaningless, the following small series to introduce you to the Nginx access log access_log configuration and information details, interested friends follow the small series to see it

Nginx access log access_log configuration and information

The Nginx access log is controlled by two main parameters: log_format # to define the log format (you can define multiple log formats, take different names) access_log # to specify the path to the day to file and which log format is used to record logs

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

Default value of access_log:

#access_log  logs/access.log  main;

log_format log_format syntax The syntax and parameters are described as follows:

log_format <NAME> <String>; Keyword Format tag Log format Keyword: The keyword error_log cannot change the format tag: Format tag sets a unique name for a set of log formats. Log format: Sets format scope for logs: http

eg:

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;

log_format Format variable:

Parameter Description Example $remote_addr Client address 211.28.65.253 $remote_user Client user name -- $time_local Access time and time zone 18/Jul/2012:17:00:01 +0800 $request Request URI and HTTP protocol "GET/article-10,000.html HTTP/1.1" $http_host request address, The address (IP or domain name) you type in your browser www.wang.com 192.168.100.100 $status HTTP request status 200 $upstream_status upstream status 200 $body_bytes_sent Send to the client file Content Size 1547 $http_referer url Go to the source https://www.baidu.com/ $http_user_agent User terminal browser and other information "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident / 4.0; SV1; GTB7.0; . NET4.0 C; $ssl_protocol SSL protocol version TLSv1 $ssl_cipher Algorithm in data exchange RC4-SHA $upstream_addr upstream address, upstream: Upstream: Upstream: Upstream: Upstream: Upstream: Upstream: Upstream: Upstream: Upstream: Upstream: upstream: upstream: Upstream: Upstream: upstream: upstream: upstream: upstream: upstream: upstream: upstream: upstream: upstream: upstream

access_log

Syntax and parameter syntax are described as follows:

access_log <FILE> <NAME>; Keyword log file format tag Keyword: The keyword error_log cannot change the log file. The log file can be stored in any directory. Format tag: Apply the specified log format to the log file. Disable access_log, that is, no access logs are recorded. access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; Instructions: buffer=size # Indicates the size of the buffer in which the access logs are stored flush=time # Indicates the time when the logs in the buffer are flushed to the disk gzip[=level] # Indicates the compression level [if = condition] # indicates other condition scopes (label segment locations of parameters) : http, server, location, if in location, limit_except

eg:

access_log /spool/logs/nginx-access.log compression buffer=32k;

open_log_file_cache

Use open_log_file_cache to set the log file cache (default is off).

max: Sets the maximum number of file descriptors in the cache. If the cache is fully occupied, the LRU algorithm closes the descriptors. min_uses: Specifies the minimum number of times a log file is used in inactive period. The default value is 10 seconds. valid: specifies the check frequency

Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; Default value: open_log_file_cache off. Scope: http, server, location

eg:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

Reference data: http://nginx.org/en/docs/http/ngx_http_log_module.html

nginx log debugging tips

Set Nginx to only record errors coming from your IP

When you set the log level to debug, if you are debugging an online high traffic website, your error log may record many messages per request, which becomes meaningless.

At events{... Configure the following to make Nginx log errors that only come from your IP address.

events {debug_connection 1.2.3.4; }

Debugging nginx rewrite rules

If the rewrite rule is incorrectly written, only a 404 page will be displayed. You can enable the nginx rewrite log in the configuration file for debugging.

server {
        error_log    /var/logs/nginx/example.com.error.log;
        rewrite_log on;
}

rewrite_log on; When enabled, it sends all log information related to rewrite to the error_log file using the [notice] level. You can then view the rewrite information in error_log.

Use location to log the specified URL

server { error_log /var/logs/nginx/example.com.error.log; location /static/ { error_log /var/logs/nginx/static-error.log debug; }}

Procedure for configuring access logs in Nginx:

(1) Create a log_format statement

worker_processes  1;
error_log logs/error.log error;
events {
    worker_connections  1024;
}
http {
    include status.conf;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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;
    server {
        listen       80;
        server_name  localhost;
                rewrite ^/.* http://www.wl.com permanent;
    }
    include vhost/*.conf;
}

(2) Insert access_log statement

server { access_log /data/log/www; listen 80; server_name abc.com www.wl.com; location / { root /data/www/www; index index.html index.htm; } error_log logs/error_www.wl.com.log error; access_log logs/access_www.wl.com.log main; # New Content ↑}

(3) Restart the service

nginx -t
nginx -s reload

Common example

main format

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                       '$upstream_addr $upstream_response_time $request_time ';
access_log  logs/access.log  main;

json format

log_format logstash_json '{"@timestamp":"$time_iso8601",'
       '"host": "$server_addr",'
       '"client": "$remote_addr",'
       '"size": $body_bytes_sent,'
       '"responsetime": $request_time,'
       '"domain": "$host",'
       '"url":"$request_uri",'
       '"referer": "$http_referer",'
       '"agent": "$http_user_agent",'
       '"status":"$status",'
       '"x_forwarded_for":"$http_x_forwarded_for"}';

Explanation:

The current URI in the request (with no request parameters, parameter is located in the uri request), unlike the value of $request_uri passed by the browser, it can be redirected internally. Or use the index directive to modify it. The protocol and host name, such as /foo/bar.html, are not included. requesturi This variable is equal to the original uri containing some client request parameters, which cannot be modified, please see request-_URI this variable is equal to the original URI containing some client request parameters, which cannot be modified, See requesturi This variable is equal to the original URI that contains some of the client request parameters. It cannot be modified. See the uri changed or the URI rewritten. In other words, requesturi is the original request url, request_uri is the original request URL, requesturi is the original request URL, and uri is the URL whose parameters are deleted after nginx processes the request. Therefore, the Chinese character will be represented as union. Pit Point: Urls can be changed or rewritten in nginx using URIs, but for log output, urls can be changed or rewritten in nginx using URIs. However, you can use request_uri for log output. If there is no special service requirement, use the request_URI instead.

Compressed format

Compressed information is added to the log.

http { log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; server { gzip on; access_log /spool/logs/nginx-access.log compression; ... }}

upstream format

Increased upstream consumption time.

http { log_format upstream_time '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"' 'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"'; server { access_log /spool/logs/nginx-access.log upstream_time; ... }}

Statistical information

Calculates the number of times status occurs

awk '{print $9}' access.log | sort | uniq -c | sort -rn
36461 200 
483 500
87 404
9 400
3 302
1 499
1 403
1 301

Displays the URL that returns the 302 status code.

awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn 1 /wp-login.php 1 /wp-admin/plugins.php? action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131 1 /wp-admin/

To this article about the Nginx access log access_log configuration and detailed information is introduced to this, more related to Nginx access log access_log content please search the previous articles of the script home or continue to browse the following related articles hope that you will support the script home in the future!

Related article

Latest comments