Nginx訪問日誌及切割,靜態文件不記錄日誌和設置過時時間

Nginx訪問日誌

在主配置文件中查看當前的日誌格式

搜索log_formatphp

[root@test-a /]# cd /usr/local/nginx/
[root@test-a nginx]# vim conf/nginx.conf

include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';

相關字段的含義
字段 | 含義 --- | --- $remote_addr | 客戶端IP(公網IP)
$http_x_forwarded_for | 代理服務器的IP
$time_local | 服務器本地時間
$host | 訪問主機名(域名)
$request_uri | 訪問的url地址
$status | 狀態碼
$http_refer | referer
$http_user_agent | user_agentcss

給虛擬機定義日誌格式

虛擬主機配置文件對應的服務配置增長 access_log /tmp/abc.com.log combined_realip;(這裏的combined_realip就是在nginx.conf中定義的日誌格式名字);從新加載配置,測試html

[root@test-a nginx]# vim conf/vhost/abc.com.conf
[root@test-a nginx]# cat conf/vhost/abc.com.conf
server
{
    listen 80;
    server_name abc.com ab.com a.com;
    index index.html index.htm index.php;
    root /data/wwwroot/abc.com;

    if ($host != 'abc.com'){
        rewrite ^/(.*)$ http://abc.com/$1 permanent;
    }

    access_log /tmp/abc.com.log combined_realip; # 

}
[root@test-a 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@test-a nginx]# ./sbin/nginx -s reload
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com
This is abc.com site.
[root@test-a nginx]# vim conf/vhost/abc.com.conf
[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:15:06 +0800] abc.com "/" 200 "-" "curl/7.29.0"

Shell腳本切割日誌

Nginx沒有自帶的日誌切割工具,寫一個腳本作到一樣的功能。nginx

[root@test-a nginx]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@test-a nginx]# 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@test-a nginx]# ls /tmp/
abc.com.log  pear  php-fcgi.sock  web-1
[root@test-a nginx]# sh /usr/local/sbin/nginx_log_rotate.sh
[root@test-a nginx]# ls /tmp/
abc.com.log  abc.com.log-20181127  pear  php-fcgi.sock  web-1
[root@test-a nginx]# date
Wed Nov 28 07:37:54 CST 2018

爲腳本設置定時任務,天天零點執行改腳本web

[root@test-a nginx]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

靜態文件不記錄日誌及設置過時時間

[root@test-a nginx]# vim conf/vhost/abc.com.conf
[root@test-a nginx]# cat conf/vhost/abc.com.conf
server
{
    listen 80;
    server_name abc.com ab.com a.com;
    index index.html index.htm index.php;
    root /data/wwwroot/abc.com;

    if ($host != 'abc.com'){
        rewrite ^/(.*)$ http://abc.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/abc.com.log combined_realip;

}
[root@test-a 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@test-a nginx]# ./sbin/nginx -s reload
[root@test-a nginx]# cat /tmp/abc.com.log
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Tue, 27 Nov 2018 23:59:05 GMT
Content-Type: text/html
Content-Length: 22
Last-Modified: Mon, 26 Nov 2018 21:36:51 GMT
Connection: keep-alive
ETag: "5bfc6773-16"
Accept-Ranges: bytes

[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Tue, 27 Nov 2018 23:59:21 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.js -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 00:00:46 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"

[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I  # 添加對應的圖片資源後,能夠看到Cache-Control過時時間  
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 00:03:09 GMT
Content-Type: image/jpeg
Content-Length: 4
Last-Modified: Wed, 28 Nov 2018 00:02:59 GMT
Connection: keep-alive
ETag: "5bfddb33-4"
Expires: Wed, 05 Dec 2018 00:03:09 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
相關文章
相關標籤/搜索