十二週三次課

十二週三次課javascript

12.10Nginx訪問日誌php

12.11Nginx日誌切割css

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

12.10Nginx訪問日誌java

•日誌格式mysql

• vim /usr/local/nginx/conf/nginx.conf //搜索log_formatnginx

$remote_addrsql

客戶端IP(公網IP)shell

$http_x_forwarded_forvim

代理服務器的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.打開主配置文件/usr/local/nginx/conf/nginx.conf

[root@tianqi-01 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,就會出來本身上網的IP地址

2.除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件去定義access_log /tmp/1.log combined_realip;來定義訪問日誌路徑.

  • vim /usr/local/nginx/conf/vhost/test.com.conf

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

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

server
{
    listen 80;
    server_name test.com test2.com test3.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;
}

保存退出

  • 若是不寫日誌格式,那就會走默認的日誌格式

3.而後檢查配置文件是否存在語法錯誤,並從新加載配置文件

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

4.測試

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test4.com/admin.index.html/adafsbs -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:17:23 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test3.com/admin.index.html/adafsbs -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:17:53 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin.index.html/adafsbs

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/admin.index.html/adafsbs -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:18:07 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin.index.html/adafsbs

5.查看日誌/tmp/test.com.log

[root@tianqi-01 vhost]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:20:17:53 +0800] test3.com "/admin.index.html/adafsbs" 301 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:20:18:07 +0800] test2.com "/admin.index.html/adafsbs" 301 "-" "curl/7.29.0"
[root@tianqi-01 vhost]# 

12.11Nginx日誌切割

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@tianqi-01 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 reload 是同樣的
  • 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@tianqi-01 vhost]# ls
aaa.com.conf  test.com.conf
[root@tianqi-01 vhost]# for f in `ls`; do ls -l $f ; done
-rw-r--r-- 1 root root 142 Mar 12 19:38 aaa.com.conf
-rw-r--r-- 1 root root 290 Mar 13 20:15 test.com.conf
[root@tianqi-01 vhost]# 

3.執行shell腳本,並加-x

  • -x選項,是爲了查看腳本執行的過程

