12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间
生活随笔
收集整理的這篇文章主要介紹了
12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
-?12.10?Nginx訪問日志
-?12.11?Nginx日志切割
-?12.12?靜態(tài)文件不記錄日志和過期時間#?12.10?Nginx訪問日志
-?日志的格式-??vim?/usr/local/nginx/conf/nginx.conf?//搜索log_format
```
[root@localhost?vhost]#?vim?/usr/local/nginx/conf/nginx.conferror_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]#?lsaaa.com.conf??test.com.conf[root@localhost?vhost]#?
```-?$remote_addr |????客戶端IP(公網(wǎng)IP)
-?$http_x_forwarded_for???| 代理服務(wù)器的IP
-?$time_local ?|????服務(wù)器本地時間
-?$host????| 訪問主機名(域名)
-?$request_uri???| 訪問的url地址
-?$status??| 狀態(tài)碼
-?$http_referer??| referer
-?$http_user_agent???| user_agent-?之前拷貝的nginx.conf下就有關(guān)于訪問日志的相關(guān)
打開配置文件```[root@localhost?vhost]#?vim?test.com.confserver
{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沒有自帶日志切割工具
所以只能借助系統(tǒng)自帶的工具或者使用腳本實現(xiàn)
-?創(chuàng)建shell腳本
-?規(guī)則:所有的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`
domv?$log?$log-$d
done
/bin/kill?-HUP?`cat?$nginx_pid`
~????????????????????????????????????????????????????????????????????????????????????????~????????????????????????????????????????????????????????????????????????????????????????3,13?????????全部
:wq
```
-?現(xiàn)在就可以去執(zhí)行下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:?缺少操作數(shù)
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`
domv?$log?$log-$d
done
/bin/kill?-HUP?`cat?$nginx_pid`
[root@localhost?vhost]#?
```
-?寫完腳本之后,還需要加個任務(wù)計劃?crontab?-e
```
[root@localhost?vhost]#?crontab?-e
no?crontab?for?root?-?using?an?empty?one0?0?*?*?8?/bin/bash?/usr/local/sbin/nginx_logrotate.sh
~????????????????????????????????????????????????????????????????????????????????????????
~????????????????????????????????????????????????????????????????????????????????????????:q!
```
-?因為是做測試的所以不保存#?12.12?靜態(tài)文件不記錄日志和過期時間
-?打開配置文件?vi?test.com.conf??
```
[root@localhost?vhost]#?vi?test.com.confserver
{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.confserver
{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]#?
```
-?這就是怎么樣定義過期時間,以及指定某些請求不去記錄日志
轉(zhuǎn)載于:https://blog.51cto.com/ch71smas/1974739
總結(jié)
以上是生活随笔為你收集整理的12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用 :placeholder-show
- 下一篇: 结对编程作业——四则运算GUI程序