Linux基礎(day50)

12.10 Nginx訪問日誌

Nginx訪問日誌目錄概要

  • 日誌格式
  • vim /usr/local/nginx/conf/nginx.conf //搜索log_format
$remote_addr 客戶端IP(公網IP)
$http_x_forwarded_for 代理服務器的IP
$time_local 服務器本地時間
$host 訪問主機名(域名)
$request_uri 訪問的url地址
$status 狀態碼
$http_referer referer
$http_user_agent user_agent
  • 除了在主配置文件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

Nginx訪問日誌

  • 日誌的文件也是在主配置文件中
  1. 打開主配置文件vim /usr/local/nginx/conf/nginx.conf
[root@hanfeng vhost]# vim /usr/local/nginx/conf/nginx.conf

搜索/log_format 找到如下內容,就是來定義日誌格式的
 log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
  • combined_realip 日誌格式的名字,能夠隨便定義,這裏定義成什麼名字,後面就引用成什麼名字,決定了虛擬主機引用日誌的類型
  • 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(標識)
  • 若想本身的公網IP,能夠直接百度IP,就會出來本身上網的IP地址
  1. 除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件去定義access_log /tmp/1.log combined_realip; 來定義訪問日誌路徑
  • vim /usr/local/nginx/conf/vhost/test.com.conf
[root@hanfeng vhost]# vim test.com.conf

在第一個括號中添加access_log /tmp/1.log combined_realip;便可

server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
     if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    access_log /tmp/test.com.log combined_realip;
}
保存退出
  • 若是不寫日誌格式,那就會走默認的日誌格式
  1. 而後檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@hanfeng 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@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
  1. 測試
[root@hanfeng vhost]# curl -x127.0.0.1:80 test1.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:18 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@hanfeng vhost]# curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:25 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@hanfeng vhost]#
  1. 查看日誌cat /tmp/test.com.log
[root@hanfeng vhost]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:15:18 +0800] test1.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:22:15:25 +0800] test2.com "/" 301 "-" "curl/7.29.0"
[root@hanfeng vhost]#

12.11 Nginx日誌切割

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

Nginx日誌切割

  • Nginx沒有自帶日誌切割工具,只能藉助系統的日誌切割的工具或者本身寫切割的腳本實現
  1. 這裏寫一個日誌切割腳本
  2. 首先建立一個shell腳本vim /usr/local/sbin/nginx_log_rotate.sh
  • 全部的shell腳本放入到/usr/local/sbin/目錄下
[root@hanfeng vhost]# vim /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`
  • d=date -d 「-1 day」 +%Y%m%d // 生成昨天的日期,格式爲年月日
  • logdir=」tmp/」 // 上一節的時候,定義了日誌存放在/tmp/目錄下
  • nginx_pid=」/usr/local/nginx/logs/nginx.pid」 //查找nginx的PID,目的是爲了執行/bin/kill -HUP cat $nginx_pid ,而這個命令目的和nginx -s relo 是同樣的
  • cd $logdir //進入「logdir」日誌目錄下
  • for log in ls *.log //開始語句循環,看錯有哪些 log後綴的文件
  • do //執行
  • mv $log $log-$d                                            //將 log更名爲《原名字-「`date -d 「-1 day」 +%Y%m%d` 」這個結尾的文件 》
  • done //結束
  • /bin/kill -HUP cat $nginx_pid // 從新加載,生成一個新的「nginx_pid=」/usr/local/nginx/logs/nginx.pid」
[root@hanfeng vhost]# ls
aaa.com.conf  test.com.conf
[root@hanfeng vhost]# for f in `ls `; do ls -l $f ; done
-rw-r--r--. 1 root root 140 1月   3 23:17 aaa.com.conf
-rw-r--r--. 1 root root 295 1月   4 22:07 test.com.conf
[root@hanfeng vhost]#
  1. 執行shell腳本,並加-x權限
  • -x選項,是爲了查看腳本執行的過程
