您的位置:首页 > 运维架构 > Nginx

12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间

2017-10-20 20:56 1221 查看
- 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]#
```
- 这就是怎么样定义过期时间,以及指定某些请求不去记录日志
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