LNMP架构 (3) 之 Nginx访问日志、日志切割、静态文件不记录日志和过期时间

1. Nginx访问日志

1.1 日志格式

[root@host ~]# vim /usr/local/nginx/conf/nginx.conf   //搜索log_format
... ...
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
#combined_realip是日志名称 
# 后面带引号和$的是日志内容

1.2 日志格式参数注释

1.3 定义虚拟主机日志格式(一定要先定义nginx.conf中的日志格式,才能在这里调用和再定义)

[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     //定义aaa.com.conf日志格式 
……
access_log /tmp/aaa.com.log combined_realip;
#指定日志位置和格式

检查错误:
[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 检查

[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日志切割

因为Nginx没有自带的日志切割工具,所以需要借助系统日志切割命令或自己编写日志切割脚本。

2.1 日志切割脚本

以后把所有的shell脚本统一保存位置:/usr/local/sbin/

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

#! /bin/bash
d=`date -d "-1 day" +%Y%m%d` 
#定义切割时间(切割一天前的日志)
#此处指定要切割的日志路径(该路径来自虚拟主机配置文件)
nginx_pid="/usr/local/nginx/logs/nginx.pid"
#调用pid的目的是为了执行命令:/bin/kill -HUP `cat $nginx_pid`
#该命令相当于命令:nginx -s reload(重新加载文件),确保与虚拟主机配置文件变更保持同步
#该地址来自nginx配置文件
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
#这里使用通配进行循环,对所有符合条件的日志文件进行切割
/bin/kill -HUP `cat $nginx_pid`
#执行此命令进行重新加载生成新的日志文件来记录新的日志

2.2 执行该脚本

[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

2.3添加到系统任务计划

[root@host vhost]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

0 3 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
#每天凌晨3点切割一次前一天日志

[root@host vhost]# chmod +x /usr/local/sbin/nginx_log_rotate.sh  //加上可执行权限

3. 静态文件不记录日志和过期时间

3.1 核心配置参数

[root@host vhost]# vim test.com.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    #匹配文件类型
    {
          expires      7d;
          #过期时间为7天
          access_log off;
          #不记录该类型文件的访问日志
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          #过期时间为12小时
          access_log off;
          #不记录该类型文件的访问日志
    }
    access_log /tmp/test.com.log combined_realip;
    #指定日志位置及格式

3.2 检测

[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

访问index.html文件
[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"
#有日志

访问baidu.png文件
[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天,即该文件缓存的过期时间为7天!

[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"
#无该文件的访问日志!!!
标签:Nginx 发布于:2019-10-25 05:35:22