1. 程式人生 > >LNMP-Nginx訪問日誌

LNMP-Nginx訪問日誌

log nginx

訪問日誌

1、定義日誌格式

 [[email protected] ~]# vi /usr/local/nginx/conf/nginx.conf
    log_format log001 ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘ $host "$request_uri" $status‘
    ‘ "$http_referer" "$http_user_agent"‘;

▎Nginx日誌格式:

$remote_addr

客戶端IP(公網IP)

$http_x_forwarded_for

代理服務器的IP

$time_local

服務器本地時間

$host

訪問主機名(域名)

$request_uri

訪問的url地址

$status

狀態碼

$http_referer

referer

$http_user_agent

user_agent

2、增加訪問日誌項

[[email protected] ~]# vi /usr/local/nginx/conf/vhost/default.conf
server
{
    listen 80 default_server;  
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
    access_log /tmp/default.log log001;##增加該行,該行元素:訪問日誌,存儲地址,格式名稱
}

3、檢查與重載

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

4、測試效果

[[email protected]
*/ ~]# curl -x127.0.0.1:80 aaa.com/index.html “This is a default site.” [[email protected] ~]# curl -x127.0.0.1:80 aaa1.com/index.html “This is a default site.” [[email protected] ~]# cat /tmp/default.log 127.0.0.1 - [12/Aug/2017:10:34:45 +0800] aaa.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [12/Aug/2017:10:34:52 +0800] aaa1.com "/index.html" 200 "-" "curl/7.29.0"


日誌切割

1、日誌切割腳本

[[email protected] ~]# vi /usr/local/sbin/nginx_log_rotate.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`

▎KILL參數:

  • INT:快速關閉,是當用戶鍵入<Control-C>時由終端驅動程序發送的信號。

  • TERM:快速關閉,請求徹底終止某項執行操作,它期望接收進程清除自給的狀態並退出。

  • HUP:平滑啟動,重新加載配置文件。

  • QUIT:從容關閉。


2、執行腳本

[[email protected] tmp]# sh -x /usr/local/sbin/nginx_log_rotate.sh 
++ date -d ‘-1 day‘ +%Y%m%d
+ d=20170811
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls default.log
+ for log in ‘`ls *.log`‘
+ mv default.log default.log-20170811
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 89689

3、檢查效果

[[email protected] tmp]# ls
default.log  default.log-20170811  mysql.sock  pear  php-fcgi.sock

4、定期任務計劃

[[email protected] tmp]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

5、清理過期日誌

[[email protected] tmp]# find /tmp/ -name *.log-* -type f -mtime +10 |xagrs rm 
##也可以放在腳本裏,定期清除


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

1、編輯配置文件

[[email protected] default]# vi /usr/local/nginx/conf/vhost/default.conf

server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
    access_log /tmp/default.log log001;
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
    location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
}

2、檢查與重載

[[email protected] default]# /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
[[email protected] default]# /usr/local/nginx/sbin/nginx -s reload

3、檢查效果

[[email protected] default]# ls
index.html  pic001.gif
[[email protected] default]# curl -x127.0.0.1:80 aaa.com/pic001.gif -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sat, 12 Aug 2017 03:31:08 GMT
Content-Type: image/gif
Content-Length: 66698
Last-Modified: Sat, 12 Aug 2017 03:29:18 GMT
Connection: keep-alive
ETag: "598e760e-1048a"
Expires: Sat, 19 Aug 2017 03:31:08 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes

[[email protected] tmp]# curl -x127.0.0.1:80 aaa.com/pic001 -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Sat, 12 Aug 2017 03:34:43 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[[email protected] tmp]# cat /tmp/default.log
127.0.0.1 - [12/Aug/2017:11:34:43 +0800] aaa.com "/pic001" 404 "-" "curl/7.29.0"


本文出自 “Gorilla City” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1955696

LNMP-Nginx訪問日誌