Nginx访问日志
1、查看nginx的日志格式
[root@aliyun logs]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
log_format
combined_realip
'
$remote_addr
$http_x_forwarded_for
[$time_local]
'
日志 日志格式_名字 来访用户的公网IP 代理服务器的IP 本地时间
'
$host
"$request_uri"
$status
' '
"$http_referer"
"$http_user_agent"
';
目标主机(域名) 目标资源 连接状态(状态码) 跳转来源 浏览器标识
2、编辑虚拟主机的配置文件
最后的结束符 } 上方插入日志存放路径和记录规则
server
{
listen 80 default_server;
server_name www.test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
access_log /usr/local/nginx/logs/test.com.log combined_realip;
}
[root@aliyun logs]# /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@aliyun logs]# /usr/local/nginx/sbin/nginx -s reload
3、测试
[root@aliyun logs]# curl -x127.0.0.1:80 test.com/123 -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Sun, 10 Jun 2018 20:13:57 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
4、查看日志
[root@aliyun logs]# tail /usr/local/nginx/logs/test.com.log
127.0.0.1 - [11/Jun/2018:04:11:18 +0800] test.com "/123" 404 "-" "curl/7.29.0"
127.0.0.1 - [11/Jun/2018:04:13:57 +0800] test.com "/123" 404 "-" "curl/7.29.0"
Nginx日志切割
nginx的日志非常简单,没有自带的日志切割工具,要切割日志需要借助系统的切割工具或者自定义切割脚本。
1、自定义的日志切割脚本
[root@aliyun logs]# vim /usr/local/sbin/nginx_log_rotate.sh
写入如下脚本内容:
#! /bin/bash 声明解释器
d=`date -d "-1 day" +%Y%m%d` 设置变量d
logdir="/data/logs/" 设置变量logdir
nginx_pid="/data/logs/nginx.pid" 定义nginx_pid路径
cd $logdir 打开logdir变量(日志目录)
for log in `ls *.log` 定义log变量的循环条件为 `ls *.log`的命令输出
mv $log $log-$d 执行动作
done
/bin/kill -HUP `cat $nginx_pid` 生成新的日志
2、写入任务计划:
[root@aliyun logs]# chmod 755 /usr/local/sbin/nginx_log_rotate.sh
[root@aliyun logs]# crontab -e
写入计划任务
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
[root@aliyun logs]# find /data/logs/ -name *.log-* f -mtime +30 |xargs rm 找出30天前的日志并删除
静态文件不记录日志,配置静态元素过期时间
1、编辑虚拟主机配置文件
写入日志记录规则
server
{
listen 80 default_server;
server_name www.test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
指定文件类型
{
expires 7d;
有效期7天
access_log off;
不记录日志
}
location ~ .*\.(js|css)$
指定文件类型
{
expires 12h;
有效期12小时
access_log off;
不记录日志
}
access_log /usr/local/nginx/logs/elon.org.cn.log combined_realip;
}
2、测试访问:
[root@aliyun logs]# /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@aliyun logs]# /usr/local/nginx/sbin/nginx -s reload
[root@aliyun logs]# curl -x 127.0.0.1:80 test.com/
1.js
-I
HTTP/1.1
200 OK
Server: nginx/1.14.0
Date: Sun, 10 Jun 2018 20:53:56 GMT
Content-Type: application/javascript
Content-Length: 8
Last-Modified: Sun, 10 Jun 2018 20:51:30 GMT
Connection: keep-alive
ETag: "5b1d8f52-8"
Expires: Mon, 11 Jun 2018 08:53:56 GMT
Cache-Control:
max-age=43200
Accept-Ranges: bytes
[root@aliyun logs]# curl -x 127.0.0.1:80 test.com/
2.jpeg
-I
HTTP/1.1
200 OK
Server: nginx/1.14.0
Date: Sun, 10 Jun 2018 20:54:38 GMT
Content-Type: image/jpeg
Content-Length: 8
Last-Modified: Sun, 10 Jun 2018 20:51:58 GMT
Connection: keep-alive
ETag: "5b1d8f6e-8"
Expires: Sun, 17 Jun 2018 20:54:38 GMT
Cache-Control:
max-age=604800
Accept-Ranges: bytes
[root@aliyun logs]# curl -x 127.0.0.1:80 test.com/
3.jss
-I
HTTP/1.1
200 OK
Server: nginx/1.14.0
Date: Sun, 10 Jun 2018 20:54:47 GMT
Content-Type: application/octet-stream
Content-Length: 0
Last-Modified: Sun, 10 Jun 2018 20:52:42 GMT
Connection: keep-alive
ETag: "5b1d8f9a-0"
Accept-Ranges: bytes
[root@aliyun logs]# tail /usr/local/nginx/logs/test.com.log
127.0.0.1 - [11/Jun/2018:04:11:18 +0800] test.com "/123" 404 "-" "curl/7.29.0"
127.0.0.1 - [11/Jun/2018:04:13:57 +0800] test.com "/123" 404 "-" "curl/7.29.0"
80.82.78.50 - [11/Jun/2018:04:15:18 +0800] www.baidu.com "/cache/global/img/gs.gif" 404 "-" "Mozilla"
127.0.0.1 - [11/Jun/2018:04:54:47 +0800] test.com "/
3.jss
" 200 "-" "curl/7.29.0"
可以看到只有3.jss的访问日志,1.js 2.jpeg的访问日志都没有记录