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

发布时间:2025-02-14 点击:31
12.10 nginx访问日志
之前拷贝的nginx.conf下就有关于访问日志的相关
打开配置文件
[root@aminglinux-02 ~]# vim /usr/local/nginx/conf/nginx.conf log_format combined_realip \\\’$remote_addr $http_x_forwarded_for [$time_local]\\\’ \\\’ $host "$request_uri" $status\\\’ \\\’ "$http_referer" "$http_user_agent"\\\’;
combined_realip 日志格式的名字,决定了虚拟主机引用日志的类型
nginx配置文件,以 “ ; ” 分号结尾,配置文件一段如果没有 分号结尾,表示这一段还没有结束,就算中间执行了换行。
$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 combined_realip;
配置好以后
检查语法 && 重新加载服务
-t && -s reload
测试是否产生日志
[root@aminglinux-02 ~]# curl -x127.0.0.1:80 test2.com/admin/index.html -i http/1.1 301 moved permanently server: nginx/1.12.1 date: fri, 11 aug 2017 16:51:26 gmt content-type: text/html content-length: 185 connection: keep-alive location: http://test.com/admin/index.html [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test2.com -i http/1.1 301 moved permanently server: nginx/1.12.1 date: fri, 11 aug 2017 16:51:31 gmt content-type: text/html content-length: 185 connection: keep-alive location: http://test.com/ [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test1.com -i http/1.1 200 ok server: nginx/1.12.1 date: fri, 11 aug 2017 16:51:36 gmt content-type: text/html content-length: 26 last-modified: thu, 10 aug 2017 16:27:09 gmt connection: keep-alive etag: "598c895d-1a" accept-ranges: bytes [root@aminglinux-02 ~]# cat /tmp/test.com.log 127.0.0.1 – [12/aug/2017:00:51:17 0800] test3.com "/admin/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 – [12/aug/2017:00:51:26 0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 – [12/aug/2017:00:51:31 0800] test2.com "/" 301 "-" "curl/7.29.0" 12.11 nginx日志切割
nginx没有自带日志切割工具
只能借助系统自带的工具或者使用脚本实现
创建shell脚本
规则:所有的shell的脚本,全部存放在/usr/local/sbin/ 目录下
[root@aminglinux-02 ~]# vim /usr/local/sbin/nginx_logrotate.sh #! /bin/bash ## 假设nginx的日志存放路径为/data/logs/ d=`date -d "-1 day" %y%m%d` // 生成昨天的日期,格式为年月日 logdir="tmp/" // 上一节的时候,定义了日志存放在/tmp/目录下 nginx_pid="/usr/local/nginx/logs/nginx.pid" //查找nginx的pid cd $logdir //进入“logdir”目录 for log in `ls *.log` //开始语句循环,看错有哪些 log后缀的文件 do //执行 mv $log $log-$d //将 log改名为《原名字-“`date -d "-1 day" %y%m%d` ”这个结尾的文件 》 done //结束 /bin/kill -hup `cat $nginx_pid` // 重新加载,生成一个新的“ nginx_pid="/usr/local/nginx/logs/nginx.pid" ”
执行脚本
sh -x /usr/local/sbin/nginx_logrotate.sh
-x选项使运行过程可见
[root@aminglinux-02 ~]# sh -x /usr/local/sbin/nginx_logrotate.sh date -d \\\’-1 day\\\’ %y%m%d d=20170811 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-20170811 cat /usr/local/nginx/logs/nginx.pid /bin/kill -hup 1202 知识点: 一、日志时间切割的定义
写shell脚本的时候,如果有命令不明白,可以直接把命令运行一下就知道结果了
假设这个命令“ d=date -d "-1 day" %y%m%d ”不明白意思
ctrl z 把当前操作暂停丢到后台
运行命令看看结果
[root@aminglinux-02 ~]# date -d "-1 day" %y%m%d 20170811 [root@aminglinux-02 ~]# dat

跨境电商零售进口商品单次交易限值提高至5000元
腾讯云备案关联云服务器
如何投资域名?投资域名要注意什么?
中国信通院报告显示:2019年我国域名注册量突破5100万个
主机进不了后台-虚拟主机/数据库问题
小程序咨询-虚拟主机/数据库问题
防ddos攻击原理
阿里云轻量服务器香港不能买