Forty-eight, Nginx access logs, Nginx log cutting, static files do not record logs and expiration time
1. Nginx access log
log format
# pwd
/usr/local/nginx/conf
[root@MRX conf]# vim nginx.conf
log_format ELA '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
Parse:
combined_realip: The name of the log format, which can be customized.
";": Semicolon, Nginx configuration files are separated by a semicolon, and a section of configuration ends, separated by a semicolon, and a newline without a semicolon is equal to one line.
$remote addr: client IP (public IP)
You can search for "IP" on Baidu to know your public IP.
$http_x_forwarded_for: The IP of the proxy server.
$time_local: Server local time.
$host: Access hostname (domain name).
$request_uri: The URL address of the visit.
$status: Status code.
$http_referer:referer。
$http_user_agent:user_agent。
In addition to defining the log format in the main configuration file nginx.conf, it also needs to be added in the virtual host configuration file.
# pwd
/usr/local/nginx/conf/vhost
[root@MRX vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
access_log /tmp/test.com.log ELA; //If the log format is not written, the default will be used.
Specify the location of the access log here.
}
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
Re-curl test, and then check this log, the 301 can be viewed.
2. Nginx log cutting
Nginx does not have its own log cutting tool, you need to use the system's log cutting tool, or write a script for cutting logs yourself.
Custom shell script:
# vim /usr/local/sbin/nginx_logrotate.sh
//The scripts written later are placed under /usr/local/sbin
#! /bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp" the path address of the log
nginx_pid="/usr/local/nginx/logs/nginx.pid" execute pid to execute the kill line
cd $logdir to enter the log directory
for log in `ls *.log`
do fixed syntax, which means start a loop
mv $log $log-$d //All files from ls after renaming in plus date.
done
/bin/kill -HUP `cat $nginx_pid` The purpose is the same as nginx -s reload
# date -d "-1 day" +%Y%m%d
20180425 generates yesterday's date
[root@MRX conf]# date
Thu Apr 26th 2018 05:06:20 CST
# ls /usr/local/nginx/logs/nginx.pid The pid address must be correct, otherwise the kill command cannot be executed.
举例:# for f in `ls `; do ls -l $f;done
for: loop. for log, with log as its variable.
f: file.
in: in which interval to loop, in `ls` loops in ls.
# sh -x /usr/local/sbin/nginx_logrotate.sh
-x: View its execution process.
++ date -d '-1 day' +%Y%m%d
+ d=20180425
+ logdir=/tmp
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp
++ ls php_errors.log test.com.log
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20180425
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180425
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1023 //If you don't kill -HUP, you can't generate a new file.
# ls /tmp
mysql.sock php_errors.log-20180425 systemd-private-71d2d82f636347309eff2c7a3230eb7f-vgauthd.service-7NpGWg test.com.log
pear php-fcgi.sock systemd-private-71d2d82f636347309eff2c7a3230eb7f-vmtoolsd.service-aM4C0h test.com.log-20180425
After that, one such dated log file is generated every day.
# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
//Do a cleanup for the ones before 30 days, if there are no qualified ones, it will not be cleaned up.
# crontab -e add a task plan
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
3. Static files do not record logs and expiration time
# pwd
/usr/local/nginx/conf/vhost
[root@MRX vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ .*\.(gif|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/test.com.log ELA;
}
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
When these static files are accessed again, no logs are recorded.