12.10-12.12 Nginx access logs, log cutting, static files do not record logs and expiration time

12.10 Nginx access log

12.11 Nginx log cutting

12.12 Static files do not log and expire



12.10 Nginx access log


1 Open the configuration file, search for /log_format, and view the log file format. Or directly grep filter out

[root@AliKvn vhost]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf

   log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';

blob.png

2 where combined_realip is the log format name, which can be customized.

#vim nginx.cnf 

blob.png

blob.png

blob.png

3 In addition to defining the log format in the main configuration file nginx.conf, it also needs to be added to the virtual host configuration file.

[root@AliKvn vhost]# vim test.com.conf 

 access_log /tmp/test.com.log our;

4 -t && -s reload check and reload

[root@AliKvn 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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

5 curl test

blob.png

6 Access logs

blob.png



12.11 Nginx log cutting

1 Custom shell script

 vim /usr/local/sbin/nginx_log_rotate.sh //Write the following content

shell script format

#! /bin/bash
## Suppose the log storage path of nginx is /data/logs/
d=`date -d "-1 day" +%Y%m%d` 
logdir="/tmp/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`


parameter parsing

d=`date -d "-1 day" +%Y%m%d`  

The format here is the current time, minus 1 day, which means when today is yesterday.

The log is generally generated at 0:00 am the next day, so cutting the log must be yesterday's time.

%Y%m%d` is the format of the time, representing year, month, day.

logdir="/tmp/"

The path where the log is located, it should be /tmp/ here, because test.com.log is in tmp/

nginx_pid="/usr/local/nginx/logs/nginx.pid"

The purpose of listing the nginx_pid path is to cooperate with the execution of the -HUP `cat $nginx_pid` variable,

/bin/kill -HUP `cat $nginx_pid` is equivalent to nginx -s reload reload function.

cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done

(cd $logdir) into $logdir(/tmp/),

Then ls see which .log (*.log) files are there, and rename all log files,

The name format is, yesterday's date with the suffix .log(mv $log $log-$d),

Refer to the format of for:

for f in `ls `; do ls -l $f; done

   where file is ls sequence`;  

[root@AliKvn vhost]# for f in `ls `; do ls -l $f; done

-rw-r--r-- 1 root root 142 Apr 24 16:58 aaa.com.conf

-rw-r--r-- 1 root root 419 Apr 25 16:18 test.com.conf


2 Execute the script

[root@AliKvn vhost]# vim  /usr/local/sbin/nginx_log_rotate.sh

[root@AliKvn tmp]# sh -x  /usr/local/sbin/nginx_log_rotate.sh

++ date -d '-1 day' +%Y%m%d

+ d=20180424

+ 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-20180424

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -HUP 17745


3 Clean up the logs 30 days ago (this depends on the actual environment, and if you test, you don't need to do scheduled tasks)

find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm  



4 Task plan, let it execute the script at 0:00 am every day (this depends on the actual environment, and if you test, you don't need to do the scheduled task)

#crontab -e

 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh


12.12 Static files do not log and expire

outline

blob.png

1 The configuration is as follows

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }


parameter parsing,

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

location ~ matches the request, followed by the specified corresponding static file.

\.(gif|jpg|jpeg|png|bmp|swf)$ 

\Defense, \. suffix, () is the file type, | means or.

2 -t && -s reload Check syntax and reload

[root@AliKvn 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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

3 curl test

Ready to work:

touch 1.gif and 2.js 

Type anything 1111 222 respectively

test

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/1.gif

111111111111111111111111111111111

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/2.js

2222

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/index.html

test.com

4 Access logs

[root@AliKvn test.com]# cat /tmp/test.com.log

127.0.0.1 - [25/Apr/2018:17:56:32 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

Revisiting non-existing page, 404 page

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/2.js11111111

<html>

<head><title>404 Not Found</title></head>

<body bgcolor="white">

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.8.0</center>

</body>

</html>

[root@AliKvn test.com]# !cat

cat /tmp/test.com.log

127.0.0.1 - [25/Apr/2018:17:56:32 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [25/Apr/2018:18:01:04 +0800] test.com "/2.js11111111" 404 "-" "curl/7.29.0"

The access log records can see that the information of curl 1.gif and 2.js is filtered out,

And other static element files that are not marked are recorded in this access record.

5 Test expiration time

blob.png

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845293&siteId=291194637