码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-14 16:57:15      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:Nginx配置   访问日志   

一、访问日志

1、查看Nginx日志格式

[root@zhulinux-02 ~]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘ $host "$request_uri" $status‘
    ‘ "$http_referer" "$http_user_agent"‘;

也可以使用vim打开nginx配置文件,搜索关键字log_format
变量说明:
技术分享图片
combined_realip为日志格式名字,可以改为其他的,后面可以调用它。

2、虚拟主机指定访问日志路径

[root@zhulinux-02 ~]# cd /usr/local/nginx/conf/vhost/
[root@zhulinux-02 vhost]# ls
linuxtest.conf  moved.conf  zlinux.conf
[root@zhulinux-02 vhost]# vim linuxtest.conf 

server
{
   listen 80;
   server_name linuxtest.com;
   index index.html index.htm index.php;
   root /data/wwwroot/linuxtest;

   location /
     {
       auth_basic         "Auth";
       auth_basic_user_file /usr/local/nginx/conf/htpasswd;
     }
   access_log /tmp/linuxtest.log combined_realip;
}
#使用access_log指定日志存储路径和使用的日志格式名字

3、测试

[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 linuxtest.com/ -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:08:32 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/ -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:08:53 GMT
Content-Type: text/html
Content-Length: 31
Last-Modified: Tue, 13 Mar 2018 07:33:35 GMT
Connection: keep-alive
ETag: "5aa77ecf-1f"
Accept-Ranges: bytes

[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/ -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:09:00 GMT
Content-Type: text/html
Content-Length: 31
Last-Modified: Tue, 13 Mar 2018 07:33:35 GMT
Connection: keep-alive
ETag: "5aa77ecf-1f"
Accept-Ranges: bytes

[root@zhulinux-02 vhost]# ls /tmp/linuxtest.log 
/tmp/linuxtest.log
[root@zhulinux-02 vhost]# cat /tmp/linuxtest.log 
127.0.0.1 - [14/Mar/2018:15:08:32 +0800] linuxtest.com "/" 401 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:08:53 +0800] linuxtest.com "/" 200 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:09:00 +0800] linuxtest.com "/" 200 "-" "curl/7.29.0"

二、访问日志切割

Nginx不自带日志切割工具,要想切割Nginx日志需要借助系统的切割工具或者自定义脚本。这里提供一个Nginx日志切割脚本供参考。

1、编写日志切割脚本

[root@zhulinux-02 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh

#! /bin/bash
# date +%Y%m%d 显示的是今天的日期 
# 加上 -d "-1 day" 显示的是昨天的日期 
d=`date -d "-1 day" +%Y%m%d`

#定义日志存放的路径,假设存放在/tmp/下
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`

2、验证

[root@zhulinux-02 vhost]# sh -x  /usr/local/sbin/nginx_log_rotate.sh   //-x选项是调试选项
++ date -d ‘-1 day‘ +%Y%m%d
+ d=20180313
+ logdir=/tmp
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp
++ ls linuxtest.log
+ for log in ‘`ls *.log`‘
+ mv linuxtest.log linuxtest.log-20180313
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 14119
[root@zhulinux-02 vhost]# ls /tmp/        //生成了带日期的日志
linuxtest.log           mysql.sock  php-fcgi.sock
linuxtest.log-20180313  pear        systemd-private-ee3d7331f2804e42bc9bfe8f2b90a0e8-vmtoolsd.service-3B2v7u

3、增加任务计划

[root@zhulinux-02 vhost]# crontab -e 
no crontab for root - using an empty one     //介入以下内容

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

三、配置静态文件不记录和过期时间

1、修改虚拟主机配置

[root@zhulinux-02 vhost]# vim linuxtest.conf
server
{
   listen 80;
   server_name linuxtest.com;
   index index.html index.htm index.php;
   root /data/wwwroot/linuxtest;

   location /
     {
       auth_basic         "Auth";
       auth_basic_user_file /usr/local/nginx/conf/htpasswd;
     } 
#    ~ 匹配后续的正则表示
#    使用\转义.,匹配.jpg等文件
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
#       expires设置过期时间  
        expires 7d;

#       关闭日志记录   
        access_log off;
    }   

   location ~ .*\.(css|js)$
    {
        expires 12h;
        access_log off;
    }   
   access_log /tmp/linuxtest.log combined_realip;
}    

2、测试

[root@zhulinux-02 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@zhulinux-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@zhulinux-02 vhost]# echo "111111" > /data/wwwroot/linuxtest/test.js
[root@zhulinux-02 vhost]# echo "111111" > /data/wwwroot/linuxtest/test.jpg
[root@zhulinux-02 vhost]# echo "111111" > /data/wwwroot/linuxtest/test.ddd    //创建一个对比文件,后缀名不在配置列表内
[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:1234566 linuxtest.com/test.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:45:47 GMT
Content-Type: application/javascript
Content-Length: 7
Last-Modified: Wed, 14 Mar 2018 07:44:42 GMT
Connection: keep-alive
ETag: "5aa8d2ea-7"
Expires: Wed, 14 Mar 2018 19:45:47 GMT
Cache-Control: max-age=43200    //最大过期时间
Accept-Ranges: bytes

[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/test.jpg -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:45:55 GMT
Content-Type: image/jpeg
Content-Length: 7
Last-Modified: Wed, 14 Mar 2018 07:45:05 GMT
Connection: keep-alive
ETag: "5aa8d301-7"
Expires: Wed, 21 Mar 2018 07:45:55 GMT
Cache-Control: max-age=604800     //最大过期时间
Accept-Ranges: bytes

[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456linuxtest.com/test.ddd -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:46:01 GMT
Content-Type: application/octet-stream
Content-Length: 7
Last-Modified: Wed, 14 Mar 2018 07:45:29 GMT
Connection: keep-alive
ETag: "5aa8d319-7"
Accept-Ranges: bytes

[root@zhulinux-02 vhost]# cat /tmp/linuxtest.log    //未记录指定后缀日志
127.0.0.1 - [14/Mar/2018:15:42:57 +0800] linuxtest.com "/1.gif" 404 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:46:01 +0800] linuxtest.com "/test.ddd" 200 "-" "curl/7.29.0"

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

标签:Nginx配置   访问日志   

原文地址:http://blog.51cto.com/3069201/2086785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!