访问日志
1、定义日志格式
1
2
3
4
|
[root@plinuxos ~]
# vi /usr/local/nginx/conf/nginx.conf
log_format log001
'$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"'
;
|
▎Nginx日志格式:
$remote_addr |
客户端IP(公网IP) |
$http_x_forwarded_for |
代理服务器的IP |
$time_local |
服务器本地时间 |
$host |
访问主机名(域名) |
$request_uri |
访问的url地址 |
$status |
状态码 |
$http_referer |
referer |
$http_user_agent |
user_agent |
2、增加访问日志项
1
2
3
4
5
6
7
8
9
|
[root@plinuxos ~]
# vi /usr/local/nginx/conf/vhost/default.conf
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root
/data/wwwroot/default
;
access_log
/tmp/default
.log log001;
##增加该行,该行元素:访问日志,存储地址,格式名称
}
|
3、检查与重载
1
2
3
4
|
[root@plinuxos ~]
# /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@plinuxos ~]
# /usr/local/nginx/sbin/nginx -s reload
|
4、测试效果
1
2
3
4
5
6
7
|
[root@plinuxos ~]
# curl -x127.0.0.1:80 aaa.com/index.html
“This is a default site.”
[root@plinuxos ~]
# curl -x127.0.0.1:80 aaa1.com/index.html
“This is a default site.”
[root@plinuxos ~]
# cat /tmp/default.log
127.0.0.1 - [12
/Aug/2017
:10:34:45 +0800] aaa.com
"/index.html"
200
"-"
"curl/7.29.0"
127.0.0.1 - [12
/Aug/2017
:10:34:52 +0800] aaa1.com
"/index.html"
200
"-"
"curl/7.29.0"
|
日志切割
1、日志切割脚本
1
2
3
4
5
6
7
8
9
10
11
|
[root@plinuxos ~]
# vi /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
d=`
date
-d
"-1 day"
+%Y%m%d`
logdir=
"/tmp/"
nginx_pid=
"/usr/local/nginx/logs/nginx.pid"
cd
$logdir
for
log
in
`
ls
*.log`
do
mv
$log $log-$d
done
/bin/kill
-HUP `
cat
$nginx_pid`
|
▎KILL参数:
-
INT:快速关闭,是当用户键入<Control-C>时由终端驱动程序发送的信号。
-
TERM:快速关闭,请求彻底终止某项执行操作,它期望接收进程清除自给的状态并退出。
-
HUP:平滑启动,重新加载配置文件。
-
QUIT:从容关闭。
2、执行脚本
1
2
3
4
5
6
7
8
9
10
11
|
[root@plinuxos tmp]
# sh -x /usr/local/sbin/nginx_log_rotate.sh
++
date
-d
'-1 day'
+%Y%m%d
+ d=20170811
+ logdir=
/tmp/
+ nginx_pid=
/usr/local/nginx/logs/nginx
.pid
+
cd
/tmp/
++
ls
default.log
+
for
log
in
'`ls *.log`'
+
mv
default.log default.log-20170811
++
cat
/usr/local/nginx/logs/nginx
.pid
+
/bin/kill
-HUP 89689
|
3、检查效果
1
2
|
[root@plinuxos tmp]
# ls
default.log default.log-20170811 mysql.sock pear php-fcgi.sock
|
4、定期任务计划
1
2
|
[root@plinuxos tmp]
# crontab -e
0 0 * * *
/bin/bash
/usr/local/sbin/nginx_log_rotate
.sh
|
5、清理过期日志
1
2
|
[root@plinuxos tmp]
# find /tmp/ -name *.log-* -type f -mtime +10 |xagrs rm
##也可以放在脚本里,定期清除
|
静态文件不记录日志和过期时间
1、编辑配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@plinuxos default]
# vi /usr/local/nginx/conf/vhost/default.conf
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root
/data/wwwroot/default
;
access_log
/tmp/default
.log log001;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
}
|
2、检查与重载
1
2
3
4
|
[root@plinuxos default]
# /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@plinuxos default]
# /usr/local/nginx/sbin/nginx -s reload
|
3、检查效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[root@plinuxos default]
# ls
index.html pic001.gif
[root@plinuxos default]
# curl -x127.0.0.1:80 aaa.com/pic001.gif -I
HTTP
/1
.1 200 OK
Server: nginx
/1
.12.1
Date: Sat, 12 Aug 2017 03:31:08 GMT
Content-Type: image
/gif
Content-Length: 66698
Last-Modified: Sat, 12 Aug 2017 03:29:18 GMT
Connection: keep-alive
ETag:
"598e760e-1048a"
Expires: Sat, 19 Aug 2017 03:31:08 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
[root@plinuxos tmp]
# curl -x127.0.0.1:80 aaa.com/pic001 -I
HTTP
/1
.1 404 Not Found
Server: nginx
/1
.12.1
Date: Sat, 12 Aug 2017 03:34:43 GMT
Content-Type: text
/html
Content-Length: 169
Connection: keep-alive
[root@plinuxos tmp]
# cat /tmp/default.log
127.0.0.1 - [12
/Aug/2017
:11:34:43 +0800] aaa.com
"/pic001"
404
"-"
"curl/7.29.0"
|
本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1955696
,如需转载请自行联系原作者