Nginx访问日志
[root@lyon-02 ~]# vim /usr/local/nginx/conf/nginx.conf //主配置文件如下
log_format lyon '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
除了在主配置文件nginx.conf里定义日志格式外,
还需要在虚拟主机配置文件中增加
access_log /tmp/1.log combined_realip;
这里的combined_realip就是在nginx.conf中定义的日志格式名字
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log
[root@lyon-02 ~]# cd /usr/local/nginx/conf/vhost/
[root@lyon-02 vhost]# ls
aaa.com.conf test.com.conf
[root@lyon-02 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 lyon; //加在这就可以
#location ~ admin.php
# {
# auth_basic "Auth";
# auth_basic_user_file /usr/local/nginx/conf/htpasswd;
#}
}
[root@lyon-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@lyon-02 vhost]# curl -x127.0.0.1:80 test.com/index.html/safs
404 Not Found
404 Not Found
nginx/1.4.7
[root@lyon-02 vhost]# curl -x127.0.0.1:80 test.com/index.html/safs -I
HTTP/1.1 404 Not Found
Server: nginx/1.4.7
Date: Sat, 21 Oct 2017 01:16:52 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
[root@lyon-02 vhost]# cat /tmp/test.com.log
127.0.0.1 - [21/Oct/2017:09:16:49 +0800] test.com "/index.html/safs" 404 "-" "curl/7.29.0"
127.0.0.1 - [21/Oct/2017:09:16:52 +0800] test.com "/index.html/safs" 404 "-" "curl/7.29.0"
~
Nginx日志切割~
自定义shell 脚本
vim /usr/local/sbin/nginx_log_rotate.sh//写入如下内容
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
logdir="/data/logs"
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`
任务计划
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
[root@lyon-02 vhost]# vim /usr/local/sbin/nginx_log_rotate.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`
[root@lyon-02 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20171020
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20171020
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1386
[root@lyon-02 vhost]#
[root@lyon-02 vhost]# ls /tmp/
mysql.sock
pear
php_errors.log-20171020
[root@lyon-02 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm //删除30天以上的日志
crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh //做个任务计划
~
静态文件不记录日志和过期时间
配置如下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
[root@lyon-02 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 lyon;
#location ~ admin.php
# {
# auth_basic "Auth";
# auth_basic_user_file /usr/local/nginx/conf/htpasswd;
#}
}
保存退出
[root@lyon-02 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@lyon-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@lyon-02 vhost]# cd /data/wwwroot/test.com/
[root@lyon-02 test.com]# ls
admin index.html
[root@lyon-02 test.com]# vim 1.gif //创建2个虚拟的文件
[root@lyon-02 test.com]# vim 2.js
[root@lyon-02 test.com]# curl -x127.0.0.1:80 test.com/1.gif
fsfds
[root@lyon-02 test.com]# curl -x127.0.0.1:80 test.com/2.js
fsfew
[root@lyon-02 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@lyon-02 test.com]# cat /tmp/test.com.log
127.0.0.1 - [21/Oct/2017:09:40:06 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
js 和 gif 不会被记录
[root@lyon-02 test.com]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Sat, 21 Oct 2017 01:42:24 GMT
Content-Type: application/x-javascript
Content-Length: 6
Last-Modified: Sat, 21 Oct 2017 01:39:17 GMT
Connection: keep-alive
ETag: "59eaa545-6"
Expires: Sat, 21 Oct 2017 13:42:24 GMT
Cache-Control: max-age=43200 //过期时间
Accept-Ranges: bytes