小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Nginx的訪問日志,Nginx日志切割,Nginx不記錄靜態(tài)文件

 WindySky 2019-02-28

Nginx的訪問日志

Nginx的日志格式是在Nginx的主配置文件中(/usr/local/nginx/conf/nginx.conf)

[root@shuai-01 vhost]# vim /usr/local/nginx/conf/nginx.conf

1

可以將日志格式名稱改一下,改為shaui

Nginx日志字段的含義

在主配置文件中定義日志的格式,在虛擬主機(jī)配置文件中定義日志路徑。

打開虛擬主機(jī)配置文件

[root@shuai-01 vhost]# vim .conf 

access_log /tmp/.log shuai;

1

2

3

注意,Nginx配置文件寫完一行要加“;”,不然就是錯(cuò)誤。

檢查配置文件語法并重新加載配置文件

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

1

2

3

4

檢測(cè):

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.2

Date: Mon, 08 Jan 2018 12:41:20 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http:///index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.2

Date: Mon, 08 Jan 2018 12:41:26 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http:///index.html

[root@shuai-01 vhost]# cat /tmp/.log 

127.0.0.1 - [08/Jan/2018:20:41:20 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0"

127.0.0.1 - [08/Jan/2018:20:41:26 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0"

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Nginx日志切割

nginx由于沒有自帶的日志切割工具,在切割日志時(shí),需要借助于系統(tǒng)帶的日志切割工具,或者是自己寫一個(gè)日志切割腳本。

自己寫一個(gè)日志切割腳本。腳本統(tǒng)一保存/usr/local/sbin/ 

先自定義一個(gè)腳本:

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

#! /bin/bash

## 假設(shè)nginx的日志存放路徑為/tmp/

d=`date -d "-1 day" +%Y%m%d` 

#定義切割時(shí)間(切割一天前的日志)

logdir="/tmp/"

#此處指定要切割的日志路徑(該路徑來自虛擬主機(jī)配置文件)

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

#調(diào)用pid的目的是執(zhí)行命令:/bin/kill -HUP `cat $nginx_pid`

#該命令等價(jià)于命令:nginx -s reload(重新加載文件),確保與虛擬主機(jī)配置文件變更保持同步

#該地址來自nginx配置文件

cd $logdir

for log in `ls *.log`

do

    mv $log $log-$d

done

#此處使用通配進(jìn)行循環(huán),并改名字(切割是每天產(chǎn)生的日志重命名)

/bin/kill -HUP `cat $nginx_pid`

#執(zhí)行此命令進(jìn)行重載生成新的日志文件來記錄新的日志

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

執(zhí)行腳本:

[root@shuai-01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh 

++ date -d '-1 day' +%Y%m%d

+ d=20180108

+ logdir=/tmp/

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

+ cd /tmp/

++ ls .log

+ for log in '`ls *.log`'

+ mv .log .log-20180108

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -HUP 1513

1

2

3

4

5

6

7

8

9

10

11

-x : 作用是顯示腳本執(zhí)行過程 

注意: 

這只是對(duì)日志進(jìn)行了切割,對(duì)日志進(jìn)行刪除需要結(jié)合任務(wù)計(jì)劃cron使用。切割也得配合cron使用。

靜態(tài)文件不記錄日志和過期時(shí)間

在配置文件中加上配置:

打開配置文件:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

    expires         7d;

    access_log off;

}

location ~.*\.(js|css)$

{

    expires         12h;

    acces_log off;

}

[root@shuai-01 vhost]# vim .conf

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

 #匹配文件類型

{

      expires      7d;

      #過期時(shí)間為7天

      access_log off;  

      #不記錄該類型文件的訪問日志     

}   

location ~ .*\.(js|css)$

{

      expires      12h;

      #過期時(shí)間為12小時(shí)

      access_log off;

      #不記錄該類型文件的訪問日志

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

檢查配置文件語法并重新加載配置文件:

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

1

2

3

4

測(cè)試:

[root@shuai-01 ]# curl -x127.0.0.1:80 /1.gif

shjdkjhkasb

[root@shuai-01 ]# curl -x127.0.0.1:80 /2.js

ajkfdchb

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

[root@shuai-01 ]# cat /tmp/.log

127.0.0.1 - [09/Jan/2018:00:39:45 +0800] "/index.html" 200 "-" "curl/7.29.0"

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多