12.10 Nginx访问日志

Linux学习笔记十二周三次课 (4月25日)

Linux学习笔记十二周三次课 (4月25日)

vim /usr/local/nginx/conf/nginx.conf //搜索log_format

$remote_addr //客户端P(公网IP)

$http_x_forwarded_for //代理服务器的IP

$time_local //服务器本地时间

$host //访问主机名(域名)

$request_uri //访问的url地址

$status //状态码

$http_referer //referer

$http_user_agent //user_agent

Linux学习笔记十二周三次课 (4月25日)Linux学习笔记十二周三次课 (4月25日)

vim /usr/local/nginx/conf/vhost/test.com.conf  //编辑配置文件,在server里增加下面一行

----------------------------------------------------------------------------

access_log /tmp/test.com.log combined_realip

----------------------------------------------------------------------------

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

/usr/local/nginx/sbin/nginx -t //检查配置是否有错

/usr/local/nginx/sbin/nginx -s reload //重新加载,也可以重启服务

curl -x127.0.0.1:80 test.com -I

cat /tmp/test.com.log


12.11 Nginx日志切割

Linux学习笔记十二周三次课 (4月25日)

Linux学习笔记十二周三次课 (4月25日)

vim /usr/local/sbin/nginx_log_rotate.sh //写入如下内容

------------------------------------------------------------------------------------

#!/bin/bash

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

d='date -d "-1 day" + %Y%m%d' //类似于20180427,昨天的日期,今天28日

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'

--------------------------------------------------------------------------------------

添加任务计划#crontab -e //写入如下内容

0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh //每天凌晨0点执行


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

Linux学习笔记十二周三次课 (4月25日)

Linux学习笔记十二周三次课 (4月25日)

vim /usr/local/nginx/conf/vhost/test.com.conf  //编辑配置文件,在server里增加下面一行

--------------------------------------------------------------

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

{

            expires        7d;

            access_log   off;

}

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

{

            expires        12h;

            access_log   off;

}

--------------------------------------------------------------

location匹配;\脱意;expires过期时间;access_log off不记录访问日志;

/usr/local/nginx/sbin/nginx -t //检查配置是否有错

/usr/local/nginx/sbin/nginx -s reload //重新加载,也可以重启服务

测试

cd /data/wwwroot/test.com/

ls

vim 1.gif //随便写点东西

vim 2.js //随便写点东西

curl -x127.0.0.1:80 test.com/1.gif

curl -x127.0.0.1:80 test.com/2.js

cat /tmp/test.com.log