目录
一、Nginx访问日志
二、Nginx日志切割
三、静态文件不记录日志和过期时间
一、Nginx访问日志
- Nginx日志格式
[root@minglinux-01 ~] 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"';
nginx配置文件中“;”是一段配置的结尾,所以以上是一行配置,这行配置定义了日志格式
combined_realip为日志格式的名字,后面可引用
$remote_addr为访问网站的用户的出口IP,客户端ip(公网ip)
$http_x_forwarded_for为代理服务器的IP,如果使用了代理,则会记录代理的IP
$time_local为服务器本地的时间
$host为访问的主机名
$request_uri为访问的URL地址
$status为状态码
$http_referer为referer地址
$http_user_agent为user_agent
- 设置虚拟主机日志格式
- 在nginx.conf定义了名字为ming的配置文件
log_format ming '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
- 到虚拟主机配置文件中指定访问日志的路径和调用ming格式
1 server
2 {
3 listen 80;
4 server_name test.com test2.com test3.com;
5 index index.html index.htm index.php;
6 root /data/wwwroot/test.com;
7 if ($host != 'test.com' ) {
8 rewrite ^/(.*)$ http://test.com/$1 permanent;
9 }
10 access_log /tmp/test.com.log ming; //加入这行
11
12 location ~admin.php
13 {
14 auth_basic "Auth";
15 auth_basic_user_file /usr/local/nginx/conf/htpasswd;
16 }
17 }
access_log指定日志的存储路径,最后面指定日志的格式名字
- 测试
[root@minglinux-01 /usr/local/nginx/conf/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@minglinux-01 /usr/local/nginx/conf/vhost] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 /usr/local/nginx/conf/vhost] curl -x127.0.0.1:80 test.com
test page
[root@minglinux-01 /usr/local/nginx/conf/vhost] curl -x127.0.0.1:80 test3.com
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@minglinux-01 /usr/local/nginx/conf/vhost] cat /tmp/test.com.log
127.0.0.1 - [27/Nov/2018:21:56:30 +0800] test.com "/" 200 "-" "curl/7.29.0"
127.0.0.1 - [27/Nov/2018:21:56:37 +0800] test3.com "/" 301 "-" "curl/7.29.0"
二、Nginx日志切割
Nginx不像httpd还有自带的切割工具,要想切割Nginx日志需要借助系统的切割工具或者自定义脚本。
下面编写一个日志切割脚本:
[root@minglinux-01 /usr/local/sbin] vim nginx_logrotate.sh
1 #!/bin/bash
2 d=`date -d "-1 day" +%Y%m%d` ##date -d "-1 day"表示昨天的日期
3 logdir="/tmp/" ##日志存放路径
4 nginx_pid="/usr/local/nginx/logs/nginx.pid" ##查找nginx的pid
5 cd $logdir
6 for log in `ls *.log`
7 do
8 mv $log $log-$d ##将匹配的文件改为$log-$d”,$d就是昨天的日
期
9 done
10 /bin/kill -HUP `cat $nginx_pid` ##命令效果同nginx的-s reload(
重新加载配置)
11
- 执行脚本
[root@minglinux-01 /usr/local/sbin] sh -x /usr/local/sbin/nginx_logrotate.sh //加-x显示执行过程
++ date -d '-1 day' +%Y%m%d
+ d=20181126
+ 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-20181126
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20181126
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1584
[root@minglinux-01 /usr/local/sbin] ls /tmp/
mysql.sock php-fcgi.sock vmware-root
pear test.com.log
php_errors.log-20181126 test.com.log-20181126
脚本将上一天的日志加上日期的后缀并生成一个新的日志文件。
- 清理日志
[root@minglinux-01 /usr/local/sbin] find /tmp/ -name *.log-* -type f -mtime +30|xargs rm
rm: 缺少操作数 //没有匹配以上条件的日志文件,不执行删除操作
Try 'rm --help' for more information.
命令意思为找出/tmp/目录下名字匹配.log-且最近一次文件内容被修改的时间超过30天以前的文件。
- 添加任务计划
[root@minglinux-01 /usr/local/sbin] crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
每日凌晨0点执行脚本
三、静态文件不记录日志和过期时间
在虚拟主机配置文件中加上以下内容:
···
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配.gif/.jpg/.jpeg/.png/.bmp/.swf
{
expires 7d; //设置过期时间为7天
access_log off; //不记录日志
}
localtion ~ .*\.(js|css)$
{
expires 12h; //过期时间一样可以和上面写在一起
access_log off; //不记录日志
}
···
- 测试
[root@minglinux-01 /usr/local/sbin] /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@minglinux-01 /usr/local/sbin] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 /usr/local/sbin] cd /data/wwwroot/test.com/
[root@minglinux-01 /data/wwwroot/test.com] ls
1.html admin admin.php index.html
[root@minglinux-01 /data/wwwroot/test.com] vim 1.gif
[root@minglinux-01 /data/wwwroot/test.com] vim 2.js
[root@minglinux-01 /data/wwwroot/test.com] curl -x127.0.0.1:80 test.com/1.gif
a
[root@minglinux-01 /data/wwwroot/test.com] curl -x127.0.0.1:80 test.com/2.js
g
[root@minglinux-01 /data/wwwroot/test.com] curl -x127.0.0.1:80 test.com/index.html
test page
[root@minglinux-01 /data/wwwroot/test.com] cat /tmp/test.com.log
127.0.0.1 - [27/Nov/2018:23:02:04 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
//访问js和gif文件时没有记录日志
[root@minglinux-01 /data/wwwroot/test.com] curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 27 Nov 2018 15:05:30 GMT
Content-Type: application/javascript
Content-Length: 2
Last-Modified: Tue, 27 Nov 2018 14:59:37 GMT
Connection: keep-alive
ETag: "5bfd5bd9-2"
Expires: Wed, 28 Nov 2018 03:05:30 GMT
Cache-Control: max-age=43200 //此处表示过期时间是43200s即12天
Accept-Ranges: bytes