您的位置:首页 > 运维架构 > Nginx

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

2018-01-04 22:08 1116 查看

12.10 Nginx访问日志

日志格式

#搜索log_format
[root@taoyuan ~]# vim /usr/local/nginx/conf/nginx.conf




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

参数含义
$remot_addr客户端IP(公网IP)
$http_x_forwarded_for代理服务器的IP
$time_local服务器本地时间
$host访问主机名(域名)
$request_uri访问的url地址
$status状态码
$http_refererreferer
$http_user_agentuser_agent
除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加如下的参数

[root@taoyuan ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
#必须根Nginx.conf 中的日志名字是一致的




注:taoyuan为日志格式的名字

-t && -s reload

[root@taoyuan ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown log format "taoyuan" in /usr/local/nginx/conf/vhost/test.com.conf:10
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@taoyuan ~]# /usr/local/nginx/sbin/nginx -s reload
nginx: [emerg] unknown log format "taoyuan" in /usr/local/nginx/conf/vhost/test.com.conf:10


curl 测试

[root@taoyuan ~]# curl -x127.0.0.1:80 test4.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 13:22:44 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@taoyuan ~]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 13:23:02 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html 
[root@taoyuan ~]# 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 13:23:10 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html


查看日志

[root@taoyuan ~]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:21:23:02 +0800] test3.com "/admin/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:21:23:10 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"

12.11 Nginx日志切割

自定义shell日志切割脚本

#创建shell脚本
[root@taoyuan ~]# vim /usr/local/sbin/nginx_log_rotate.sh

#写入如下的内容
#!/bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"
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@taoyuan ~]# 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 test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180103
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1055


加任务计划

[root@taoyuan ~]# crontab -e
#加入如下内容
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

#在/tmp/目录下创建 test.com.log-20180109 已时间结尾的日志文件

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

配置如下

#修改虚拟主机配置文件
[root@taoyuan local]# cd /usr/local/nginx/conf/vhost/
[root@taoyuan vhost]# ls
aaa.com.conf  test.com.conf
[root@taoyuan vhost]# vim test.com.conf

#内容
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires     7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires     12h;
access_log off;
}

截图如下:



-t && -s reload

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


测试

[root@taoyuan ~]# cd /data/wwwroot/test.com/

[root@taoyuan test.com]# vim 1.gif
[root@taoyuan test.com]# vim 2.js
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/1.gif
1.gif
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/2.js
2.js
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@taoyuan test.com]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:04:11 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@taoyuan test.com]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:04:11 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:22:04:33 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

#测试过期时间
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:06:15 GMT
Content-Type: application/javascript
Content-Length: 5
Last-Modified: Thu, 04 Jan 2018 14:03:28 GMT
Connection: keep-alive
ETag: "5a4e3430-5"
Expires: Fri, 05 Jan 2018 02:06:15 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
#在配置文件中 expires 是控制过期时间的,如果注释掉 -I将不显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