1. Nginx access log
1.1 log format
[root@host ~]# vim /usr/local/nginx/conf/nginx.conf / / search for log \ ... ... log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; #Combined? Realip is the log name # The contents of the log are followed by quotes and $
1.2 log format parameter notes
Name | Meaning |
---|---|
$remote_addr | Client IP (public IP) |
$http_x_forwarded_for | IP of the proxy server |
$time_local | Server local time |
$host | Access host name (domain name) |
$request_uri | URL address visited |
$status | Status code |
$http_referer | referer |
$http_user_agent | user_agent |
1.3 define the log format of the virtual host (you must define the log format in nginx.conf before calling and redefining it here)
[root@host ~]# cd /usr/local/nginx/conf/vhost/ [root@host vhost]# ls aaa.com.conf test.com.conf [root@host vhost]# vim aaa.com.conf / / define the format of aaa.com.conf log ...... access_log /tmp/aaa.com.log combined_realip; #Specify log location and format //Check for errors: [root@host vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1.4 check
[root@host vhost]# curl -x127.0.0.1:80 aaacom This is aaa.com [root@host vhost]# cat /tmp/aaa.com.log 127.0.0.1 - [8/Sep/2017:22:39:68 +0800] aaa.com "/" 200 "-" "curl/7.29.0"
2. Nginx log cutting
Because Nginx does not have its own log cutting tool, you need to use the system log cutting command or write your own log cutting script.
2.1 log cutting script
In the future, save all shell scripts in the same location: / usr/local/sbin/
[root@host vhost]# vim /usr/local/sbin/nginx_log_rotate.sh #! /bin/bash d=`date -d "-1 day" +%Y%m%d` #Define cutting time (log one day before cutting) logdir="/tmp/" #The log path to cut (from the virtual host profile) is specified here nginx_pid="/usr/local/nginx/logs/nginx.pid" #The purpose of calling pid is to execute the command: / bin / kill - HUP ` cat $nginx? pid` #This command is equivalent to the command: nginx -s reload to ensure synchronization with changes to the virtual host configuration file #This address is from nginx configuration file cd $logdir for log in `ls *.log` do mv $log $log-$d done #Here, the general configuration is used to cycle and cut all qualified log files /bin/kill -HUP `cat $nginx_pid` #Execute this command to reload and generate a new log file to record the new log
2.2 execute the script
[root@host vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20170909 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls test.com.log yum.log + for log in '`ls *.log`' + mv test.com.log test.com.log-20170909 + for log in '`ls *.log`' + mv yum.log yum.log-20170909 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 59154
3. Static files do not record log and expiration time
3.1 core configuration parameters
[root@host vhost]# vim test.com.conf location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #Match file type { expires 7d; #Expiration time is 7 days access_log off; #Do not log access to files of this type } location ~ .*\.(js|css)$ { expires 12h; #Expires 12 hours access_log off; #Do not log access to files of this type } access_log /tmp/test.com.log combined_realip; #Specify log location and format
3.2 detection
[root@host vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@host vhost]# /usr/local/nginx/sbin/nginx -s reload //Access the index.html file [root@host vhost]# !curl curl -x127.0.0.1:80 test.com This is test.com [root@host vhost]# !cat cat /tmp/test.com.log 127.0.0.1 - [9/Sep/2017:00:12:26 +0800] test.com "/" 200 "-" "curl/7.29.0" #Log //Access baidu.png file [root@host test.com]# curl -x127.0.0.1:80 test.com/baidu.png -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Sat, 9 Aug 2017 09:47:57 GMT Content-Type: image/png Content-Length: 3706 Last-Modified: Sat, 09 Sep 2017 01:13:35 GMT Connection: keep-alive ETag: "59805459-e7a" Expires: Sat, 09 Sep 2017 00:47:46 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes #Max age = 604800s = 7 days, that is, the expiration time of the file cache is 7 days! [root@host test.com]# cat /tmp/test.com.log 127.0.0.1 - [9/Sep/2017:00:13:39+0800] test.com "/" 200 "-" "curl/7.29.0" #No access log for this file!!!