11月27日任务
12.10 Nginx访问日志
12.11 Nginx日志切割
12.12 静态文件不记录日志和过期时间
Nginx访问日志
- 修改nginx配置文件
[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf
# 搜索:/log_format
# 在nginx中以;作为一行的结尾,所以下列代码时一个配置
# 格式:“log_format 日志格式名 格式;”
log_format test '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
格式内使用的变量说明如下:
变量名 | 说明 |
---|---|
$remote_addr | 客户端ip(公网ip) |
$http_x_forwarded_for | 代理服务器的ip |
$time_local | 服务器本地时间 |
$host | 访问主机名(域名) |
$request_uri | 访问的url地址 |
$status | 状态码 |
$http_referer | referer |
$http_user_agent | user_agent |
- 在虚拟主机内定义日志路径
# 在server块内插入
access_log /tmp/test.com.log test;
# 格式为access_log 日志存放路径 日志格式名(主配置文件内定义的)
- 重启服务
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
- 验证效果
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com
[root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index.php
[root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index1.php
# 成功记录
[root@localhost vhost]# cat /tmp/test.com.log
127.0.0.1 - [...:19:05:22 +0800] test.com "/" 401 "-" "curl/7.29.0"
127.0.0.1 - [...:19:05:34 +0800] test2.com "/index.php" 301 "-" "curl/7.29.0"
127.0.0.1 - [...:19:05:39 +0800] test2.com "/index1.php" 301 "-" "curl/7.29.0"
Nginx日志切割
nginx没有apache内的rotatelog
日志切割命令,我们可以通过自定义shell脚本来实现日志切割的功能。
- 创建自定义脚本
[root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@localhost vhost]# cat /usr/local/sbin/nginx_log_rotate.sh
#!/bin/bash
# date +%Y%m%d 显示的是今天的日期
# 加上 -d "-1 day" 显示的是昨天的日期
d=`date -d "-1 day" +%Y%m%d`
# 定义日志存放的路径,虚拟主机配置文件内定义
logdir="/tmp"
# pid文件
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
# 在日志存放路径下循环更改日志文件名
for log in `ls *.log`
do
mv $log $log-$d
done
# 在不关闭进程前提下重启,等价于nginx -s reload
/bin/kill -HUP `cat $nginx_pid`
- 脚本测试
# sh -x 可以显示脚本执行的过程
[root@localhost vhost]# sh -x /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@localhost vhost]# ls /tmp/*.log*
/tmp/test.com.log /tmp/test.com.log-20180102
- 配合crontab命令周期性执行
[root@localhost vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
静态文件不记录日志和过期时间
- 修改虚拟主机配置文件
[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
# ~ 匹配后续的正则表示
# 使用\转义.,匹配.jpg等文件
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
# expires设置过期时间
expires 7d;
# 关闭日志记录
access_log off;
}
location ~ .*\.(css|js)$
{
expires 12h;
access_log off;
}
- 测试
- 验证静态文件不记录日志
[root@localhost 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@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif
jhasdifhoai
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js
abdsuofghan
# gif/js文件日志未记录访问日志
[root@localhost vhost]# cat /tmp/test.com.log
127.0.0.1 - [...:19:36:25 +0800] test.com "/index.php" 401 "-" "curl/7.29.0"
- 验证过期时间
- 测试gif的过期时间
[root@localhost 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, ... 11:36:52 GMT
Content-Type: image/gif
Content-Length: 12
Last-Modified: Wed, ... 11:35:29 GMT
Connection: keep-alive
ETag: "5a4cc001-c"
Expires: Wed, ... 11:36:52 GMT
# 信息内显示max-age时间
Cache-Control: max-age=604800
Accept-Ranges: bytes
- 测试js文件的过期时间
[root@localhost 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, ... 11:36:58 GMT
Content-Type: application/javascript
Content-Length: 12
Last-Modified: Wed, ... 11:35:44 GMT
Connection: keep-alive
ETag: "5a4cc010-c"
Expires: Wed, ... 23:36:58 GMT
# 信息内显示max-age时间
Cache-Control: max-age=43200
Accept-Ranges: bytes
- 测试PHP文件,没有max-age信息
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, ... 11:46:58 GMT
Content-Type: application/octet-stream
Content-Length: 19
Last-Modified: Wed, ... 11:36:44 GMT
Connection: keep-alive
ETag: "5a4e1572-13"
Accept-Ranges: bytes