180104 LNMP Nginx访问日志

nginx访问日志

  • 日志格式
  • vi /usr/local/nginx/conf/nginx.conf
  • 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
  • access_log /tmp/1.log combined_realip;
  • 这里的combined_realip就是在nginx.conf中定义的日志格式名字
  • -t && -s reload

  • cat /tmp/1.log
[root@node15 ~]# cd /usr/local/nginx/conf/vhost/
[root@node15 vhost]# vim ../nginx.conf
 vim /usr/local/nginx/conf/nginx.conf //搜索log_format
添加access_log /tmp/test.com.log user;
[root@node15 vhost]# ls
aaa.conf  test.com.conf
[root@node15 vhost]# vim test.com.conf 
[root@node15 vhost]# cat 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 aming;    
}


[root@node15 vhost]# 
[root@node15 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@node15 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@node15 vhost]# curl -x 127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Sat, 06 Jan 2018 13:32:32 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html


[root@node15 vhost]# cat /tmp/test.com.log 
127.0.0.1 - [06/Jan/2018:21:32:32 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"

nginx日志切割

  • 自定义shell脚本
  • vi /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
  • crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
[root@node15 vhost]# vi /usr/local/sbin/nginx_logrotate.sh
[root@node15 vhost]# cat /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`
[root@node15 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180105
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log yum.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180105
+ for log in '`ls *.log`'
+ mv yum.log yum.log-20180105
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 921
[root@node15 vhost]# ls /tmp/
ks-script-nkGVTh
mysql.sock
pear
php-fcgi.sock
systemd-private-80b9c77f34f34e37b89436144d6c8a86-chronyd.service-9UflCm
systemd-private-80b9c77f34f34e37b89436144d6c8a86-vgauthd.service-sPqrpN
systemd-private-80b9c77f34f34e37b89436144d6c8a86-vmtoolsd.service-jzdaO3
systemd-private-958b1655ca604b05afb404a39145f056-chronyd.service-gnEs10
systemd-private-958b1655ca604b05afb404a39145f056-vgauthd.service-QJLw1W
systemd-private-958b1655ca604b05afb404a39145f056-vmtoolsd.service-4jj2W5
test.com.log
test.com.log-20180105
yum.log-20180105
[root@node15 vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

将一个月之前的日志作删除
[root@node15 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm
rm: 缺少操作数
Try 'rm --help' for more information.

静态文件不记录日志和过期时间

  • 配置如下 location ~..(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~..(js|css)$ { expires 12h; access_log off; }
[root@node15 vhost]# cat 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 aming;    
}

[root@node15 vhost]# cd /data/wwwroot/test.com/
[root@node15 test.com]# ls
admin  index.html
[root@node15 test.com]# vim 1.gif
[root@node15 test.com]# vim 2.js

[root@node15 test.com]# curl -x127.0.0.1:80 test.com/1.gif
ahsjhdfsvb
[root@node15 test.com]# curl -x127.0.0.1:80 test.com/2.js
dshfvbnkjd
[root@node15 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@node15 test.com]# cat /tmp/test.com.log
127.0.0.1 - [06/Jan/2018:21:53:08 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"


你可能感兴趣的:(180104 LNMP Nginx访问日志)