11-5 12 Nginx访问日志 日志切割 静态过时

12.10 Nginx访问日志css

12.11 Nginx日志切割html

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

12.10 Nginx访问日志

  • 与apache相似,主配置文件中,有定义日志格式

日志格式shell

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

  • combined_realip 格式名字,用于调用日志格式,可改
  • $remote_addr 客户端IP(公网IP)
  • $http_x_forwarded_for 代理服务器的IP
  • $time_local 服务器本地时间
  • $host 访问主机名(域名)
  • $request_uri 访问的url地址
  • $status 状态码
  • $http_referer 跳转来源URL
  • $http_user_agent 访问工具(浏览器参数)

虚拟主机启用日志apache

access_log /tmp/ccc.log combined_realip;

  • 这里的combined_realip就是在nginx.conf中定义的日志格式类型名称

-t && -s reload curl -x127.0.0.1:80 ccc.com -I cat /tmp/ccc.log //vim

12.11 Nginx日期切割

  • 没有apache的切割命令,须要自定义shell 切割脚本
vim /usr/local/sbin/nginx_log_rotate.sh//写入以下内容
···
#! /bin/bash
# 定义变量d为昨天日期
d=`date -d "-1 day" +%Y%m%d`
# 定义变量logdir为日志路径,若是不知道能够看虚拟主机配置
logdir="/tmp/"
#定义nginx_pid为日志进程文件
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
# i in commed 将i循环赋值为命令的每一个输出,进行do的操做
for i in `ls *.log`
do
    mv $i $i-$d
done
# 文件更名后须要中断日志pid,不然新日志没法生成
/bin/kill -HUP `cat $nginx_pid`

手动执行(-x显示执行过程)浏览器

[root@axiang-02 ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh

任务计划(天天0点执行)bash

crontab -e
...
 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

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

核心配置服务器

location ~ .*(gif|jpg|jpeg|png|bmp|swf)$  //正则精准匹配
{
      expires      7d;   //过时时间
      access_log off;  //不记录日志
}
location ~ .*\.(js|css)$
{
      expires      12h;
      access_log off;
}
  • 加载access前面便可

测试curl

[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif
asfoawnfnasxojfan
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/2.js
soawejifna
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html
This is ccc.com
[root@axiang-02 ccc.com]# cat /tmp/ccc.log  //没重启nginx会继续生成静态文件日志
127.0.0.1 - [06/Aug/2017:09:41:47 +0800] ccc.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:41:57 +0800] ccc.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:42:16 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0"
[root@axiang-02 ccc.com]# /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@axiang-02 ccc.com]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html
This is ccc.com
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/2.js
soawejifna
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif
asfoawnfnasxojfan
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/index.html
This is ccc.com
[root@axiang-02 ccc.com]# !cat
cat /tmp/ccc.log
127.0.0.1 - [06/Aug/2017:09:41:47 +0800] ccc.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:41:57 +0800] ccc.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:42:16 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Aug/2017:09:42:57 +0800] ccc.com "/index.html" 200 "-" "curl/7.29.0"
[root@axiang-02 ccc.com]# curl -x127.0.0.1:80 ccc.com/1.gif -I
HTTP/1.1 200 OK
...
Cache-Control: max-age=604800   //失效为7天
相关文章
相关标签/搜索