Nginx訪問日誌、Nginx日誌切割、靜態檔案不記錄日誌和過期時間

技術小甜發表於2017-11-07

Nginx訪問日誌

    log_format combined_realip `$remote_addr $http_x_forwarded_for [$time_local]`

    ` $host “$request_uri” $status`

    ` “$http_referer” “$http_user_agent”`;

combined_realip:日誌名稱

其餘為日誌內容

$remote_addr 客戶端IP(公網IP)

$http_x_forwarded_for 代理伺服器的IP

$time_local 伺服器本地時間

$host 訪問主機名(域名)

$request_uri 訪問的URL地址

$status 狀態碼

$http_referer referer

$http_user_agent user_agent

[root@centos7 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf


server

{

    listen 80;

    server_name test.com  test2.com test3.com;

    index index.html index.htm index.php;

    access_log /tmp/test.com.log combined_realip;


驗證:

[root@centos7 ~]# tail /tmp/test.com.log 

127.0.0.1 – [10/Nov/2017:17:48:20 +0800] test.com “/” 401 “-” “curl/7.29.0”

127.0.0.1 – [10/Nov/2017:17:48:30 +0800] test.com “/index.php” 200 “-” “curl/7.29.0”


Nginx日誌切割

日誌切割指令碼

[root@centos7 shell]# vi nginx_log_rotate.sh


#! /bin/bash

d=`date -d “-1 day” +%Y%m%d`

#定義切割時間(切割一天前的日誌)

logdir=”/tmp/”

#此處指定要切割的日誌路徑(該路徑來自虛擬主機配置檔案)

nginx_pid=”/usr/local/nginx/logs/nginx.pid”

#呼叫pid的目的是執行命令:/bin/kill -HUP `cat $nginx_pid`

#該命令等價於命令:nginx -s reload(重新載入檔案)

cd $logdir

for log in `ls *.log`

do

    mv $log $log-$d

done

/bin/kill -HUP `cat $nginx_pid`

#執行此命令進行過載生成新的日誌檔案,與kill -usr1 一樣


靜態檔案不記錄日誌和過期時間

驗證訪圖片的時候:

[root@centos7 test.com]# curl -x127.0.0.1:80 test.com/baidu.png -I

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Fri, 10 Nov 2017 10:01:03 GMT

Content-Type: image/png


[root@centos7 test.com]# curl -x127.0.0.1:80 test.com/index.php

沒記錄日誌

[root@centos7 test.com]# tail /tmp/test.com.log 

127.0.0.1 – [10/Nov/2017:17:48:20 +0800] test.com “/” 401 “-” “curl/7.29.0”

127.0.0.1 – [10/Nov/2017:17:48:30 +0800] test.com “/index.php” 200 “-” “curl/7.29.0”

127.0.0.1 – [10/Nov/2017:18:02:38 +0800] test.com “/index.php” 200 “-” “curl/7.29.0”

127.0.0.1 – [10/Nov/2017:18:02:38 +0800] test.com “/index.php” 200 “-” “curl/7.29.0”


問題:curl訪問時間不對,但是系統時間是對的?

[root@centos7 test.com]#  curl -x127.0.0.1:80 test.com/baidu.png -I

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Wed, 15 Nov 2017 02:58:15 GMT

Content-Type: image/png

[root@centos7 test.com]# date

Wed Nov 15 10:58:32 CST 2017




`

本文轉自方向對了,就不怕路遠了!51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1981934 ,如需轉載請自行聯絡原作者


相關文章