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访问日志、日志切割、静态文件不记录日志和过期时间的更多相关文章

  1. Nginx访问日志 Nginx日志切割 静态文件不记录日志和过期时间

  2. Nginx访问日志、 Nginx日志切割、静态文件不记录日志和过期时间

    1.Nginx访问日志 配制访问日志:默认定义格式: log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_loc ...

  3. Linux centosVMware Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间

    一.Nginx访问日志 vim /usr/local/nginx/conf/nginx.conf //搜索log_format  日至格式 改为davery格式 $remote_addr  客户端IP ...

  4. nginx日志、nginx日志切割、静态文件不记录日志和过期时间

    2019独角兽企业重金招聘Python工程师标准>>> 12.10 Nginx访问日志 日志格式 vim /usr/local/nginx/conf/nginx.conf //搜索l ...

  5. Linux centos7 VMware Apache访问日志不记录静态文件、访问日志切割、静态元素过期时间

    一.Apache访问日志不记录静态文件 网站大多元素为静态文件,如图片.css.js等,这些元素可以不用记录 vim /usr/local/apache2.4/conf/extra/httpd-vho ...

  6. Nginx对于图片,js等静态文件的缓存设置

    以下是自学it网--中级班上课笔记 网址:www.zixue.it Nginx对于图片,js等静态文件的缓存设置 注:这个缓存是指针对浏览器所做的缓存,不是指服务器端的数据缓存. 主要知识点: loc ...

  7. Apache配置 5.访问日志不记录静态文件

    介绍:项目中的CSS.图片.js都是静态文件.一般会将静态文件放到一个单独的目录中,以方便管理. 1. 配置 # vim /usr/local/apache2.4/conf/extra/httpd-v ...

  8. nginx与tomcat 组合 实现静态文件和jsp组合访问

    主要修改nginx的配置文件: 设置代理 location /{proxy_pass http://47.94.158.2:8080;proxy_redirect off;proxy_set_head ...

  9. 使用nginx缓存服务器上的静态文件

    一.nginx缓存的优点 如图所示,nginx缓存,可以在一定程度上,减少源服务器的处理请求压力. 因为静态文件(比如css,js, 图片)中,很多都是不经常更新的.nginx使用proxy_cach ...

随机推荐

  1. 基于node的前端组件包发布至nexus和npmjs

    目录 目录... 3 1. 前言... 1 2. 配置... 1 2.1. 建立组件的导出模块... 1 2.2. 建立组件入口文件... 1 2.3. 配置“ng-package.json”文件.. ...

  2. 5. SOFAJRaft源码分析— RheaKV中如何存放数据?

    概述 上一篇讲了RheaKV是如何进行初始化的,因为RheaKV主要是用来做KV存储的,RheaKV读写的是相当的复杂,一起写会篇幅太长,所以这一篇主要来讲一下RheaKV中如何存放数据. 我们这里使 ...

  3. NOIP模拟 2

    大概就是考试的时候慌的一批,因为一道正解也没想出来,T1,T3只会暴搜,听见天皇在旁边的窃喜声本渣内心是崩溃的 会打暴搜的我先打了暴搜,大多数时间都用在第二题上,妄想自己能拿50多分- 最后半小时万念 ...

  4. Vue使用antV G2制作看板

    工作中需要制作一个看板,选型选用antV G2进行开发. 由于项目前端是使用Vue,于是研究了antVG2在Vue中的使用. 1.安装antv/g2 npm install @antv/g2 --sa ...

  5. 20190630A(贪心)

    题目描述 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...

  6. MySQL系列:MySQL的基本使用

    数据库的基本操作 在MySQL数据库中,对于一个MySQL示例,是可以包含多个数据库的. 在连接MySQL后,我们可以通过 show databases; 来进行查看有那么数据库.这里已经存在一些库了 ...

  7. 010.Kubernetes二进制部署kube-controller-manager

    一 部署高可用kube-controller-manager 1.1 高可用kube-controller-manager介绍 本实验部署一个三实例 kube-controller-manager 的 ...

  8. (数据科学学习手札71)在Python中制作个性化词云图

    本文对应脚本及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 一.简介 词云图是文本挖掘中用来表征词频的数据可视化 ...

  9. 过滤条件的时候用between和<>的区别

    无论是sqlsever还是oracle都支持between函数, 2个函数的基本语法是 WHERE A BETWEEN 1 AND 2/ WHERE A >=1 AND A <=2 ,be ...

  10. SpringSecurity系列之自定义登录验证成功与失败的结果处理

    一.需要自定义登录结果的场景 在我之前的文章中,做过登录验证流程的源码解析.其中比较重要的就是 当我们登录成功的时候,是由AuthenticationSuccessHandler进行登录结果处理,默认 ...