vim /usr/local/nginx/conf/vhost/../nginx.conf
[root@xavi vhost]# vim ../nginx.conf
找到如下,是定义日志格式:
log_format xavi ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘ $host "$request_uri" $status‘
‘ "$http_referer" "$http_user_agent"‘;
combined_realip;为日志格式名字,可以改为其他的,后面可以调用它
上述定义日志的名称改为xavi。
注:这边改成什么名字,待会引用的时候就写成什么!
如上除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加:
access_log /tmp/haha.com_acess.log xavi;
使用access_log指定日志存储路径和使用的日志格式名字
[root@xavi vhost]# vim atorreid.com.conf
server
{
listen 80 default_server;
server_name atorreid.com xavi.com abc.com;
index index.html index.htm index.php;
root /data/nginx/www.torreid.com;
if ($host != ‘torreid.com‘ ) {
rewrite ^/(.*)$ http://torreid.com/$1 permanent;
}
access_log /tmp/test.com.log xavi
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
[root@xavi vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@xavi vhost]# !curl
curl -x127.0.0.1:80 www.atorreid.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 16:05:55 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://torreid.com/index.html
日志对于统计排错来说非常有利的,如果一个100G的日志别说查看了就打开我们都需要等待很久这样不仅浪费了我们的硬件资源同时也浪费了时间。如果按照每天分成一个日志,是不是更有利于我们去排障呢?-- by ZDY
由于Nginx不像Apache有自己的切割工具,在此我们需要写个脚本完成需求:
养成好的习惯把脚本放在sbin目录下:
[root@xavi ~]# vim /usr/local/sbin/nginx_logrotate.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`
d=date -d "-1 day" +%Y%m%d;生成昨天的日期
for log inls *.log
do
mv $log $log-$d
done 这是一个for循环,把ls列举的log文件,执行以日期格式的重命名
nginx_pid=”/usr/local/nginx/logs/nginx.pid”;就是为了最后一行而设定的。/bin/kill -HUP
cat $nginx_pid
最后一行的意思和之前使用的 -s reload 是一个意思 重载nginx.pid,然后就会再次生成一个新的日志文件。否则不生成日志文件
[root@xavi ~]# 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 atorreid.com.log ‘ php_errors.log‘ php_errors.log test.com.log torrecid.com.log
+ for log in ‘`ls *.log`‘
+ mv atorreid.com.log atorreid.com.log-20180314
+ for log in ‘`ls *.log`‘
+ mv php_errors.log php_errors.log-20180314
+ for log in ‘`ls *.log`‘
+ mv php_errors.log php_errors.log-20180314
mv: 无法获取"php_errors.log" 的文件状态(stat): 没有那个文件或目录
+ for log in ‘`ls *.log`‘
+ mv test.com.log test.com.log-20180314
+ for log in ‘`ls *.log`‘
有了切割可以满足我们的日常工作需要,但是随着访问量的剧增,如果不删除老的日志文件我们的磁盘很快就会占用完。
[root@xavi ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
crontab -e
加入如下内容
0 0 * /bin/bash /usr/local/sbin/nginx_logrotate.sh
[root@xavi ~]# crontab -e
no crontab for root - using an empty one
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
转载自老七博客:http://www.okay686.cn/524.html
#! /bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/usr/local/php-fpm/var/log/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `find . -name "*_slow.log"`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
find . -name "*_slow.log-*" -mtime +30 | xargs rm -rf
[root@xavi ~]# cd /usr/local/nginx/conf/vhost/
[root@xavi vhost]# ls
atorreid.com.conf bcd.com.conf test.com.conf
[root@xavi vhost]# vim test.com.conf
server
{
listen 80 default_server;
server_name test.com xavi.com abc.com;
index index.html index.htm index.php;
root /data/nginx/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 xavi;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
~匹配.gif .jpg .jpeg等格式的静态文件不计入日志,|表示或者
\表示脱义字符,XXX.gif
[root@xavi vhost]# cd /data/nginx/test.com/
[root@xavi test.com]# ls
admin admin.php index.html
[root@xavi test.com]# vim 1.gif
[root@xavi test.com]# vim 2.js
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/2.js
axccdecdcece:
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/1.gif
dadadsdsda
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/x.gif
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@xavi test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:21:09:12 +0800] www.atorreid.com "/index.html" 301 "-" "curl/7.29.0"
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/xx2.jsdsdsad
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@xavi test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:21:09:12 +0800] www.atorreid.com "/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:21:12:25 +0800] test.com "/xx2.jsdsdsad" 404 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:21:15:33 +0800] test.com "/2.jsdsdsad" 404 "-" "curl/7.29.0"
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/2.js -I //访问js类型的文件,缓存过期时间为12小时
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 13:17:07 GMT
Content-Type: application/javascript
Content-Length: 14
Last-Modified: Thu, 15 Mar 2018 13:08:00 GMT
Connection: keep-alive
ETag: "5aaa7030-e"
Expires: Fri, 16 Mar 2018 01:17:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
原文地址:http://blog.51cto.com/12995218/2087432