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 ...
随机推荐
- MIT线性代数:3.矩阵相乘
- 学习笔记05一般处理程序ashx
1.获取由表单传过来的参数var value1 = HttpContext.Request["健"]; 2.使得网站目录下的相对路径转化为绝对路径:(用于文件操作)var file ...
- 单点登录 - API 认证系统 Passport(二)
安装 composer require laravel/passport=~4.0 notes: 1)确保系统安装unzip.zip等命令. 2)composer 安装出现 Authenticatio ...
- 2684亿!阿里CTO张建锋:不是任何一朵云都撑得住双11
2019天猫双11 成交额2684亿! "不是任何一朵云都能撑住这个流量.中国有两朵云,一朵是阿里云,一朵叫其他云."11月11日晚,阿里巴巴集团CTO张建锋表示,"阿里 ...
- 大数据之路day04_1--数组 and for循环进阶
Java数组 在开始之前,提一个十分重要的一点:注意:在给数组分配内存空间时,必须指定数组能够存储的元素来确定数组大小.创建数组之后不能修改数组的大小,可以使用length属性获取数组的大小.在jav ...
- 『题解』洛谷P3958 奶酪
Portal Portal1: Luogu Portal2: LibreOJ Portal3: Vijos Description 现有一块大奶酪,它的高度为\(h\),它的长度和宽度我们可以认为是无 ...
- linux中dd相关命令骚操作
一.dd如何快速将磁盘写满 方法一: dd if=/dev/zero of=/tmp/file bs=1G count=10 # 参数解释 1. if=文件名:输入文件名,缺省为标准输入.即指定源文件 ...
- 理解Spark SQL(一)—— CLI和ThriftServer
Spark SQL主要提供了两个工具来访问hive中的数据,即CLI和ThriftServer.前提是需要Spark支持Hive,即编译Spark时需要带上hive和hive-thriftserver ...
- 《JAVA 程序员面试宝典(第四版)》读书笔记之前言
工作五年了一直在小的软件公司混,总感觉自己的专业知识没有太大的提升.当然了中间也换了一两家公司,面试的公司就很多家,总感觉正规的软件公司(无论大小)对于基础知识的考核都非常重视,而不管你说你之前服务过 ...
- C# UTM坐标和WGS84坐标转换小工具
工具根据:http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html js代码改编 工具源码github:https://github. ...