LNMP Architecture - Nginx access log, log cutting, static file does not record log and expiration time

Nginx access log

Modify profile

[root@dl-001 vhost]# vim /usr/local/nginx/conf/nginx.conf
// Search / log? Format
// In nginx, it ends with; as a line, so the following code is a configuration
// Format: log format log format name format

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

The variables used in the format are described as follows:

Define log path

[root@dl-001 vhost]# vim test.com.conf
# Insert in server block
access_log /tmp/test.com.log test;

# The format is access log path log format name (defined in the main configuration file)

Detect and reload

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

Detection effect

[root@dl-001 vhost]# curl -x 127.0.0.1:80 test.com

[root@dl-001 vhost]# curl -x 127.0.0.1:80 test2.com/index.php

[root@dl-001 vhost]# curl -x 127.0.0.1:80 test2.com/index1.php


[root@dl-001 vhost]# cat /tmp/test.com.log 
127.0.0.1 - [03/Jan/2018:19:05:22 +0800] test.com "/" 401 "-" "curl/7.29.0"
127.0.0.1 - [03/Jan/2018:19:05:34 +0800] test2.com "/index.php" 301 "-" "curl/7.29.0"
127.0.0.1 - [03/Jan/2018:19:05:39 +0800] test2.com "/index1.php" 301 "-" "curl/7.29.0"

Nginx log cutting

Because Nginx does not have its own log cutting tool, you need to use the system log cutting command or custom script.

Create custom script

[root@dl-001 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@dl-001 vhost]# cat /usr/local/sbin/nginx_log_rotate.sh
#!/bin/bash

// date +%Y%m%d shows today's date
// Add -d "-1 day" Show yesterday's date
d=`date -d "-1 day" +%Y%m%d`

// Define the path of log storage, defined in the virtual host configuration file
logdir="/tmp"

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

cd $logdir

// Cycle to change the log file name under the log storage path
for log in `ls *.log`
do
    mv $log $log-$d
done

// Restart without shutting down the process, equivalent to nginx -s reload
/bin/kill -HUP `cat $nginx_pid`

Test script

[root@dl-001 vhost]# SH - X / / SH - x can display the process of script execution / usr / local / SBIN / nginx ﹣ log ﹣ rotate.sh 
++ date -d '-1 day' +%Y%m%d
+ d=20180102
+ 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-20180102
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1299


[root@dl-001 vhost]# ls /tmp/*.log * / / check whether the function is implemented
/tmp/test.com.log  /tmp/test.com.log-20180102

Periodic execution with crontab command

[root@dl-001 vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

Static files do not log and expire

Modify virtual host profile

[root@dl-001 vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf 
# ~Matching subsequent regular representations
# Use \ escape. To match. jpg and other files
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        # expires set expiration time
        expires 7d;

        # Turn off logging
        access_log off;
    }

location ~ .*\.(css|js)$
    {
        expires 12h;
        access_log off;
    }

test

  • 1. Verify that static files do not log
[root@dl-001 vhost]# curl -x 127.0.0.1:80 test.com/index.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

[root@dl-001 vhost]# curl -x 127.0.0.1:80 test.com/1.gif
jhasdifhoai

[root@dl-001  vhost]# curl -x 127.0.0.1:80 test.com/2.js
abdsuofghan

// gif/js file log does not record access log
[root@dl-001 vhost]# cat /tmp/test.com.log
127.0.0.1 - [03/Jan/2018:19:36:25 +0800] test.com "/index.php" 401 "-" "curl/7.29.0"
  • 2 validation expiration time
[root@dl-001 vhost]# curl -x 127.0.0.1:80 test.com/1.gif -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 03 Jan 2018 11:36:52 GMT
Content-Type: image/gif
Content-Length: 12
Last-Modified: Wed, 03 Jan 2018 11:35:29 GMT
Connection: keep-alive
ETag: "5a4cc001-c"
Expires: Wed, 10 Jan 2018 11:36:52 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
  • Test js file expiration time
[root@dl-001 vhost]# curl -x 127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 03 Jan 2018 11:36:58 GMT
Content-Type: application/javascript
Content-Length: 12
Last-Modified: Wed, 03 Jan 2018 11:35:44 GMT
Connection: keep-alive
ETag: "5a4cc010-c"
Expires: Wed, 03 Jan 2018 23:36:58 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
  • Test PHP file, no max age information
[root@dl-001 vhost]# curl -x 127.0.0.1:80 test.com/index.php
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 03 Jan 2018 11:46:58 GMT
Content-Type: application/octet-stream
Content-Length: 19
Last-Modified: Wed, 03 Jan 2018 11:36:44 GMT
Connection: keep-alive
ETag: "5a4e1572-13"
Accept-Ranges: bytes

Tags: Nginx curl PHP vim

Posted on Sun, 03 May 2020 04:47:30 -0400 by lth2h