12.10 Nginx访问日志php
12.11 Nginx日志切割css
12.12 静态文件不记录日志和过时时间html
12.10 Nginx访问日志:nginx
~1.日志格式shell
vim /usr/local/nginx/conf/nginx.conf //搜索log_formatvim
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
以上红色部分(combined_realip)为日志格式的名字,能够随意更改。好比改为“axin”,在这定义成什么名字,在后面引用他的时候就写成什么bash
在nginx中,命令是以分号(;)做为结束的。以上有分行,可是做为一串配置的服务器
$remote_addr 客户端IP(公网IP)。也就是远程的客户地址,不是咱们192.168这个,是咱们出口的IP。打开百度,搜索IP便可查询curl
$http_x_forwarded_for 代理服务器的IP工具
$time_local 服务器本地时间
$host 访问主机名(域名)好比咱们作测试,test.com.conf/admin/index.htmi, test.com.conf就是host
$request_uri 访问的url地址。好比咱们作测试,test.com.conf/admin/index.htmi, admin/index.html就是url
$status 状态码
$http_referer referer 从哪一个页面连接过来的
$http_user_agent user_agent 用户的一些信息
~1.1(实例:)
除了在主配置文件nginx.conf里定义日志格式外,还须要在虚拟主机配置文件中增长
access_log /tmp/test.com.log axin;
这里的combined_realip就是在nginx.conf中定义的日志格式名字
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log
实例:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
access_log /tmp/test.com.log axin;在主配置文件里,咱们刚才定义的访问日志的名字就叫axin,这里就要设定为axin
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown log format "axin" in /usr/local/nginx/conf/vhost/test.com.conf:10
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
nginx: [emerg] unknown log format "axin" in /usr/local/nginx/conf/vhost/test.com.conf:10
[root@localhost~]# curl -x127.0.0.1:80 test2.com/admin/index.html/hgjkjkgjkhj -I 测试登陆一下
HTTP/1.1 301 Moved Permanently Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 07:34:56 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/admin/index.html/hgjkjkgjkhj
[root@localhost ~]# cat /tmp/test.com.log cat一下
127.0.0.1 - - [23/Jul/2019:15:53:00 +0800] "HEAD HTTP://test2.com/admin/index.html/hgjkjkgjkhj HTTP/1.1" 301 0 "-" "curl/7.29.0"
12.11 Nginx日志切割: Nginx不像Apache那样自带日志切割的工具,因此要借助于系统的日志切割工具,或者本身写日志切割的脚本。一下是日志切割的脚本:
~~1. 自定义shell 脚本 ~1\. vim /usr/local/sbin/nginx\_log\_rotate.sh 写入以下内容(之后得shell脚本要放在/usr/local/sbin下面)
~2.#! /bin/bash \## 假设nginx的日志存放路径为/tmp/logs/ d=\`date -d "-1 day" +%Y%m%d\` 命令行会敲出昨天的日期。目的是生成昨天的日期 logdir="/tmp/logs" 日志路径 nginx_pid="/usr/local/nginx/logs/nginx.pid" 找pid是由于要执行最下面kill的命令。由于从新修改log的路径,他实际上还在写以前自带的日志,要把他kil掉。这个pid的路径要找对了,否则最下面的kill执行不成功 cd $logdir 先进入到logdir目录下 for log in \`ls *.log\` 看一下目录下有哪些log do mv $log $log-$d 给全部的log更名字。就是切割,好比在0点0分的时候执行,改革名字加个后缀,$d表明上面的变量d,也就是后缀名是昨天的日期 done /bin/kill -HUP \`cat $nginx_pid\` 最后从新加载,生成新的
~3.sh -x /usr/local/sbin/nginx\_log\_rotate sh即执行脚本,-x显示执行的过程
~4.find /tmp/ -type f -mtime +30 -name \*log-\* | xargs rm 能够作这项操做,删除30天之前的日志 ~~2. 任务计划 0 0 * * * /bin/bash /usr/local/sbin/nginx\_log\_rotate.sh 还要作一个任务计划,天天0点0分执行这个脚本
实例: ~~1.切割日志: [root@localhost ~\]# vim /usr/local/sbin/nginx\_log\_rotate.sh #! /bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/tmp/logs" 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@localhost logs\]# sh -x /usr/local/sbin/nginx\_log\_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20190722 + logdir=/tmp/logs + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/logs /usr/local/sbin/nginx_log_rotate.sh: 第 5 行:cd: /tmp/logs: 没有那个文件或目录 ++ ls '*.log' ls: 没法访问*.log: 没有那个文件或目录 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 3775 ~~2.任务计划: \[root@localhost logs\]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx\_log\_rotate.sh
12.12 静态文件不记录日志和过时时间:
配置以下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 匹配什么样的请求(也是正则)
{
expires 7d; 过时时间
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h; .js|css小文件的过时时间
access_log off;
}
实例:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } access_log /tmp/test.com.log; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
[root@localhost test.com]# /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 test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.jjpg -I 先访问一个没定义的.jjpg文件
HTTP/1.1 404 Not Found Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 09:04:11 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.jpg -I 访问1.jpg
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.gif -I 访问2.gif
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - - [23/Jul/2019:15:53:00 +0800] "HEAD HTTP://test2.com/admin/index.html/hgjkjkgjkhj HTTP/1.1" 301 0 "-" "curl/7.29.0" 127.0.0.1 - - [23/Jul/2019:17:04:11 +0800] "HEAD HTTP://test.com/1.jjpg HTTP/1.1" 404 0 "-" "curl/7.29.0"