码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-06 10:48:53      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:Linux

Nginx访问日志


1. 进入配置文件

[root@gary-tao src]# vim /usr/local/nginx/conf/nginx.conf  //搜索log_format


参考更改配置成如下:

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

如图:

技术分享图片



日志格式字段含义如下:

combined_realip为日志格式的名字,后面可以调用它。

技术分享图片


2.到虚拟主机配置文件中指定访问日志的路径

[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf


增加如下内容:

access_log /tmp/test.com.log combined_realip;  

//这里的combined_realip就是在nginx.conf中定义的日志格式名字

如图:

技术分享图片


3.测试语法及重新加载配置

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


4.使用curl测试

[root@gary-tao src]# curl -x127.0.0.1:80 test.com -I

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 09:19:09 GMT

Content-Type: text/html

Content-Length: 15

Last-Modified: Wed, 03 Jan 2018 13:03:42 GMT

Connection: keep-alive

ETag: "5a4cd4ae-f"

Accept-Ranges: bytes


[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/admin -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 09:19:54 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://test.com/admin


[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/admin/index.html -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 09:20:04 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

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


[root@gary-tao src]# cat /tmp/test.com.log 

127.0.0.1 - [04/Jan/2018:17:19:09 +0800] test.com "/" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:17:19:54 +0800] test2.com "/admin" 301 "-" "curl/7.29.0"

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



Nginx日志切割

1.自定义一个脚本

[root@gary-tao src]# vim /usr/local/sbin/nginx_log_rotate.sh


定义如下内容:

#!/bin/bash

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

d=`date -d "-1 day" +%Y%m%d`   //这个日期是昨天的日期,因为日志切割是第二天才执行这个脚本的。

logdir="/data/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` //跟Nginx的-s重新加载配置文件一样


2.执行脚本

sh执行,-x是显示执行的过程


[root@gary-tao src]# sh -x /usr/local/sbin/nginx_log_rotate.sh

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

+ d=20180103

+ logdir=/tmp/

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

+ cd /tmp/

++ ls php_errors.log test.com.log

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

+ mv php_errors.log php_errors.log-20180103

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

+ mv test.com.log test.com.log-20180103

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

+ /bin/kill -HUP 62748


[root@gary-tao src]# ls /tmp/

mysql.sock                                                                systemd-private-b666888e47f84d62afce0dcb90bdfc91-vmtoolsd.service-09q5L2

pear                                                                      systemd-private-fdc53ff508e94ecda3c5a90dad98a792-vmtoolsd.service-O5O620

php_errors.log-20180103                                                   test.com.log

php-fcgi.sock                                                             test.com.log-20180103

systemd-private-6fc6799999fe42dd97426bde338fb145-vmtoolsd.service-qi1M6k


3.任务计划

[root@gary-tao src]# crontab -e //添加任务计划

增加如下内容:

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh



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

1.修改虚拟主机配置文件

[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf


增加如下内容:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  //匹配脱义静态文件

    {

          expires      7d;   //配置过期时间

          access_log off;

    }

location ~ .*\.(js|css)$    //匹配js,css文件

    {

          expires      12h;

          access_log off;

    }


如图:

技术分享图片


2.测试语法及重新加载配置

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


3.使用curl测试

[root@gary-tao src]# cd /data/wwwroot/test.com

[root@gary-tao test.com]# ls

admin  index.html

[root@gary-tao test.com]# vim 1.gif

[root@gary-tao test.com]# echo "dgagadgadgs" > /data/wwwroot/test.com/2.js

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/1.gif

dggagadggagdag

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.js

dgagadgadgs

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/index.html

“test.com”

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/index.html

“test.com”

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.js

dgagadgadgs

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.jsagdaga

<html>

<head><title>404 Not Found</title></head>

<body bgcolor="white">

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.12.1</center>

</body>

</html>

[root@gary-tao test.com]# cat /tmp/test.com.log

127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [04/Jan/2018:18:55:22 +0800] test.com "/2.jsagdaga" 404 "-" "curl/7.29.0"

[root@gary-tao test.com]# curl -x127.0.0.1:80 -I test.com/2.js

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Thu, 04 Jan 2018 10:56:11 GMT

Content-Type: application/javascript

Content-Length: 12

Last-Modified: Thu, 04 Jan 2018 10:51:59 GMT

Connection: keep-alive

ETag: "5a4e074f-c"

Expires: Thu, 04 Jan 2018 22:56:11 GMT

Cache-Control: max-age=43200  //43200秒表示过期时间12小时,与配置文件里一样。

Accept-Ranges: bytes


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

标签:Linux

原文地址:http://blog.51cto.com/ccj168/2083281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!