标签:日志
Nginx访问日志
1.访问日志配置文件:
vim /usr/local/nginx/conf/nginx.conf
server_names_hash_max_size 4096;
log_format weixing ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘ $host "$request_uri" $status‘
2.增加新的语句:
[root@weixing01 vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != ‘test.com‘ ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
access_log /tmp/test.com.log weixing;
}
3.测试
[root@weixing01 vhost]# curl -x127.0.0.1:80 test3.com/index.html/qwkljfkaj -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 16:59:53 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html/qwkljfkaj
[root@weixing01 vhost]# curl -x127.0.0.1:80 test2.com/index.html/qwkljfkaj -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:00:03 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html/qwkljfkaj
[root@weixing01 vhost]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:00:59:53 +0800] test3.com "/index.html/qwkljfkaj" 301 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:01:00:03 +0800] test2.com "/index.html/qwkljfkaj" 301 "-" "curl/7.29.0"
1.创建shell脚本:
[root@weixing01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
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`
2.执行
[root@weixing01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh
++ date -d ‘-1 day‘ +%Y%m%d
+ d=20180314
+ 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-20180314
+ for log in ‘`ls *.log`‘
+ mv test.com.log test.com.log-20180314
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 964
3.查看:
[root@weixing01 vhost]# ls /tmp
pear
php_errors.log-20180314
php-fcgi.sock
systemd-private-d2e174b4ed3e4c7b88d41945dde9c305-chronyd.service-AitKj7
systemd-private-d2e174b4ed3e4c7b88d41945dde9c305-vgauthd.service-JcZvBd
systemd-private-d2e174b4ed3e4c7b88d41945dde9c305-vmtoolsd.service-LU4x4E
test.com.log
test.com.log-20180314
4.添加任务计划:
[root@weixing01 vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
1.修改配置文件:
[root@weixing01 vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != ‘test.com‘ ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/test.com.log weixing;
}
2.测试:
[root@weixing01 test.com]# vim 1.gif
[root@weixing01 test.com]# vim 2.js
[root@weixing01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
sjlgjakljgkaj
[root@weixing01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sakjgklsjkgjkl
[root@weixing01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@weixing01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:01:21:51 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@weixing01 test.com]# curl -x127.0.0.1:80 test.com/2.jssgaga
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@weixing01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:01:21:51 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:01:22:40 +0800] test.com "/2.jssgaga" 404 "-" "curl/7.29.0"
有最长保存时间
[root@weixing01 test.com]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:23:33 GMT
Content-Type: application/javascript
Content-Length: 15
Last-Modified: Wed, 14 Mar 2018 17:21:01 GMT
Connection: keep-alive
ETag: "5aa959fd-f"
Expires: Thu, 15 Mar 2018 05:23:33 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
标签:日志
原文地址:http://blog.51cto.com/13517254/2087000