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 ...
随机推荐
- 为什么磁盘慢会导致Linux负载飙升?
一.CPU利用率和负载率的区别 这里要区别CPU负载和CPU利用率,它们是不同的两个概念,但它们的信息可以在同一个top命令中进行显示.CPU利用率显示的是程序在运行期间实时占用的CPU百分比,这是对 ...
- CSP2019知识点整理
也算是接下来二十天的复习计划吧 仅止于联赛难度左右 基础算法 字符串 char[] cstring memset() 输入无& gets(), fgets(stdin, ,); strcmp, ...
- 暑期集训20190726 跳动(skip)
[题目描述] 福州三中的操场上有着数不尽的跳动的小朋友. 当然善于思考的你总能从中发掘出不一样的问题 福州三中的跑道是一个n个格子围成的圆形,从0~n-1编号,有m个同学,第i个同学步长为a[i], ...
- CSPS模拟测试59
这场考得我心态爆炸......... 开场T1只会$n^{2}$,然后发现bfs时每个点只需要被更新一次,其他的更新都是没用的. 也就是说,我们可以只更新还没被更新的点? 于是我先YY了一个链表,发现 ...
- 机器学习之scikit-learn库
前面讲到了,这个库适合学习,轻量级,所以先学它. 安装就不讲了,简单.不过得先安装numpy和pandas库才能安装scikit-learn库. 如果安装了anaconda得话,会自带有这个库. -- ...
- 使用Typescript重构axios(三)——实现基础功能:处理get请求url参数
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- 考试T1护花
传送门 这题的提议似乎有什么问题,只要约翰选好了要抓那头牛,他就不会吃草了,站在原地傻等? 这题就是贪心,但在用cmp中比较单位时间吃草数量时,要用double型,不然可能会有点一样... 还有就是主 ...
- Zabbix-(三)监控主机CPU、磁盘、内存并创建监控图形
Zabbix-(三)监控主机CPU.磁盘.内存并创建监控图形 一.前言 前文中已经讲述了两种方式对Zabbix的搭建,本文将讲述如何在zaibbx上添加需要监控的主机,以及使用Zabbix自带模板和自 ...
- 微擎使用post提交,并显示弹出层
微擎使用post提交,并显示弹出层 function changeStatus(id, status) { // 提交数据 var id = parseInt(id); var status = pa ...
- nyoj 35-表达式求值(stack, 栈的应用)
35-表达式求值 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:37 submit:53 题目描述: ACM队的mdd想做一个计算器,但是,他要做的 ...