Detailed log-related configuration in Nginx server _nginx

Source: Internet
Author: User
Tags nginx server

Nginx Log related instructions are mainly two,
Log_format, used to set the log format,
Access_log, used to specify the path, format, and cache size of the log file

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


The default example:

Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" "$status $body _bytes_s ent" $http _referer
" c3/> ' "$http _user_agent" "$http _x_forwarded_for"]

#注释:
$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: Used to record access time and time zone;
$request: The URL used to record the request and the HTTP protocol;
$status: Used to record request status; Success is 200,
$body _bytes_s ent: Records are sent to the client file body content size;
$http _referer: Used to record links from the page to visit;
$http _user_agent: Record the client, the browser related information;

Usually the Web server is placed behind the reverse proxy, so that the client's IP address cannot be obtained, and the IP address obtained through $remote_add is the IP address of the reverse proxy server. In the HTTP header information of the forwarding request, the reverse proxy server can increase the X_forwarded_for information to record the IP address of the original client and the server address of the original client's request;

Log_format Mylogformat ' $http _x_forwarded_for-$remote _user [$time _local] '
"$request" ' $status $body _bytes_s ent "
$http _referer" "$http _user_agent";

Using the access_log instruction 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 the log file;
Access_log path (store paths) format (custom log name)


#示例:
access_log logs/access.log main;
 
#我们用log_format defines a mylogformat log we can write this
access_log logs/access.log Mylogformat;
 
#如果不想启用日志:
access_log off;


Note In the definition log directory that the the users and groups set by the Nginx process must have permission to create files on the path, assuming that the user name and user group set by the nginx USR directive are www, and the logs directory's username and group is root, then the log file cannot 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 '")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.