十二周三次课 (3月14日)

时间:2022-07-29 13:48:30

Nginx访问日志

  • 在ngInx的主配置文件中出现的log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' 其中combined_realip为日志格式的名字,也可以自定义,后面也要写成自定义的名字。
log_format lufei  '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';

$remote_addr ——客户端IP(公网IP)
查看出口IP
十二周三次课 (3月14日)

$http_x_forwarded_for ——代理服务器IP(不配置这段,查看不到代理IP地址)

$time_local——服务器本地时间

$host——访问主机名(域名)

$request_uri——访问的URL地址

$status——状态码

$http_referer——referer

$http_user_agent——user_agent

  • 定义访问日志
    1.除了在主配置文件里定义日志格式外,还需要在虚拟主机配置文件中增加。例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 ~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
access_log /tmp/test.com.log lufei; //定义日志路径以及格式


}

2 .检查并加载配置文件

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

3.测试

[root@chunt vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html/1232424 -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:39:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/1232424

[root@chunt vhost]#
[root@chunt vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 17:40:00 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@chunt vhost]#
[root@chunt vhost]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:01:39:52 +0800] test2.com "/admin/index.html/1232424" 301 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:01:40:00 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0"
[root@chunt vhost]#

Nginx日志切割

  • Nginx的日志很简单,不想httpd还有自带的切割工具,要想切割nginx日志需要借助系统的切割工具或者自定义脚本。
  • 创建 日志切割脚本
 vim /usr/local/sbin/nginx_logroate.sh  //脚本放在/usr/local/sbin目录下。

写入以下内容

#! /bin/bash
## 假设nginx的日志存放路径为/tmp/logs/
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@chunt vhost]# sh -x  /usr/local/sbin/nginx_logroate.sh  //-x选项是查看执行过程
++ date -d '-1 day' +%Y%m%d
+ d=20180314
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log yum.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180314
+ for log in '`ls *.log`'
+ mv yum.log yum.log-20180314
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 908
[root@chunt vhost]# ls /tmp/
ks-script-WUDGbN systemd-private-cd32fbcb5a394984b61e840dabb8208a-chronyd.service-CfRLfi test.com.log-20180314
mysql.sock systemd-private-cd32fbcb5a394984b61e840dabb8208a-vgauthd.service-Jd2v1F yum.log-20180314
pear systemd-private-cd32fbcb5a394984b61e840dabb8208a-vmtoolsd.service-e6dim4
php-fcgi.sock test.com.log

  • 时间长了是需要删除之前的日志的,可以使用find来执行
find /tmp/ -name *.log-* -type f -mtime +30  |xargs rm  //删除修改文件时间大于30天的
  • 写完脚本后,还需要增加任务计划
crontab -e //创建任务计划
0 0 * * * /bin/bash  /usr/local/sbin/nginx_logroate.sh  //里面的内容

保存即可。


静态文件不记录日志和过期时间

  • 静态文件访问是可以不用记录到日志中,因为静态文件太多了,会占用大量的系统资源
  • 配置如下:
    在test.com.conf 中加入以下内容
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //图片格式 
{
expires 7d; //过期时间
access_log off; //不记录访问日志
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
  • 检查并加载配置文件

  • 测试

[root@chunt test.com]# curl -x127.0.0.1:80 test.com/1.gif
gllgh fhkflgkhe
[root@chunt test.com]#
[root@chunt test.com]# curl -x127.0.0.1:80 test.com/2.js
l;f lfdlfb;lf;
aasdbfd.Z`
[root@chunt test.com]#
[root@chunt test.com]# curl -x127.0.0.1:80 test.com/index.html
nihao chongqing
[root@chunt test.com]# curl -x127.0.0.1:80 test.com/2.jsfdgdg //这里并不是我们所匹配的,所以会记录下来
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@chunt test.com]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:02:35:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [15/Mar/2018:02:37:05 +0800] test.com "/2.jsfdgdg" 404 "-" "curl/7.29.0"
[root@chunt test.com]#

如果在配置文件中未定义expires的话,那么curl -I 选项是没有Cache-Control的

[root@chunt test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 18:41:32 GMT
Content-Type: application/javascript
Content-Length: 26
Last-Modified: Wed, 14 Mar 2018 18:33:57 GMT
Connection: keep-alive
ETag: "5aa96b15-1a"
Expires: Thu, 15 Mar 2018 06:41:32 GMT
Cache-Control: max-age=43200 //
Accept-Ranges: bytes