48.Nginx访问日志 日志切割 静态文件不记录

12.10 Nginx访问日志

12.11 Nginx日志切割

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

12.10 Nginx访问日志:

~1.日志格式

vim /usr/local/nginx/conf/nginx.conf //搜索log_format

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

' $host "$request_uri" $status'

' "$http_referer" "$http_user_agent"';

以上红色部分(combined_realip)为日志格式的名字,可以随意更改。比如改成“axin”,在这定义成什么名字,在后面引用他的时候就写成什么

在nginx中,命令是以分号(;)作为结束的。以上有分行,但是作为一串配置的

$remote_addr 客户端IP(公网IP)。也就是远程的客户地址,不是我们192.168这个,是我们出口的IP。打开百度,搜索IP即可查询

$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@axinlinux-01 ~]# 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@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

nginx: [emerg] unknown log format "axin" in /usr/local/nginx/conf/vhost/test.com.conf:10

[root@axinlinux-01 ~]# 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, 14 Aug 2018 12:50:56 GMT

Content-Type: text/html

Content-Length: 184

Connection: keep-alive

Location: http://test.com/admin/index.html/hgjkjkgjkhj

[root@axinlinux-01 ~]# cat /tmp/test.com.log cat一下

127.0.0.1 - [14/Aug/2018:20:50:56 +0800] test2.com "/admin/index.html/hgjkjkgjkhj" 301 "-" "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@axinlinux-01 ~\]# 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@axinlinux-01 logs\]# sh -x /usr/local/sbin/nginx\_log\_rotate.sh

\+\+ date -d '-1 day' +%Y%m%d

\+ d=20180813

\+ logdir=/tmp/logs

\+ nginx_pid=/usr/local/nginx/logs/nginx.pid

\+ cd /tmp/logs

\+\+ ls '*.log'

ls: 无法访问*.log: 没有那个文件或目录

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

\+ /bin/kill -HUP 1040

~~2.任务计划:

\[root@axinlinux-01 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@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 7d;

access_log off;

}

location ~ .*\.(js|css)$

{

expires 12h;

access_log off;

}

[root@axinlinux-01 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@axinlinux-01 test.com]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 test.com]# curl -x127.0.0.1:80 test.com/1.jjpg -I 先访问一个没定义的.jjpg文件

[root@axinlinux-01 test.com]# curl -x127.0.0.1:80 test.com/1.jpg -I 访问1.jpg

[root@axinlinux-01 test.com]# curl -x127.0.0.1:80 test.com/2.gif -I 访问2.gif

[root@axinlinux-01 test.com]# cat /tmp/test.com.log

127.0.0.1 - [14/Aug/2018:20:50:56 +0800] test2.com "/admin/index.html/hgjkjkgjkhj" 301 "-" "curl/7.29.0"

127.0.0.1 - [14/Aug/2018:22:39:20 +0800] test.com "/1.jjpg" 404 "-" "curl/7.29.0" 成功

转载于:https://my.oschina.net/u/3866149/blog/1927978

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值