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

Posted 淡淡!!!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间相关的知识,希望对你有一定的参考价值。

nginx访问日志

1. 进入配置文件
[[email protected] linux]# vim /usr/local/nginx/conf/nginx.conf  //搜索log_format

参考更改配置成如下:

log_format aming ‘$remote_addr $http_x_forwarded_for [$time_local]‘
如图:

技术分享图片

日志格式字段含义如下:

combined_realip为日志格式的名字,后面可以调用它。
技术分享图片

2.到虚拟主机配置文件中指定访问日志的路径。
[[email protected] linux]# vim /usr/local/nginx/conf/vhost/test.com.conf

增加如下内容:

access_log /tmp/test.com.log combined_realip;  

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

技术分享图片

3.测试语法及重新加载配置
[[email protected] linux]# /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
[[email protected] linux]# /usr/local/nginx/sbin/nginx -s reload
4.使用curl测试
[[email protected] linux]# 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

[[email protected] linux]# 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

[[email protected] linux]# 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

[[email protected] linux]# 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.自定义一个脚本
[[email protected] linux]# 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是显示执行的过程

[[email protected] linux]# 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

[[email protected] linux]# 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.任务计划
[[email protected] linux]# crontab -e //添加任务计划

增加如下内容:

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

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

1.修改虚拟主机配置文件
[[email protected] linux]# 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.测试语法及重新加载配置
[[email protected] linux]# /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
[[email protected] linux]# /usr/local/nginx/sbin/nginx -s reload
3.使用curl测试
[[email protected]g linux]# cd /data/wwwroot/test.com
[[email protected] test.com]# ls
admin  index.html
[[email protected] test.com]# vim 1.gif
[[email protected] test.com]# echo "dgagadgadgs" > /data/wwwroot/test.com/2.js
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/1.gif
dggagadggagdag
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js
dgagadgadgs
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.html
“test.com”
[[email protected] 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"
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.html
“test.com”
[[email protected] 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"
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js
dgagadgadgs
[[email protected] 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"
[[email protected] 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>
[[email protected] 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"
[[email protected] 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

以上是关于12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间的主要内容,如果未能解决你的问题,请参考以下文章

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

6月8日任务(12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件)

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

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

linux十二周三次课 (4月25日)笔记

2018-6-8