In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will take you to understand the specific method of Nginx log configuration. The knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "what is the specific method of configuring Nginx logs?"
Nginx logs are useful for statistics and system service troubleshooting. There are many kinds of Nginx logs, mainly divided into two types: access_log (access log) and error_log (error log). By accessing the log, we can get the user's IP address, browser information, request processing time and other information. The error log records the information of access errors, which can help us locate the cause of the error. Let's explain how to configure Nginx log in detail.
Nginx log configuration
Nginx has a very flexible logging mode. Each level of configuration can have its own independent access log. The log format is defined by the log_format command. Ngx_http_log_module is used to define the request log format.
Access_log instruction access_log path [format [buffer=size [flush=time]; access_log path format gzip [= level] [buffer=size] [flush=time]; access_log syslog:server=address [, parameter=value] [format]; access_log off;# default value: access_log logs/access.log combined;# configuration segment: http, server, location, if in location, limit_except#gzip compression level. # buffer sets the memory cache size. The maximum time for # flush to be kept in the cache. # No logging: access_log off;# uses the default combined format to log: access_log logs/access.log or access_log logs/access.log combined;log_format instruction syntax: log_format name string... ; default value: log_format combined "…" ; configuration segment: http
Name represents the format name, and string represents the equivalent format. Log_format has a default combined log format that does not need to be set, which is equivalent to apache's combined log format, as follows:
Log_format combined'$remote_addr-$remote_user [$time_local]'"$request" $status $body_bytes_sent''"$http_referer"$http_user_agent"'
If the nginx is located in the load balancer, after the squid,nginx reverse proxy, the web server cannot directly obtain the real IP address of the client. $remote_addr gets the IP address of the reverse proxy. The reverse proxy server can add X-Forwarded-For information to the http header information of the forwarding request, which is used to record the client IP address and the server address requested by the client.
Log_format porxy'$http_x_forwarded_for-$remote_user [$time_local]'"$request" $status $body_bytes_sent''"$http_referer"$http_user_agent"'
The log format allows you to include variable comments as follows
$remote_addr, $http_x_forwarded_for record client IP address $remote_user record client user name $request record request URL and HTTP protocol $status record request status $bytes sent to the client by body_bytes_sent, excluding the size of the response header; this variable is compatible with the "% B" parameter in the Apache module mod_log_config. The total number of bytes sent by $bytes_sent to the client. Serial number of the $connection connection. The number of requests currently obtained by $connection_requests through a connection. $msec log write time. The unit is seconds and the precision is milliseconds. Pipe if the request is sent through the HTTP pipeline (pipelined), the PIP value is "p", otherwise it is ".". $http_referer records the page link from which the $http_user_agent records the client browser related information $request_length request length (including the request line, request header, and request body). Request_time request processing time, in seconds, precision milliseconds, starting from reading the first byte of the client until the last character is sent to the client for log writing. Local time in the time_iso8601 ISO8601 standard format. Local time under the $time_local universal log format. The response header sent to the client has the prefix "sent_http_". For example, $sent_http_content_range.
Examples are as follows:
Http {log_format main'$remote_addr-$remote_user [$time_local] "$request"'"$status" $body_bytes_sent = "$http_referer"'"$http_user_agent"$http_x_forwarded_for"''"$gzip_ratio" $request_time $bytes_sent $request_length' Log_format srcache_log'$remote_addr-$remote_user [$time_local] "$request"'"$status" $body_bytes_sent $request_time $bytes_sent $request_length'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]'; open_log_file_cache max=1000 inactive=60s;server {server_name ~ ^ (www\.)? (. +) $ Access_log logs/$2-access.log main; error_log logs/$2-error.log; location / srcache {access_log logs/access-srcache.log srcache_log;}} open_log_file_cache instruction syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; default: open_log_file_cache off; configuration segment: http, server, location
For each log record, the file will be opened, the log will be written, and then closed. You can use open_log_file_cache to set the log file cache (default is off) in the following format:
The parameters are annotated as follows:
Max: sets the maximum number of file descriptors in the cache and closes the descriptor using the LRU algorithm if the cache is full. Inactive: set survival time. Default is 10smin_uses: set in inactive period, after the minimum number of log files are used, the log file descriptor is recorded in the cache. Default is 1 valid: set check frequency, default 60soff: disable cache
Examples are as follows:
Open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;log_not_found instruction syntax: log_not_found on | off; default value: log_not_found on; configuration segment: http, server, location
Whether to log errors that do not exist in error_log. The default is.
Log_subrequest instruction syntax: log_subrequest on | off; default value: log_subrequest off; configuration segment: http, server, location
Whether to record the access log of child requests in access_log. It is not recorded by default.
Rewrite_log instruction description: provided by the ngx_http_rewrite_module module. Used to record rewrite logs. It is recommended to turn on debugging rewriting rules. Syntax: rewrite_log on | off; default: rewrite_log off; configuration segment: http, server, location, if
When enabled, notice-level rewrite logs are recorded in error log.
Error_log instruction syntax: error_log file | stderr | syslog:server=address [, parameter=value] [debug | info | notice | warn | error | crit | alert | emerg]; default value: error_log logs/error.log error; configuration segment: main, http, server, location
Configure the error log.
Thank you for your reading, the above is "what is the specific method of Nginx log configuration" all the content, learn friends hurry up to operate it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.