Detail the logging configuration in the Nginx server

  • 2020-05-14 05:33:48
  • OfStack

There are two main instructions related to nginx log,
log_format, which sets the log format,
access_log, which specifies the location, format, and cache size of the log files

log_format format
log_format name (format name) format style (that is, what kind of log content you want to get)


Default example:


log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_s ent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"']

# comment:
$remote_addr and $http_x_forwarded_for are used to record the ip address of the client;
$remote_user: used to record the client user name;
$time_local: to record access time and time zone;
$request: the url and http protocols used to record requests;
$status: used to record request status; Success is 200,
$body_bytes_s ent: record sent to the client file body content size;
$http_referer: used to record visits from that page link;
$http_user_agent: record client, browser information;

Usually the web server is placed behind the reverse proxy, so the client's IP address cannot be obtained. The IP address obtained through $remote_add is the iP address of the reverse proxy server. The reverse proxy server can add the x_forwarded_for information in the http header information of forwarding the request, so as to record the IP address of the original client and the server address requested by the original client.


log_format mylogformat ' $http_x_forwarded_for- $remote_user [$time_local] '
' "$request" '$status $body_bytes_s ent '
' "$http_referer" "$http_user_agent" ';

Use access_log command log file to store the path;
After using the log_format instruction to set the log format, it is necessary to use the access_log instruction to specify the storage path of log files.
access_log path(store path) format (custom log name)



# The sample :
access_log logs/access.log main;
 
# We use log_format  Defines the 1 a mylogformat The log   We could write it like this 
access_log logs/access.log mylogformat;
 
# If you don't want to enable logging  :
access_log off ;


In defining the log directory, it should be noted that the user and group set by the nginx process must have the permission to create files in this path. If the user name and group set by nginx's usr directive are www, and the user name and group set by logs are root, then the log file will not be created.

Nginx log cutting script


#!/usr/bin/env python
 
import datetime,os,sys,shutil
 
log_path = '/alidata/log/nginx/access/'
log_file = 'www.wpython.com.log'
 
yesterday = (datetime.datetime.now() - datetime.timedelta(days = 1))
 
try:
 os.makedirs(log_path + yesterday.strftime('%Y') + os.sep + \
    yesterday.strftime('%m'))
 
except OSError,e:
 print
 print e
 sys.exit()
 
 
shutil.move(log_path + log_file,log_path \
   + yesterday.strftime('%Y') + os.sep \
   + yesterday.strftime('%m') + os.sep \
   + log_file + '_' + yesterday.strftime('%Y%m%d') + '.log')
 
 
os.popen("sudo kill -USR1 `cat /alidata/server/nginx/logs/nginx.pid`")


Related articles: