网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

LNMP架构之访问日志、日志切割、静态文件不记录及过期时间设置

本文索引:

10多年的绥江网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整绥江建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“绥江网站设计”,“绥江网站推广”以来,每个客户项目都认真落实执行。

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


Nginx访问日志 修改nginx配置文件

[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf # 搜索:/log_format # 在nginx中以;作为一行的结尾,所以下列代码时一个配置 # 格式:“log_format 日志格式名 格式;” log_format test \'$remote_addr $http_x_forwarded_for [$time_local]\' \' $host "$request_uri" $status\' \' "$http_referer" "$http_user_agent"\';

格式内使用的变量说明如下:

变量名说明 $remote_addr客户端ip(公网ip) $http_x_forwarded_for代理服务器的ip $time_local服务器本地时间 $host访问主机名(域名) $request_uri访问的url地址 $status状态码 $http_refererreferer $http_user_agentuser_agent 在虚拟主机内定义日志路径

# 在server块内插入 access_log /tmp/test.com.log test; # 格式为access_log 日志存放路径 日志格式名(主配置文件内定义的) 重启服务

[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload 验证效果

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index.php [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index1.php # 成功记录 [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:19:05:22 +0800] test.com "/" 401 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:19:05:34 +0800] test2.com "/index.php" 301 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:19:05:39 +0800] test2.com "/index1.php" 301 "-" "curl/7.29.0" Nginx日志切割

nginx没有apache内的rotatelog日志切割命令,我们可以通过自定义shell脚本来实现日志切割的功能。

创建自定义脚本

[root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh [root@localhost vhost]# cat /usr/local/sbin/nginx_log_rotate.sh #!/bin/bash # date +%Y%m%d 显示的是今天的日期 # 加上 -d "-1 day" 显示的是昨天的日期 d=`date -d "-1 day" +%Y%m%d` # 定义日志存放的路径,虚拟主机配置文件内定义 logdir="/tmp" # pid文件 nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir # 在日志存放路径下循环更改日志文件名 for log in `ls *.log` do mv $log $log-$d done # 在不关闭进程前提下重启,等价于nginx -s reload /bin/kill -HUP `cat $nginx_pid` 脚本测试

# sh -x 可以显示脚本执行的过程 [root@localhost vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d \'-1 day\' +%Y%m%d + d=20180102 + 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-20180102 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1299 # 查看是否实现功能 [root@localhost vhost]# ls /tmp/*.log* /tmp/test.com.log /tmp/test.com.log-20180102 配合crontab命令周期性执行

[root@localhost vhost]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh


静态文件不记录日志和过期时间 修改虚拟主机配置文件

[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf # ~ 匹配后续的正则表示 # 使用转义.,匹配.jpg等文件 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { # expires设置过期时间 expires 7d; # 关闭日志记录 access_log off; } location ~ .*.(css|js)$ { expires 12h; access_log off; } 测试 验证静态文件不记录日志

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php 401 Authorization Required

401 Authorization Required


nginx/1.12.2
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif jhasdifhoai [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js abdsuofghan # gif/js文件日志未记录访问日志 [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:19:36:25 +0800] test.com "/index.php" 401 "-" "curl/7.29.0" 验证过期时间 测试gif的过期时间

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:36:52 GMT Content-Type: image/gif Content-Length: 12 Last-Modified: Wed, 03 Jan 2018 11:35:29 GMT Connection: keep-alive ETag: "5a4cc001-c" Expires: Wed, 10 Jan 2018 11:36:52 GMT # 信息内显示max-age时间 Cache-Control: max-age=604800 Accept-Ranges: bytes 测试js文件的过期时间

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:36:58 GMT Content-Type: application/javascript Content-Length: 12 Last-Modified: Wed, 03 Jan 2018 11:35:44 GMT Connection: keep-alive ETag: "5a4cc010-c" Expires: Wed, 03 Jan 2018 23:36:58 GMT # 信息内显示max-age时间 Cache-Control: max-age=43200 Accept-Ranges: bytes 测试PHP文件,没有max-age信息

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:46:58 GMT Content-Type: application/octet-stream Content-Length: 19 Last-Modified: Wed, 03 Jan 2018 11:36:44 GMT Connection: keep-alive ETag: "5a4e1572-13" Accept-Ranges: bytes



标题名称:LNMP架构之访问日志、日志切割、静态文件不记录及过期时间设置
分享链接:http://bjjierui.cn/article/cpegho.html

其他资讯