“Reader welfare! 2 TB of all kinds of technical resources for free”

preface

Nginx logs are useful for statistics and system service troubleshooting.

There are two main types of Nginx logs: access_log(access log) and error_log(error log). Through the access log we can get the user’s IP address, browser information, request processing time and other information. Error logs record information about access errors and help us locate the cause of the error.

This article describes in detail how to configure Nginx logging.

Set the access_log

Access logs record client requests. Every request a client makes to the Nginx server is logged here. The client IP, browser information, referer, request processing time, request URL, and so on can all be obtained in the access log. Of course, you can specify what information to record using the log_format directive.

grammar

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; Access_log off; # Disable access loggingCopy the code
  • Path Specifies the log storage location.

  • Format Specifies the log format. The default is the predefined combined.

  • The buffer is used to specify the size of the cache for log writing. The default is 64K.

  • Gzip logs are compressed before being written. The compression ratio can be specified from 1 to 9. The higher the compression ratio is, the slower the compression speed is. The default is 1.

  • Flush Sets the validity period of the cache. If the time specified by Flush is exceeded, the cache is cleared.

  • If condition judgment. If the specified condition evaluates to 0 or an empty string, the request is not logged.

In addition, there is a special value off. If this value is specified, all request logging in the current scope is turned off.

scope

The access_log directive can be applied to HTTP, server, location, limit_except scopes. That is, Nginx will report an error if the directive is used outside these scopes.

This is the basic syntax and the meaning of the parameters of the access_log directive. Let’s look at a couple of examples to get a better understanding.

Basic usage

access_log /var/logs/nginx-access.logCopy the code

This example specifies the log write path to /var/logs/nginx-access.log, using the default format of combined.

access_log /var/logs/nginx-access.log buffer=32k gzip flush=1mCopy the code

In this example, the path for writing logs is /var/logs/nginx-access.log, the default format is combined, the cache size is set to 32K, gzip is enabled for compression before writing logs, the compression ratio is set to 1, and the valid cache time is set to 1 minute.

Use log_format to customize log formats

Nginx predefines a log format called combined, which is used by default if no explicit log format is specified:

log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer"  "$http_user_agent"';Copy the code

If you don’t want to use Nginx’s predefined formats, you can customize them via the log_format directive.

grammar

log_format name [escape=default|json] string ... ;Copy the code
  • Name Format name. Referenced in the access_log directive.

  • Escape sets whether the character encoding in the variable is JSON or default, which is default.

  • String Specifies the log format content to be defined. There can be more than one parameter. Parameters can use Nginx variables.

Here are some variables commonly used in the log_format directive:

$byTES_SENT Total number of bytes sent to the client $body_BYTES_SENT Number of bytes sent to the client, not including the size of the response header $Connection Connection serial number $CONNECtion_requests Number of requests currently sent over the connection $msec Log write time, $request_length Specifies the length of the request (including the line, header, and body). $request_Time Specifies the length of the request to be processed, in seconds, with an accuracy of milliseconds. From the first byte read into the client, $status Response status code $TIME_ISO8601 Local time in standard format, such as "2017-05-24T18:31:27+08:00" $TIME_local Local time in common log format. For example, "24/May/2017:18:31:27 +0800"$http_referer Specifies the requested referer address. $http_user_agent Specifies the browser information of the client. $remote_ADDR Client IP$HTTP_X_forwarded_for This parameter takes effect only when the x_forwarded_for is configured for the proxy server. $request Complete original request line, such as "GET/HTTP/1.1"$remote_user Client user name, for user authentication enabled requests $request_uri Complete request address, such as "https://daojia.com/"Copy the code

Here is a demonstration of the use of custom log formats:

access_log /var/logs/nginx-access.log mainlog_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                  '$status $body_bytes_sent "$http_referer" '                  '"$http_user_agent" "$http_x_forwarded_for"';Copy the code

We define a format of main using the log_format directive and refer to it in the access_log directive. If the client makes a request: https://suyunfe.com/, let’s take a look at the log of a request I intercepted:

112.195.209.90 - - [20/Feb/2018:12:12:14 +0800] "GET/HTTP/1.1" 200 190 "-" Mozilla/5.0 (Linux; The Android 6.0. Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36" "Copy the code

We see that $remote_user, $http_referer, and $http_x_forwarded_for all correspond to a – in the final log because these variables are empty.

Set the error_log

Error logging is implemented in Nginx using the error_log directive. This directive logs error messages during server and request processing.

grammar

The path and log level of the log file were incorrectly configured.

error_log file [level]; Default: error_log logs/error.log error;Copy the code

The first parameter specifies where the log is written.

The second parameter specifies the level of logging. Level can be any value of DEBUG, INFO, notice, WARN, error, crit, Alert, or emerg. You can see the range of values in order of urgency from lowest to highest. Only logs whose error level is equal to or higher than the value specified by level are recorded in error logs. The default value is error.

Basic usage

error_log /var/logs/nginx/nginx-error.logCopy the code

It can be configured in the main, HTTP, mail, stream, server, and location scopes.

In this example, the error log path is /var/logs/nginx-nginx-error. log. The default error log level is used.

open_log_file_cache

Each log record is written by opening the file, then writing the record, and then closing the log file. If you use variables such as access_log /var/logs/$host/nginx-access.log in your log file path, you can use the open_log_file_cache directive to set the cache of log file descriptors to improve performance.

grammar

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];Copy the code
  • Max sets the maximum number of file descriptors that can be held in the cache. If they are full, the LRU algorithm is used to close the descriptor.

  • Inactive Sets the cache lifetime. Default is 10 seconds.

  • Min_uses Specifies the minimum number of times a log file is used in an inactive period. The log file descriptor is cached. The default is 1.

  • Valid: Sets how long the log file name is checked to see if it has changed. The default value is 60s.

  • Off: No cache is used. The default value is off.

Basic usage

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

It can be configured in the HTTP, server, and Location scopes.

In the example, set the cache to cache a maximum of 1000 log file descriptors. If the log file descriptors in the cache are accessed for at least two times within 20 seconds, the cache will not be closed. Check the file descriptor file name in the cache every 1 minute to see if it still exists.

conclusion

Nginx uses the access_log and error_log directives to configure access logs and error logs. Log_format allows you to customize log formats. If variables are used in the log file path, we can use the open_log_file_cache directive to set the cache to improve performance.

In addition, in the access_log and log_format USES a lot of variables, these variables are not listed, detailed variable information can refer to Nginx official documentation: http://nginx.org/en/docs/varindex.html

Author: antwang

juejin.im/post/5aa09bb3f265da238f121b6c

You will receive a book entitled "Distributed Middleware Technology Practice" as a gift. The deadline is 23:00 on February 10, 2020.

Key words: 1024 You can get a copy of the latest finishing 2TB technology dry goods: Including system operation and maintenance, database, Redis, MogoDB, e-book, Java foundation course, Java practice project, architect comprehensive tutorial, architect practice project, big data, Docker container, ELK Stack, machine learning, BAT interview intensive video, etc.

Being is the END being

Excellent article recommendation:


Pay treasure breach of contract temporarily does not calculate broken promise! Netizen: conscience!

Microsoft pulls another stunt! Half a billion users are homeless, putting 170 million Chinese at great risk

Wechat online new functions, just 30S, direct dialogue with The State Council!

Huawei went back to work on February 3, which was ridiculed by netizens

High risk vulnerability! Sudo can be used by hackers to gain root privileges

Click [read article] to find out more

Please click here ↓↓↓

Copy the code