[root@tianqi-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180312
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls php_errors.log test.com.log
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20180312
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180312
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 803
[root@tianqi-01 vhost]# 

4.查看日誌切割文件,天天都生成一個日誌,在天天切割後,過段時間還要按期清理

[root@tianqi-01 vhost]# ls /tmp/
mysql.sock
pear
php_errors.log-20180312
php-fcgi.sock
systemd-private-0e2b4ec78d5546abb6c87fc75918c612-vgauthd.service-Tmhc6I
systemd-private-0e2b4ec78d5546abb6c87fc75918c612-vmtoolsd.service-bKnQdM
systemd-private-10939be3906d4885a694c9ccaa969513-vgauthd.service-up8a1e
systemd-private-10939be3906d4885a694c9ccaa969513-vmtoolsd.service-qrMrpr
systemd-private-1e99790229834f839348180fcbb3aa93-vgauthd.service-v7jGAu
systemd-private-1e99790229834f839348180fcbb3aa93-vmtoolsd.service-pRTqgc
systemd-private-2d7e7760b6cd416895880c21a3911a53-vgauthd.service-S1F6Rz
systemd-private-2d7e7760b6cd416895880c21a3911a53-vmtoolsd.service-muJvMP
systemd-private-491ebf486ca940d885ed9348c17675f5-vgauthd.service-3sBthy
systemd-private-491ebf486ca940d885ed9348c17675f5-vmtoolsd.service-sJ80kS
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vgauthd.service-DBvBHm
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vmtoolsd.service-AjXoy6
systemd-private-7ad9977beb634b3280df7fd64e4b16d6-vgauthd.service-qkAtId
systemd-private-7ad9977beb634b3280df7fd64e4b16d6-vmtoolsd.service-z35BeI
systemd-private-7d038e320b614122b872e50458348353-vgauthd.service-Ka4suT
systemd-private-7d038e320b614122b872e50458348353-vmtoolsd.service-radGF1
systemd-private-889a29715b47432482f67cbf18e2c4ac-vgauthd.service-hGaIS9
systemd-private-889a29715b47432482f67cbf18e2c4ac-vmtoolsd.service-gDfN0p
systemd-private-aa279ed589d542bcbc4f2903687ce469-vgauthd.service-xDYbZY
systemd-private-aa279ed589d542bcbc4f2903687ce469-vmtoolsd.service-tTo26l
systemd-private-f4d35a41f34c4234a6934c03885429ba-vgauthd.service-owt3oB
systemd-private-f4d35a41f34c4234a6934c03885429ba-vmtoolsd.service-eJFX1G
test.com.log
test.com.log-20180312
[root@tianqi-01 vhost]# 

5.刪除30天之前的日誌文件

[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
rm: missing operand
Try 'rm --help' for more information.
[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30
[root@tianqi-01 vhost]# 

//不會刪除任何的文件,由於根本沒與符合要求的文件

[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f
/tmp/php_errors.log-20180312
/tmp/test.com.log-20180312
[root@tianqi-01 vhost]# ls /usr/local/sbin/
iptables.sh  nginx_log_rotate.sh
[root@tianqi-01 vhost]# cat /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`
[root@tianqi-01 vhost]# 

6.完腳本後,還要加一個任務計劃crontab -e——>這裏由於是測試,腳本就不加入到任務計劃中了

[root@tianqi-01 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@tianqi-01 vhost]# date -d "-1 day" +%Y%m%d
20180312
[root@tianqi-01 vhost]# date
Tue Mar 13 21:16:08 CST 2018
[root@tianqi-01 vhost]# 

  • 就是時間,並且是昨天的時間,由於目前作的日誌切割都是以天爲單位,並且,日誌須要過了當天23點59分59秒之後到次日的0點0分01秒才切割

2.指定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@tianqi-01 vhost]# ls /usr/local/nginx/logs/
access.log  error.log  nginx_error.log  nginx.pid
[root@tianqi-01 vhost]# 

3.循環語句理解

  • 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.打開虛擬主機配置文件

[root@tianqi-01 vhost]# 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;
    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;
}

保存退出

2.檢查配置文件語法錯誤,並從新加載配置文件

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

3.測試,先來模擬一個圖片

[root@tianqi-01 vhost]# cd /data/wwwroot/test.com/
[root@tianqi-01 test.com]# ls

admin  index.html
[root@tianqi-01 test.com]# vim 1.gif    //在裏面隨便寫點東西

asdbasdgb

保存退出

[root@tianqi-01 test.com]# vim 2.js    //在裏面隨便寫點東西

sasgashv

保存退出

4.接下來作訪問測試

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
asdbasdgb
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sasgashv

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com

4.查看日誌,會看到只有一條日誌

[root@tianqi-01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log

127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sasgashv
[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log

127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# 

//這說明訪問js的時候不會記錄日誌

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.jsagasg
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# 

5.測試過時時間,加上-I參數

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 13:38:09 GMT
Content-Type: application/javascript
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 13:31:38 GMT
Connection: keep-alive
ETag: "5aa7d2ba-9"
Expires: Wed, 14 Mar 2018 01:38:09 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@tianqi-01 test.com]# 

  • max-age=43200 過時時間

6.若是去掉expires,則不會顯示max-age過時時間

[root@tianqi-01 test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf 

#          expires      12h;

//註釋掉上面這一句話

7.查看配置文件是否存在語法錯誤,並從新加載配置文件

[root@tianqi-01 test.com]# /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@tianqi-01 test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 test.com]# 

8.從新進行curl訪問,則max-age=43200不會出現

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 13:41:49 GMT
Content-Type: application/javascript
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 13:31:38 GMT
Connection: keep-alive
ETag: "5aa7d2ba-9"
Accept-Ranges: bytes

[root@tianqi-01 test.com]# 

友情連接:阿銘Linux

相關文章
相關標籤/搜索