Nginx訪問日誌、日誌切割、靜態文件不記錄日誌和過時時間

11月27日任務php

12.10 Nginx訪問日誌css

12.11 Nginx日誌切割html

12.12 靜態文件不記錄日誌和過時時間mysql

 

12.10 Nginx訪問日誌linux

  • 除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件中增長
  •  access_log /tmp/1.log combined_realip;
  •  這裏的combined_realip就是在nginx.conf中定義的日誌格式名字
  •  -t && -s reload
  •  curl -x127.0.0.1:80 test.com -I
  •  cat /tmp/1.log

[root@zgxlinux-01 vhost]# vim ../nginx.conf         #修改日誌名稱
nginx

[root@zgxlinux-01 vhost]# vim test.com.conf sql

[root@zgxlinux-01 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@zgxlinux-01 vhost]# /usr/local/nginx/sbin/nginx -s reloadshell

[root@zgxlinux-01 vhost]# curl -x127.0.0.1:80 test4.com/admin/index.html/asdkfh -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 03:20:47 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alivevim

[root@zgxlinux-01 vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html/asdkfh -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 03:20:54 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/asdkfhbash

[root@zgxlinux-01 vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html/asdkfh -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 03:21:08 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/asdkfh

[root@zgxlinux-01 vhost]# cat /tmp/test.com.log 
127.0.0.1 - [01/Dec/2018:11:20:54 +0800] test3.com "/admin/index.html/asdkfh" 301 "-" "curl/7.29.0"
127.0.0.1 - [01/Dec/2018:11:21:08 +0800] test2.com "/admin/index.html/asdkfh" 301 "-" "curl/7.29.0"
 

 

12.11 、Nginx日誌切割

  • 自定義shell 腳本
  •  vim /usr/local/sbin/nginx_log_rotate.sh//寫入以下內容

#! /bin/bash
## 假設nginx的日誌存放路徑爲/data/logs/
d=`date -d "-1 day" +%Y%m%d` 
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`

  •  任務計劃
  •  0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

 

#操做過程

[root@zgxlinux-01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh

[root@zgxlinux-01 vhost]# sh -x  /usr/local/sbin/nginx_logrotate.sh       #執行這個腳本,-x表示查看執行過程
++ date -d '-1 day' +%Y%m%d
+ d=20181130
+ 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-20181130
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 4421
[root@zgxlinux-01 vhost]# ls /tmp/
mysql.sock
pear
php-fcgi.sock
systemd-private-bbc1c842e4c14aefa0295c2d1c669add-chronyd.service-yevkTQ
test.com.log
test.com.log-20181130
[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm   #長時間之後須要清理日誌,xargs進行刪除操做。
rm: 缺乏操做數
Try 'rm --help' for more information.

[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f 
/tmp/test.com.log-20181130
[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f |xargs rm -rf
[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f 
[root@zgxlinux-01 vhost]# ls /tmp/
mysql.sock
pear
php-fcgi.sock
systemd-private-bbc1c842e4c14aefa0295c2d1c669add-chronyd.service-yevkTQ
test.com.log
[root@zgxlinux-01 vhost]# ls /usr/local/sbin/
iptables.sh  nginx_logrotate.sh
[root@zgxlinux-01 vhost]# cat !$/nginx_logrotate.sh
cat /usr/local/sbin//nginx_logrotate.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`

 

 

12.12 、靜態文件不記錄日誌和過時時間

  • 配置以下

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

 

#操做過程

[root@zgxlinux-01 vhost]# vim test.com.conf 

[root@zgxlinux-01 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@zgxlinux-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 vhost]# cd /data/wwwroot/test.com/
[root@zgxlinux-01 test.com]# ls
admin  index.html
[root@zgxlinux-01 test.com]# vim 1.gif
[root@zgxlinux-01 test.com]# vim 2.js

[root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif kdhfkadshfkashkdh [root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/2.js akdksfalsdjflajdfns,shvawekhfkl [root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@zgxlinux-01 test.com]# cat /tmp/test.com.log  127.0.0.1 - [01/Dec/2018:12:07:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" [root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/2.jsdsfdss <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.14.0</center> </body> </html>

相關文章
相關標籤/搜索