Nginx customizes how access logs are configured

  • 2020-05-13 04:32:50
  • OfStack

preface

There are two main types of Nginx logs: access logs and error logs. The log switch is set in the Nginx configuration file (/etc/nginx/ nginx.conf). Both logs can be optionally turned off, and both are turned on by default.

The access log mainly records every request made by the client to access Nginx. The format can be customized. Through the access log, you can get the user's geographical source, jump source, user terminal, a certain URL visit and other relevant information.

The importance of logging is self-evident, 1 generally speaking, we will define our own log format and storage path for each project during the development process.

In the case of our normal JAVAWEB project, the important log 1 output is stored in the log directory of Tomcat, and the log output level is distinguished. Used to distinguish, refer to, and count related log information.

Of course, this is not the point, the point is that many companies do not seem to attach much importance to the importance of the log, of course, it may have to do with the platform itself.

The log configuration

In fact, by analyzing the Nginx log, we can get a lot of useful data, such as response time of url, request time, request quantity in each time period and concurrent quantity. Together with the ELK logging system, the system usage can be well presented.

1 in general, common lazy common log configuration, multiple projects common 1 access.log or error.log , resulting in extremely large log files after 1 period of operation, including several G and even several 10G.

Next, I'll focus on the proper configuration of the Nginx log.

Log output format:


# This is going to be configured in http In the 
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

Project configuration:


limit_req_zone $binary_remote_addr $uri zone=api_read:20m rate=50r/s;# The query 
# The report 
server {
 listen 80;
 server_name report.52itstyle.com;
 index login.jsp;
 access_log /usr/local/nginx/logs/report.52itstyle.com.access.log access;
 # Turn off log printing for static files 
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|ico)?$ {
 expires 1d;
 access_log off;
 proxy_pass http://report;
 }
 location / {
 limit_req zone=api_read burst=5;# Request the current limit , Set the queue 
 proxy_pass http://report;
 }
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
}
upstream report {
 fair;
 server 172.16.1.120:8882 weight=1 max_fails=2 fail_timeout=30s;
 server 172.16.1.120:8881 weight=1 max_fails=2 fail_timeout=30s;
}

The above configuration optimizes several points:

Individual projects configure their own log output path files Get rid of unwanted static file access logs

Of course, there are more optimized schemes, such as Nginx log output in date format, but Nginx itself does not support this function, can only be cut through sheel script, interested friends can recommend through the following


Related articles: