Nginx访问日志Nginx日志切割 静态文件不记录日志和过期时间

6月8日任务

12.10 Nginx访问日志
12.11 Nginx日志切割
12.12 静态文件不记录日志和过期时间

Nginx访问日志目录概要

  • 日志格式
  • vim /usr/local/nginx/conf/nginx.conf //搜索log_format
$remote_addr客户端IP(公网IP)
$http_x_forwarded_for代理服务器的IP
$time_local服务器本地时间
$host访问主机名(域名)
$request_uri访问的url地址
$status状态码
$http_refererreferer
$http_user_agentuser_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

Nginx访问日志

  • 日志的文件也是在主配置文件中
  • 打开主配置文件vim /usr/local/nginx/conf/nginx.conf
[root@yong-01 vhost]# vim /usr/local/nginx/conf/nginx.conf

搜索/log_format 找到以下内容,就是来定义日志格式的
 log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
  • combined_realip 日志格式的名字,可以随便定义,这里定义成什么名字,后面就引用成什么名字,决定了虚拟主机引用日志的类型
  • nginx配置文件,有一个特点,以 “ ; ” 分号结尾,配置文件一段如果没有 分号结尾,表示这一段还没有结束,就算中间执行了换行。
$remote_addr客户端IP(公网IP)
$http_x_forwarded_for代理服务器的IP
$time_local服务器本地时间
$host访问主机名(域名)
$request_uri访问的url地址
$status状态码
$http_refererreferer(跳转页)
$http_user_agentuser_agent(标识)
  • 若想自己的公网IP,可以直接百度IP,就会出来自己上网的IP地址
  • 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件去定义access_log /tmp/test.com.log combined_realip; 来定义访问日志路径
[root@yong-01 vhost]# vim test.com.conf 

在第一个括号中添加access_log /tmp/test.com.log combined_realip;即可

server
{
    listen 80;
    server_name test.com test1.com test2.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 combined_realip;
}

  • 如果不写日志格式,那就会走默认的日志格式
  • 然后检查配置文件是否存在语法错误,并重新加载配置文件
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

  • 测试
[root@yong-01 vhost]# curl -x127.0.0.1:80 test1.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Sat, 09 Jun 2018 01:03:43 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/

[root@yong-01 vhost]# curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Sat, 09 Jun 2018 01:03:58 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/
  • 查看日志cat /tmp/test.com.log
[root@yong-01 vhost]# cat /tmp/test.com.log 
127.0.0.1 - [09/Jun/2018:09:03:43 +0800] test1.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [09/Jun/2018:09:03:58 +0800] test2.com "/" 301 "-" "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

Nginx日志切割

  • Nginx没有自带日志切割工具,只能借助系统的日志切割的工具或者自己写切割的脚本实现
  • 这里写一个日志切割脚本
  • 首先创建一个shell脚本vim /usr/local/sbin/nginx_log_rotate.sh
  • 所有的shell脚本放入到/usr/local/sbin/目录下
[root@yong-01 vhost]# vim /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`

  • d=date -d “-1 day” +%Y%m%d // 生成昨天的日期,格式为年月日
  • logdir=”/tmp/” // 上一节的时候,定义了日志存放在/tmp/目录下
  • nginx_pid=”/usr/local/nginx/logs/nginx.pid” //查找nginx的PID,目的是为了执行/bin/kill -HUP cat $nginx_pid ,而这个命令目的和nginx -s reload 是一样的
  • cd $logdir //进入“logdir”日志目录下
  • for log in ls *.log //开始语句循环,看错有哪些 log后缀的文件
  • do //执行  mv $log $log-$d 改名
  • done //结束
  • /bin/kill -HUP cat $nginx_pid // 重新加载,生成一个新的“nginx_pid=”/usr/local/nginx/logs/nginx.pid”
[root@yong-01 vhost]# ls 
aaa.com.conf  test.com.conf
[root@yong-01 vhost]# for f in `ls` ; do ls -l $f ;done
-rw-r--r-- 1 root root 142 6月   7 21:47 aaa.com.conf
-rw-r--r-- 1 root root 293 6月   9 09:01 test.com.conf
  • 执行shell脚本,并加-x选项,是为了查看脚本执行的过程
[root@yong-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh 
++ date -d '-1 day' +%Y%m%d
+ d=20180608
+ 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-20180608
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1226

  • 查看日志切割文件,每天都生成一个日志,在每天切割后,过段时间还要定期清理
[root@yong-01 vhost]# ls /tmp/
mysql.sock
pear
php-fcgi.sock
systemd-private-4f8e9ff25515441f842387e8592cb371-chronyd.service-rUvlFo
systemd-private-4f8e9ff25515441f842387e8592cb371-vgauthd.service-Pjk7Gv
systemd-private-4f8e9ff25515441f842387e8592cb371-vmtoolsd.service-Fbw1Ir
systemd-private-fb16b9da48d04706bda2fb1e850ee20d-chronyd.service-PfzjDV
systemd-private-fb16b9da48d04706bda2fb1e850ee20d-vgauthd.service-7voK16
systemd-private-fb16b9da48d04706bda2fb1e850ee20d-vmtoolsd.service-TQ5rlv
test.com.log
test.com.log-20180608
  • 删除30天以前的日志文件
[root@yong-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
  • 写完脚本后,还要加一个任务计划crontab -e
[root@yong-01 vhost]# 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;
    }

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

  • 打开虚拟主机配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
  • 在配置文件中添加
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    //匹配gif|jpg|jpeg|png|bmp|swf 后缀的文件
    {
          expires      7d;        //7天后过期
          access_log off;        //匹配“.*.(gif|jpg|jpeg|png|bmp|swf) ”关闭记录日志
    }
location ~ .*\.(js|css)$
    {
          expires      12h;        //12个小时后过期
          access_log off;        //匹配“.*.(js|css) ”关闭记录日志
    }

  • 检查配置文件语法错误,并重新加载配置文件
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
  • 测试,先来模拟一个图片
[root@yong-01 vhost]# cd /data/wwwroot/test.com/
[root@yong-01 test.com]# ls
admin  admin.php  index.html
[root@yong-01 test.com]# vim 1.gif
[root@yong-01 test.com]# vim 3.js
  • 接下来做一个访问测试
[root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
fasdfasf
[root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/3.js
fadsfasgjsd
[root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
  • 查看日志,会看到只有一条日志
[root@yong-01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [09/Jun/2018:09:45:28 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  • 测试过期时间,加上-I参数
[root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/3.js -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Sat, 09 Jun 2018 01:46:33 GMT
Content-Type: application/x-javascript
Content-Length: 12
Last-Modified: Sat, 09 Jun 2018 01:44:11 GMT
Connection: keep-alive
ETag: "5b1b30eb-c"
Expires: Sat, 09 Jun 2018 13:46:33 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

  • max-age=43200 过期时间
  • 如果去掉配置文件中的expires,则不会显示max-age过期时间

转载于:https://my.oschina.net/u/3791387/blog/1827187

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值