LNMP架构之访问日志、日志切割、静态文件不记录及过期时间设置

Wesley13
• 阅读 669

本文索引:

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

Nginx访问日志

  • 修改nginx配置文件

    [root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf

    搜索:/log_format

    在nginx中以;作为一行的结尾,所以下列代码时一个配置

    格式:“log_format 日志格式名 格式;”

    log_format test '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';

格式内使用的变量说明如下:

变量名

说明

$remote_addr

客户端ip(公网ip)

$http_x_forwarded_for

代理服务器的ip

$time_local

服务器本地时间

$host

访问主机名(域名)

$request_uri

访问的url地址

$status

状态码

$http_referer

referer

$http_user_agent

user_agent

  • 在虚拟主机内定义日志路径

    在server块内插入

    access_log /tmp/test.com.log test;

    格式为access_log 日志存放路径 日志格式名(主配置文件内定义的)

  • 重启服务

    [root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

  • 验证效果

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com

    [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index.php

    [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index1.php

    成功记录

    [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:19:05:22 +0800] test.com "/" 401 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:19:05:34 +0800] test2.com "/index.php" 301 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:19:05:39 +0800] test2.com "/index1.php" 301 "-" "curl/7.29.0"

Nginx日志切割

nginx没有apache内的rotatelog日志切割命令,我们可以通过自定义shell脚本来实现日志切割的功能。

  • 创建自定义脚本

    [root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh [root@localhost vhost]# cat /usr/local/sbin/nginx_log_rotate.sh #!/bin/bash

    date +%Y%m%d 显示的是今天的日期

    加上 -d "-1 day" 显示的是昨天的日期

    d=date -d "-1 day" +%Y%m%d

    定义日志存放的路径,虚拟主机配置文件内定义

    logdir="/tmp"

    pid文件

    nginx_pid="/usr/local/nginx/logs/nginx.pid"

    cd $logdir

    在日志存放路径下循环更改日志文件名

    for log in ls *.log do mv $log $log-$d done

    在不关闭进程前提下重启,等价于nginx -s reload

    /bin/kill -HUP cat $nginx_pid

  • 脚本测试

    sh -x 可以显示脚本执行的过程

    [root@localhost vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d

    • d=20180102
    • 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-20180102

    ++ cat /usr/local/nginx/logs/nginx.pid

    • /bin/kill -HUP 1299

    查看是否实现功能

    [root@localhost vhost]# ls /tmp/.log /tmp/test.com.log /tmp/test.com.log-20180102

  • 配合crontab命令周期性执行

    [root@localhost vhost]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh


静态文件不记录日志和过期时间

  • 修改虚拟主机配置文件

    [root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf

    ~ 匹配后续的正则表示

    使用\转义.,匹配.jpg等文件

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { # expires设置过期时间 expires 7d;
    # 关闭日志记录 access_log off; } location ~ .*.(css|js)$ { expires 12h; access_log off; }

  • 测试

  1. 验证静态文件不记录日志

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php

    401 Authorization Required

    401 Authorization Required


    nginx/1.12.2

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif jhasdifhoai

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js abdsuofghan

    gif/js文件日志未记录访问日志

    [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:19:36:25 +0800] test.com "/index.php" 401 "-" "curl/7.29.0"

  2. 验证过期时间

  • 测试gif的过期时间

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:36:52 GMT Content-Type: image/gif Content-Length: 12 Last-Modified: Wed, 03 Jan 2018 11:35:29 GMT Connection: keep-alive ETag: "5a4cc001-c" Expires: Wed, 10 Jan 2018 11:36:52 GMT

    信息内显示max-age时间

    Cache-Control: max-age=604800 Accept-Ranges: bytes

  • 测试js文件的过期时间

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:36:58 GMT Content-Type: application/javascript Content-Length: 12 Last-Modified: Wed, 03 Jan 2018 11:35:44 GMT Connection: keep-alive ETag: "5a4cc010-c" Expires: Wed, 03 Jan 2018 23:36:58 GMT

    信息内显示max-age时间

    Cache-Control: max-age=43200 Accept-Ranges: bytes

  • 测试PHP文件,没有max-age信息

    [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:46:58 GMT Content-Type: application/octet-stream Content-Length: 19 Last-Modified: Wed, 03 Jan 2018 11:36:44 GMT Connection: keep-alive ETag: "5a4e1572-13" Accept-Ranges: bytes


点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
可莉 可莉
2年前
12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配置 12.16 Nginx代理
12.13Nginx防盗链因为该配置也使用location板块,所以本节可结合日志管理(不记录和过期时间)一起配置:root@cham002~vim/usr/local/nginx/conf/vhost/test.com.conflocation~^.\.(gif|jp
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Logstash收集nginx访问日志和错误日志
1、收集访问日志1)、首先是要在nginx里面配置日志格式化输出log_formatmain"$http_x_forwarded_for|$time_local|$request|$status|$body_bytes_sent|$request_body|$content_length|$http_ref
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Wesley13 Wesley13
2年前
LAMP架构之访问日志的设置及切割、静态文件失效设置
本文索引访问日志不记录静态文件访问日志切割静态文件过期时间访问日志不记录静态文件为什么要这样设置网站大多为静态网页,网页内部的图片、css文件等同样有其网址链接,如果不设置,这些无效的信息也将被存入访问日志中,会导致访问日志文件大小快速增加,占用大量存储空间。我们可以通过设置不记录某
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这