分享

Nginx的访问日志,Nginx日志切割,Nginx不记录静态文件

 WindySky 2019-02-28

Nginx的访问日志

Nginx的日志格式是在Nginx的主配置文件中(/usr/local/nginx/conf/nginx.conf)

[root@shuai-01 vhost]# vim /usr/local/nginx/conf/nginx.conf

1

可以将日志格式名称改一下,改为shaui

Nginx日志字段的含义

在主配置文件中定义日志的格式,在虚拟主机配置文件中定义日志路径。

打开虚拟主机配置文件

[root@shuai-01 vhost]# vim .conf 

access_log /tmp/.log shuai;

1

2

3

注意,Nginx配置文件写完一行要加“;”,不然就是错误。

检查配置文件语法并重新加载配置文件

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

1

2

3

4

检测:

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.2

Date: Mon, 08 Jan 2018 12:41:20 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http:///index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.2

Date: Mon, 08 Jan 2018 12:41:26 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http:///index.html

[root@shuai-01 vhost]# cat /tmp/.log 

127.0.0.1 - [08/Jan/2018:20:41:20 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0"

127.0.0.1 - [08/Jan/2018:20:41:26 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0"

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Nginx日志切割

nginx由于没有自带的日志切割工具,在切割日志时,需要借助于系统带的日志切割工具,或者是自己写一个日志切割脚本。

自己写一个日志切割脚本。脚本统一保存/usr/local/sbin/ 

先自定义一个脚本:

[root@shuai-01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh

#! /bin/bash

## 假设nginx的日志存放路径为/tmp/

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

#定义切割时间(切割一天前的日志)

logdir="/tmp/"

#此处指定要切割的日志路径(该路径来自虚拟主机配置文件)

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

#调用pid的目的是执行命令:/bin/kill -HUP `cat $nginx_pid`

#该命令等价于命令:nginx -s reload(重新加载文件),确保与虚拟主机配置文件变更保持同步

#该地址来自nginx配置文件

cd $logdir

for log in `ls *.log`

do

    mv $log $log-$d

done

#此处使用通配进行循环,并改名字(切割是每天产生的日志重命名)

/bin/kill -HUP `cat $nginx_pid`

#执行此命令进行重载生成新的日志文件来记录新的日志

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

执行脚本:

[root@shuai-01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh 

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

+ d=20180108

+ logdir=/tmp/

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

+ cd /tmp/

++ ls .log

+ for log in '`ls *.log`'

+ mv .log .log-20180108

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

+ /bin/kill -HUP 1513

1

2

3

4

5

6

7

8

9

10

11

-x : 作用是显示脚本执行过程 

注意: 

这只是对日志进行了切割,对日志进行删除需要结合任务计划cron使用。切割也得配合cron使用。

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

在配置文件中加上配置:

打开配置文件:

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

{

    expires         7d;

    access_log off;

}

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

{

    expires         12h;

    acces_log off;

}

[root@shuai-01 vhost]# vim .conf

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

 #匹配文件类型

{

      expires      7d;

      #过期时间为7天

      access_log off;  

      #不记录该类型文件的访问日志     

}   

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

{

      expires      12h;

      #过期时间为12小时

      access_log off;

      #不记录该类型文件的访问日志

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

检查配置文件语法并重新加载配置文件:

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

1

2

3

4

测试:

[root@shuai-01 ]# curl -x127.0.0.1:80 /1.gif

shjdkjhkasb

[root@shuai-01 ]# curl -x127.0.0.1:80 /2.js

ajkfdchb

[root@shuai-01 ]# curl -x127.0.0.1:80 /index.html

[root@shuai-01 ]# cat /tmp/.log

127.0.0.1 - [09/Jan/2018:00:39:45 +0800] "/index.html" 200 "-" "curl/7.29.0"

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多