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 ...
随机推荐
- Python爬虫常用小技巧之设置代理IP
设置代理IP的原因 我们在使用Python爬虫爬取一个网站时,通常会频繁访问该网站.假如一个网站它会检测某一段时间某个IP的访问次数,如果访问次数过多,它会禁止你的访问.所以你可以设置一些代理服务器来 ...
- Ambari 集群时间同步配置教程
本文原始地址:https://sitoi.cn/posts/27560.html 步骤 在时间同步主节点创建 ntp.conf 文件 在时间同步从节点上创建 ntp.conf 文件 修改所有节点时区 ...
- CVE-2019-13272 Linux kernel 权限许可和访问控制问题漏洞
漏洞简介: Linuxkernel是美国Linux基金会发布的开源操作系统Linux所使用的内核. Linuxkernel5.1.17之前版本中存在安全漏洞,该漏洞源于kernel/ptrace.c文 ...
- 【XSY1986】【BZOJ1455】罗马游戏
就是一模板题. 合并就直接merge. pop就是将自己的值设为一,再将自己的左右儿子合并即可. 查询直接找到堆顶,输出. 模板左偏树 代码: #include<bits/stdc++.h> ...
- 学习笔记53_C#操作MongoDB
1.配置MongoDB的连接字符串 MongoDB程序集引用 在使用db.GetCollerction<T>,也可以不指定类,因为Mongodb是无模式的. ****关系型数据设计转化为j ...
- 利用python的requests和BeautifulSoup库爬取小说网站内容
1. 什么是Requests? Requests是用Python语言编写的,基于urllib3来改写的,采用Apache2 Licensed 来源协议的HTTP库. 它比urllib更加方便,可以节约 ...
- OV5640摄像头配置一些值得注意的关键点(三)
一.字节标志的注意点 由于摄像头的输出是RGB56格式,所以需要将两帧的数据进行拼接,之后送到上位机进行显示. reg byte_flag; always@(posedge cmos_pclk_i) ...
- 等距结点下的Newton插值多项式系数计算(向前差分)
插值多项式的牛顿法 1.为何需要牛顿法? 使用Lagrange插值法不具备继承性.当求好经过\(({x_0},{y_0})-({x_n},{y_n})\)共n+1个点的插值曲线时候,如果再增加一个 ...
- [考试反思]1105csp-s模拟测试102: 贪婪
还是有点蠢... 多测没清空T3挂40...(只得了人口普查分20) 多测题要把样例复制粘两遍自测一下防止未清空出锅. 然而不算分... 其实到现在了算不算也不重要了吧... 而且其实T3只考虑最长路 ...
- day 1 晚上 P2824 [HEOI2016/TJOI2016]排序 线段树
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #inclu ...