12.10 Nginx訪問日誌 12.11 Nginx日誌切割 12.12 靜態文件不記錄日誌和過期時間

- 12.10 Nginx訪問日誌
- 12.11 Nginx日誌切割
- 12.12 靜態文件不記錄日誌和過期時間

# 12.10 Nginx訪問日誌
- 日誌的格式

-  vim /usr/local/nginx/conf/nginx.conf //搜索log_format
```
[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf

error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format aming  '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
                                                                       17,21         10%
 [root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf
 [root@localhost vhost]# ls
 aaa.com.conf  test.com.conf
 [root@localhost vhost]# 
```


- $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下就有關於訪問日誌的相關
打開配置文件

```


[root@localhost vhost]# vim 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;
    }
    access_log /tmp/test.com.log aming;

}
~                                                                                                                                                                             
:wq
[root@localhost vhost]# vim test.com.conf
```
- 然後檢查配置 重新加載,測試
```
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# !curl
curl -x127.0.0.1:80 test4.com/admin/index.html/dkdkdkdk  -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 13:11:35 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@localhost vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html/dkdkdkdk  -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 13:11:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/dkdkdkdk

[root@localhost vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html/dkdkdkdk  -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 13:12:04 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/dkdkdkdk
```
- 查看日誌文件
```

[root@localhost vhost]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:21:11:52 +0800] test3.com "/admin/index.html/dkdkdkdk" 301 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:21:12:04 +0800] test2.com "/admin/index.html/dkdkdkdk" 301 "-" "curl/7.29.0"
[root@localhost vhost]# 

```











# 12.11 Nginx日誌切割
- 因爲nginx沒有自帶日誌切割工具
所以只能藉助系統自帶的工具或者使用腳本實現
- 創建shell腳本
- 規則:所有的shell的腳本,以後全部存放在/usr/local/sbin/ 目錄下
```
[root@localhost vhost]# vim /usr/local/sbin/nginx_logrotate.sh

[1]+  已停止               vim /usr/local/sbin/nginx_logrotate.sh
[root@localhost vhost]# date -d "-1 day" 
2017年 10月 18日 星期三 21:22:44 CST
[root@localhost vhost]# date -d "-1 day" +%Y%m%d
20171018
[root@localhost vhost]# date
2017年 10月 19日 星期四 21:23:14 CST
[root@localhost vhost]# fg
vim /usr/local/sbin/nginx_logrotate.sh

[1]+  已停止               vim /usr/local/sbin/nginx_logrotate.sh
[root@localhost vhost]# ls /usr/local/nginx/logs/nginx.pid
/usr/local/nginx/logs/nginx.pid
[root@localhost vhost]# fg
vim /usr/local/sbin/nginx_logrotate.sh

[1]+  已停止               vim /usr/local/sbin/nginx_logrotate.sh
[root@localhost vhost]# ls
aaa.com.conf  test.com.conf
[root@localhost vhost]# for f in `ls `;do ls -l $f;done
-rw-r--r--. 1 root root 142 10月 17 21:20 aaa.com.conf
-rw-r--r--. 1 root root 287 10月 19 21:11 test.com.conf
[root@localhost vhost]# fg
vim /usr/local/sbin/nginx_logrotate.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`
~                                                                                        

~                                                                                        
                                                                       3,13         全部
:wq
```
- 現在就可以去執行下shell  用sh -x 查看過程
```
[root@localhost vhost]# sh -x  /usr/local/sbin/nginx_logrotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20171018
+ 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-20171018
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20171018
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 965
[root@localhost vhost]# ls /tmp/
mysql.sock
pear
php_errors.log-20171018
php-fcgi.sock
systemd-private-c7ea2fb090494a1192f9d0cfbca1de2c-vmtoolsd.service-c7k0sX
test.com.log
test.com.log-20171018
[root@localhost vhost]# 
```
- 長此以往每天會日誌切割,過段時間要要做 定時清理日誌  (因爲沒有符合的條件)
```
[root@localhost vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm
rm: 缺少操作數
Try 'rm --help' for more information.
[root@localhost vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm ^C
[root@localhost vhost]# find /tmp/ -name *.log-* -type f 
/tmp/php_errors.log-20171018
/tmp/test.com.log-20171018
[root@localhost vhost]# 
```
- 日誌腳本
```
[root@localhost vhost]# cat /usr/local/sbin/nginx_logrotate.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@localhost vhost]# 
```
- 寫完腳本之後,還需要加個任務計劃 crontab -e
```
[root@localhost vhost]# crontab -e
no crontab for root - using an empty one

0 0 * * 8 /bin/bash /usr/local/sbin/nginx_logrotate.sh
~                                                                                        
~                                                                                        
                                                                                   
:q!
```
- 因爲是做測試的所以不保存








# 12.12 靜態文件不記錄日誌和過期時間
- 打開配置文件 vi test.com.conf  
```
[root@localhost vhost]# vi 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;
    }
    access_log /tmp/test.com.log aming;

}
~                                                                                     
~                                                                                        
"test.com.conf" 12L, 287C
```
- 加入以下配置
```
[root@localhost vhost]# vi 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 aming;

}
~                                                                                     
~                                                                                        
-- INSERT --

[root@localhost vhost]# vi test.com.conf
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# 
```
- 先來模擬一個圖片
```
[root@localhost vhost]# cd /data/wwwroot//test.com/
[root@localhost test.com]# ls
admin  index.html
[root@localhost test.com]# vim 1.gif
[root@localhost test.com]# vim 2.js
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.gif
dkdldldkdkdkd
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js
kdkdkdkdk
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@localhost test.com]# 
```
- 再來查看日誌
```
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@localhost test.com]# 
```
- 再來crul 一下,,又生成新的一條
```
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:03:58 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@localhost test.com]# 
```

- 再來curl -x127.0.0.1:80 test.com/2.js
```
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js
kdkdkdkdk
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:03:58 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@localhost test.com]# 
[root@localhost test.com]# 
[root@localhost test.com]# 
[root@localhost test.com]# 
[root@localhost test.com]# 
```
- 這個說明訪問js的時候 不會記錄日誌,
- 再來在js後面加點東西 curl -x127.0.0.1:80 test.com/2.jslasdflk 這樣就會記錄
- 因爲這個/2.jslasdflk 不匹配剛剛的語句,所以會記錄
```
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.jslasdflk
<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@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:03:58 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:06:06 +0800] test.com "/2.jslasdflk" 404 "-" "curl/7.29.0"
[root@localhost test.com]# 
```
- 下面來測試下它的過期時間 ,max-age=43200
```
[root@localhost 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, 19 Oct 2017 14:07:53 GMT
Content-Type: application/javascript
Content-Length: 10
Last-Modified: Thu, 19 Oct 2017 14:02:16 GMT
Connection: keep-alive
ETag: "59e8b068-a"
Expires: Fri, 20 Oct 2017 02:07:53 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@localhost test.com]# 
```


-
```

location ~ .*\.(js|css)$
    {
#          expires      12h;
          access_log off;
    }

    access_log /tmp/test.com.log aming;

}
~                                                                                        
                                                                                       
-- INSERT --

[root@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost test.com]# !curl
curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 14:12:15 GMT
Content-Type: application/javascript
Content-Length: 10
Last-Modified: Thu, 19 Oct 2017 14:02:16 GMT
Connection: keep-alive
ETag: "59e8b068-a"
Accept-Ranges: bytes

[root@localhost test.com]# 
```
- 這就是怎麼樣定義過期時間,以及指定某些請求不去記錄日誌


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章