[root@hanfeng vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180103
+ 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-20180103
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1612
[root@hanfeng vhost]#
  1. 查看日誌切割文件,天天都生成一個日誌,在天天切割後,過段時間還要按期清理
[root@hanfeng vhost]# ls /tmp/
mysql.sock  pear  php-fcgi.sock  test.com.log  test.com.log-20180103
[root@hanfeng vhost]#
  1. 刪除30天之前的日誌文件
[root@hanfeng vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
  1. 寫完腳本後,還要加一個任務計劃crontab -e——>這裏由於是測試,腳本就不加入到任務計劃中了
[root@hanfeng vhost]# crontab -e

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

shell腳本知識點

  • 知識點:
  1. 日誌時間切割的定義
  • 寫shell腳本的時候,若是有命令不明白,能夠直接把命令運行一下就知道結果了
  • 假設這個命令「 d=date -d 「-1 day」 +%Y%m%d 」不明白意思
  • ctrl+z 把當前操做暫停丟到後臺
[root@hanfneg ~]# date -d 「-1 day」 +%Y%m%d
20180103
[root@hanfeng vhost]# date
2018年 01月 04日 星期四 23:27:23 CST
  • 就是時間,並且是昨天的時間,由於目前作的日誌切割都是以天爲單位,並且,日誌須要過了當天23點59分59秒之後到次日的0點0分01秒才切割
  1. 指定PID路徑的意義
  • 「 nginx_pid=」/usr/local/nginx/logs/nginx.pid」 」這條命令的意思,就是指定nginx的PID 的路徑所在
  • 若是找不到指定PID的所在,那麼下面的「 /bin/kill -HUP cat $nginx_pid 」這個命令也將沒有辦法繼續執行
  • 「 /bin/kill -HUP cat $nginx_pid 」 z這條命令的意思就是從新加載一次nginx服務
  • 執行「 /bin/kill -HUP cat $nginx_pid 」這條命令的目的是由於切割日誌之後 「mv $log $log-$d 」 會將日誌移動位置,若是不使用這條命令從新加載一次nginx服務、從新生成一第二天志文件,那麼將會致使服務出錯
  • 因此,爲了保證「 /bin/kill -HUP cat $nginx_pid 」能準確的執行,須要肯定nginx的PID所在
[root@hanfeng ~]# ls /usr/local/nginx/logs/
access.log  error.log  nginx_error.log  nginx.pid
  1. 循環語句理解
  • for f in ‘ls ‘ ; do ls -l $f; done
    • for 循環開始,f 表示文件,in 表示作什麼,‘ls’in執行的東西; do 執行 ls -f $f;done 結束
  • 任務計劃
    • 腳本寫完之後,須要寫一個計劃,讓腳本在規定的時間運行。
    • crontab -e
      • 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
  • 長時間累積,會生成大量的日誌須要進行清理
    • fidn /tmp/ -type f -name .log- -mtime +30 |xargs rm

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

靜態文件不記錄日誌和過時時間目錄概要

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

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

  • 在配置文件中添加
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    //匹配gif|jpg|jpeg|png|bmp|swf 後綴的文件
    {
          expires      7d;        //7天后過時
          access_log off;        //匹配「.*.(gif|jpg|jpeg|png|bmp|swf) 」關閉記錄日誌
    }
location ~ .*\.(js|css)$
    {
          expires      12h;        //12個小時後過時
          access_log off;        //匹配「.*.(js|css) 」關閉記錄日誌
    }
  1. 打開虛擬主機配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
[root@hanfeng vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf

server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
     if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }     
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }     
    access_log /tmp/test.com.log combined_realip;
}   
保存退出
  1. 檢查配置文件語法錯誤,並從新加載配置文件
[root@hanfeng 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@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
  1. 測試,先來模擬一個圖片
[root@hanfeng vhost]# cd /data/wwwroot/test.com/
[root@hanfeng test.com]# ls
admin  index.html
[root@hanfeng test.com]# vim 1.gif        在1.gif隨意寫入一些內容
[root@hanfeng test.com]# vim 2.js
[root@hanfeng test.com]#
  1. 接下來作一個訪問測試
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/1.gif
sdafasf
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/2.js
fghdfsd
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/index.html
「test.com」
[root@hanfeng test.com]#
  1. 查看日誌,會看到只有一條日誌
[root@hanfeng test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jan/2018:00:17:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@hanfeng test.com]#
  1. 測試過時時間,加上-I參數
[root@hanfeng test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 16:22:07 GMT
Content-Type: application/javascript
Content-Length: 8
Last-Modified: Thu, 04 Jan 2018 16:15:42 GMT
Connection: keep-alive
ETag: "5a4e532e-8"
Expires: Fri, 05 Jan 2018 04:22:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@hanfeng test.com]#
  • max-age=43200 過時時間
  1. 若是去掉expires,則不會顯示max-age過時時間
相關文章
相關標籤/搜索