Nginx log file access_log Detailed logs and split

Module ngx_http_log_module

nginx log related instructions are mainly two,

Is a log_format , used to set the log format,
the other one is access_log , used to store the path, the format and size for the log file cache.
Popular understanding is first with log_format custom log format they want to use, then log_format name when using custom log access_log then defined followed;

1, log_format format

log_format name (format name) string (format style that is what you want to get the contents of the log)

Example:

log_format Access ' $ REMOTE_ADDR - $ REMOTE_USER [$ time_local] "$ Request" "$ REQUEST_TIME" $ Status $ body_bytes_sent "$ HTTP_REFERER" "$ HTTP_USER_AGENT" $ HTTP_X_FORWARDED_FOR ' ; 

Note: 
$ REMOTE_ADDR: and $ HTTP_X_FORWARDED_FOR for recording the client ip address; 
$ REMOTE_USER: used to record the client user name; 
$ time_local: used to record the access time and time zone; 
$ request: request for the recording mode and url http-; 
$ REQUEST_TIME: used to record the time of the request; 
$ Status: It used to record the status of the request; success is 200, 
$ body_bytes_sent: records are sent to the client the main content file size; 
$ HTTP_REFERER: used to record the access link from that page over; 
$ HTTP_USER_AGENT: record information about clients poison ah browser.

Usually placed behind a reverse proxy web server, so that you can not get to the customer's IP address, and by $ remote_add get the IP address of the reverse proxy server iP address.

Reverse proxy server http header information transfer request, may increase x_forwarded_for information used to record the server address and the IP address of the client requesting the client

2, storage paths access_log command log file ;
followed by the log_format instruction set log format, specify the storage path to the log file with the access_log instruction;
access_log path (storage path) access (name of the custom log name, and log_format disposed coincide )

示例:
access_log logs/access.log access;

Note:
In the custom log directory to be noted that the users and groups nginx process set must have the path to create access to the file,
assuming the user name and user group of nginx usr instruction set is a user name and www, while logs directory group is root, then the log file will not be created.

After doing front-end proxy by nginx, we found HTTP_X_FORWARDED_FOR can not get to the real client IP address of the.

 

The reason nginx default does not increase X_FORWARDED_FOR header information, plus we give him enough. Simple configuration is as follows:

location /   
{   
    proxy_pass          http://www.xxx.com;   
    proxy_set_header    Host             $host;   
    proxy_set_header    X-Real-IP        $remote_addr;   
    proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;    //别忘了这一句   
    proxy_set_header    HTTP_X_FORWARDED_FOR $remote_addr;              //或是加上这一句   
    proxy_redirect      default;   
}

After the restart nginx load the new configuration, you can get the real client IP address of the

 

3 . Log Cutting:
Log achieve cutting by:

vim /opt/cut_nginx.sh
#!/bin/bash
#切割日志
datetime=$(date -d "-1 day" "+%Y%m%d") log_path
="/usr/local/nginx/logs" pid_path="/usr/local/nginx/logs/nginx.pid" [ -d $log_path/backup ] || mkdir -p $log_path/backup if [ -f $pid_path ] then mv $log_path/access.log $log_path/backup/access.log-$datetime kill -USR1 $(cat $pid_path) find $log_path/backup -mtime +30 | xargs rm -f \\mtime :文件被修改时间 atime: Access time (the time the file was last accessed database) ctime : change the time (metadata file changes such as permissions, ownership, and so on.) The else echo " Error, Nginx IS not Working! " | TEE -a / var / log / messages Fi

chmod +x /opt/cut_nginx.sh

set the timer task crontab -e

0  0  *  *  *     /opt/cut_nginx.sh

[root@localhost ~]# /opt/cut_nginx_log.sh

[root@localhost ~]# ls /usr/local/nginx/logs/backup/

access.log-20161117

[root@localhost ~]# killall -9 nginx

[root@localhost ~]# /opt/cut_nginx_log.sh

Error,Nginx is not working!

[root@localhost ~]# tail -1 /var/log/messages

Error,Nginx is not working!

 

Guess you like

Origin www.cnblogs.com/canflyfish/p/11568588.html