Nginx访问日志、日志切割、静态文件不记录日志和过期时间
6月8日任务
12.10 Nginx访问日志
12.11 Nginx日志切割
12.12 静态文件不记录日志和过期时间
12.10 Nginx访问日志

除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
[root@jimmylinux-001 vhost]# vim test.com.conf

重新加载配置文件后测试
[root@jimmylinux- 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@jimmylinux- vhost]# /usr/local/nginx/sbin/nginx -s reload


12.11 Nginx日志切割
Nginx不像Apache有自带的日志切割工具,所以需要借助于系统的日志切割工具或者自己写日志切割脚本。
1、创建一个shell脚本
[root@jimmylinux- 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"
#! /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@jimmylinux- vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh sh运行脚本,-x表示查看执行脚本的过程。
++ date -d '-1 day' +%Y%m%d
+ d=
+ 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-
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP
以上就是日志切割的脚本,长此以往每天都会生成一个日志,除了每天切割之外还需要做一个清理,可以执行如下命令清理。
[root@jimmylinux- vhost]# find /tmp/ -name *.log-* -type f -mtime + |xargs rm
rm: 缺少操作数 因为没有满足条件的文件,所以会报错提示“缺少操作数”
Try 'rm --help' for more information.
2、加一个任务计划
[root@jimmylinux- vhost]# crontab -e 加一个任务计划 * * * /usr/local/sbin/nginx_log_rotate.sh 每天的凌晨0点开始执行

12.12 静态文件不记录日志和过期时间
在Apache配置的时候介绍了静态文件可以设置不记录日志的,那么在Nginx里面同样也可以把一些静态文件忽略掉,不记录日志。
1、编辑配置文件test.com.conf
[root@jimmylinux- vhost]# vim test.com.conf 添加以下内容 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 匹配URL里面的关键词,括号里面的|是或者,.\是脱义的意思。
{
expires 7d; 过期时间7天
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
2、重新加载配置文件后模拟一个图片
[root@jimmylinux- 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@jimmylinux- vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@jimmylinux- vhost]# cd /data/wwwroot/test.com/ [root@jimmylinux- test.com]# ls
admin index.html [root@jimmylinux- test.com]# vim .gif 新建一个1.gif文件 [root@jimmylinux- test.com]# vim .js 新建一个2.js文件 [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/.jif
<html>
<head><title> Not Found</title></head>
<body bgcolor="white">
<center><h1> Not Found</h1></center>
<hr><center>nginx/1.12.</center>
</body>
</html> [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/.gif
sdjjfdsjfl [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/.js
yyyyybbbbbaaaaccccc [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/index.html
test.com [root@jimmylinux- test.com]# cat /tmp/test.com.log
127.0.0.1 - [/Jun/::: +] test.com "/1.jif" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0" [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/index.html
test.com [root@jimmylinux- test.com]# cat /tmp/test.com.log
127.0.0.1 - [/Jun/::: +] test.com "/1.jif" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0" [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/.js
yyyyybbbbbaaaaccccc [root@jimmylinux- test.com]# cat /tmp/test.com.log
127.0.0.1 - [/Jun/::: +] test.com "/1.jif" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0" [root@jimmylinux- test.com]# curl -x127.0.0.: test.com/.jsfsdfsd
<html>
<head><title> Not Found</title></head>
<body bgcolor="white">
<center><h1> Not Found</h1></center>
<hr><center>nginx/1.12.</center>
</body>
</html>
[root@jimmylinux- test.com]# cat /tmp/test.com.log
127.0.0.1 - [/Jun/::: +] test.com "/1.jif" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/index.html" "-" "curl/7.29.0"
127.0.0.1 - [/Jun/::: +] test.com "/2.jsfsdfsd" "-" "curl/7.29.0"
[root@jimmylinux- test.com]# curl -x127.0.0.: -I test.com/.js
HTTP/1.1 OK
Server: nginx/1.12.
Date: Fri, Jun :: GMT
Content-Type: application/javascript
Content-Length:
Last-Modified: Fri, Jun :: GMT
Connection: keep-alive
ETag: "5b1a9dd5-14"
Expires: Sat, Jun :: GMT 过期时间
Cache-Control: max-age=43200 过期最大值
Accept-Ranges: bytes [root@jimmylinux- test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf 编辑配置文件,把过期时间加#注释掉。
[root@jimmylinux- test.com]# /usr/local/nginx/sbin/nginx -s reload 重新加载配置文件 [root@jimmylinux- test.com]# curl -x127.0.0.: -I test.com/.js 重新再访问
HTTP/1.1 OK
Server: nginx/1.12.
Date: Fri, Jun :: GMT
Content-Type: application/javascript
Content-Length:
Last-Modified: Fri, Jun :: GMT
Connection: keep-alive
ETag: "5b1a9dd5-14" 这个时候就没有过期时间了
Accept-Ranges: bytes




Nginx访问日志、日志切割、静态文件不记录日志和过期时间的更多相关文章
- Nginx访问日志 Nginx日志切割 静态文件不记录日志和过期时间
- Nginx访问日志、 Nginx日志切割、静态文件不记录日志和过期时间
1.Nginx访问日志 配制访问日志:默认定义格式: log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_loc ...
- Linux centosVMware Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间
一.Nginx访问日志 vim /usr/local/nginx/conf/nginx.conf //搜索log_format 日至格式 改为davery格式 $remote_addr 客户端IP ...
- nginx日志、nginx日志切割、静态文件不记录日志和过期时间
2019独角兽企业重金招聘Python工程师标准>>> 12.10 Nginx访问日志 日志格式 vim /usr/local/nginx/conf/nginx.conf //搜索l ...
- Linux centos7 VMware Apache访问日志不记录静态文件、访问日志切割、静态元素过期时间
一.Apache访问日志不记录静态文件 网站大多元素为静态文件,如图片.css.js等,这些元素可以不用记录 vim /usr/local/apache2.4/conf/extra/httpd-vho ...
- Nginx对于图片,js等静态文件的缓存设置
以下是自学it网--中级班上课笔记 网址:www.zixue.it Nginx对于图片,js等静态文件的缓存设置 注:这个缓存是指针对浏览器所做的缓存,不是指服务器端的数据缓存. 主要知识点: loc ...
- Apache配置 5.访问日志不记录静态文件
介绍:项目中的CSS.图片.js都是静态文件.一般会将静态文件放到一个单独的目录中,以方便管理. 1. 配置 # vim /usr/local/apache2.4/conf/extra/httpd-v ...
- nginx与tomcat 组合 实现静态文件和jsp组合访问
主要修改nginx的配置文件: 设置代理 location /{proxy_pass http://47.94.158.2:8080;proxy_redirect off;proxy_set_head ...
- 使用nginx缓存服务器上的静态文件
一.nginx缓存的优点 如图所示,nginx缓存,可以在一定程度上,减少源服务器的处理请求压力. 因为静态文件(比如css,js, 图片)中,很多都是不经常更新的.nginx使用proxy_cach ...
随机推荐
- SpringMVC----执行流程+底层解析
SpringMVC流程图如上面所示,根据上图,串联一下底层源码: 1.在DispatcherServlet中找到doDisPatch 2.观察方法体,然后找到getHandler方法 3.点进方法,发 ...
- NOIP模拟 6
考试时看了看T1,觉得是结论题,推了推没有成果,跑去看第二题, 题意很明确,求过定点的最小环,还没思考解题策略,然后觉得是水题 打了个tarjan找边双(据说会炸但是平均表现良好),在边双里暴力拆边找 ...
- Feign设置assessToken
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.an ...
- NOIP模拟测试13
考得还算可以,T3还有提升空间(没看清题&&样例没过 拿了4分). 期望得分:80+40+0=120 实际得分:80+85+4=169 一脸黑线.....是数据比较水的原因,T2分都比 ...
- python的基础认识
一.python的简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名 ...
- python学习基础—day01
一. python是什么? 优势:简单, 可以跨平台 劣势:执行效率没有C语言那么高 python是解释型语言,逐行编译解释,在不同的系统windows与Linux,需要不同的解释器来编译. 而编译型 ...
- Netty创建服务器与客户端
Netty 创建Server服务端 Netty创建全部都是实现自AbstractBootstrap.客户端的是Bootstrap,服务端的则是ServerBootstrap. 创建一个 HelloSe ...
- MAC OS下编译apple跨平台的libevent库 (可延申到其它第三库)
apple下的跨平台是指不同设备上的苹果系统以及同一系统在不同cpu体系的不同版本. 前面一篇介绍如何用ndk编译android跨平台的第三库,那样的方法却不能应用在apple上. 网上可以找到这么一 ...
- 11.13的C++##不想写结构,更不爱指针
//2019.11.13 卑微的Loving-Q瞎写的程序 报错请更改VS中的SDL检查// 我要去嗨了,在线卑微 1 #include<iostream> #include<std ...
- .NET做人脸识别并分类
.NET做人脸识别并分类 在游乐场.玻璃天桥.滑雪场等娱乐场所,经常能看到有摄影师在拍照片,令这些经营者发愁的一件事就是照片太多了,客户在成千上万张照片中找到自己可不是件容易的事.在一次游玩等活动或家 ...