Access log for Nginx
The log format of Nginx is in the main configuration file of Nginx (/ usr/local/nginx/conf/nginx.conf)
[root@shuai-01 vhost]# vim /usr/local/nginx/conf/nginx.conf
You can change the log format name to shaui
The meaning of Nginx log field
Define the format of the log in the main configuration file and the log path in the virtual host configuration file.
Open virtual host profile
[root@shuai-01 vhost]# vim test.com.conf access_log /tmp/test.com.log shuai;
Note that ";" should be added after writing a line of Nginx configuration file, otherwise it is an error.
Check the configuration file syntax and reload the configuration file
[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
testing:
[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.2 Date: Mon, 08 Jan 2018 12:41:20 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/index.html [root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.2 Date: Mon, 08 Jan 2018 12:41:26 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/index.html [root@shuai-01 vhost]# cat /tmp/test.com.log 127.0.0.1 - [08/Jan/2018:20:41:20 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 - [08/Jan/2018:20:41:26 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0"
Nginx log cutting
Because nginx does not have its own log cutting tool, it needs to use the log cutting tool of the system or write a log cutting script.
Write a log cutting script by yourself. Script unified save / usr/local/sbin/
Customize a script first:
[root@shuai-01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh #! /bin/bash ## Assume that the log storage path of nginx is / tmp/ 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 #Use general configuration to cycle here, and change the name (cutting is the daily log rename) /bin/kill -HUP `cat $nginx_pid` #Execute this command to overload and generate a new log file to record the new log
Execute script:
[root@shuai-01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh ++ date -d '-1 day' +%Y%m%d + d=20180108 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls test.com.log + for log in '`ls *.log`' + mv test.com.log test.com.log-20180108 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1513
-x: to display the script execution process
be careful:
This is just to cut the log. Deleting the log needs to be used in combination with the task plan cron. Cutting must also be used with cron.
Static files do not log and expire
Add configuration to the configuration file:
Open profile:
[root@shuai-01 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 }
Check the configuration file syntax and reload the configuration file:
[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
Test:
[root@shuai-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif shjdkjhkasb [root@shuai-01 test.com]# curl -x127.0.0.1:80 test.com/2.js ajkfdchb [root@shuai-01 test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@shuai-01 test.com]# cat /tmp/test.com.log 127.0.0.1 - [09/Jan/2018:00:39:45 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"