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

nginx访问日志:

vim /usr/local/nginx/conf/nginx.conf    #搜索log_format   该字段定义日志格式,默认如下:

#combined_realip日志格式的名字,可随意定义;

 

定义访问日志:

需在虚拟主机配置文件里面中添加配置。

1.编辑虚拟主机配置文件,

vim /usr/local/nginx/conf/vhost/test.com.conf       ## 在server{}中增加“access_log /tmp/test.com.log combined_realip;”   #日志路径和名称为/tmp/test.com.log;combined_realip为nginx.conf中日志格式的名字。

2.检查并重新加载配置文件:

/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload

3.使用curl进行访问测试:

4.查看对应日志目录的日志内容:

 

nginx日志切割:

编写日志切割脚本:

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`

 

 

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

 

1.编辑虚拟主机配置文件:

vim /usr/local/nginx/conf/vhost/test.com.conf               # 在server{}中增加:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$            #匹配以  .xxx结尾的,xxx为()里字符串
    {
          expires      7d;                            #过期时间为7天
          access_log off;                             #访问日志关闭
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }

 

2.检查并重新加载配置文件:

/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload

 

3.创建测试文件:

cd /data/wwroot/test.com;echo "123134" > 1.gif ;echo "1244536" > 2.js              #在网站根目录创建测试gif和js文件

 

4.访问测试并查看日志:

curl -x127.0.0.1:80 test.com/1.gif

curl -x127.0.0.1:80 test.com/2.js

cat /tmp/test.com.log            #若配置正确,此处日志并不会显示1.gif和2.js的访问记录

 

5.查看过去时间字段是否生效:max-age=43200 单位为妙,即12h,和上面配置对应

转载于:https://my.oschina.net/u/3866910/blog/1927934

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值