2019独角兽企业重金招聘Python工程师标准>>>
概述
- 前文写了关于Nginx环境配置,但是还没有完,接下来将会继续讲三个相关的配置
- 主要是以下三个 1.Nginx访问日志 2.Nginx日志切割 3.静态文件不记录日志和过期时间
Nginx访问日志
1.先看看日志格式
#日志的路径
[root@centos7mei ~]# vim /usr/local/ngin/conf/nginx.conf
#搜索log_format
#注意下面这段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_referer | referer |
$http_user_agent | user_agent |
2.刚才在主配置文件中定义了日志格式,接下来还需要在虚拟主机配置中定义日志的储存路径,最后面指定日志的格式名字
[root@centos7mei ~]# cd /usr/local/nginx/conf/vhost/
[root@centos7mei vhost]# ls
default.conf test.com.conf
#编辑配置文件
[root@centos7mei vhost]# vim test.com.conf
#内容如下
[root@centos7mei vhost]# cat 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/1.log combined_realip;
}
#检查读写并重新加载服务
[root@centos7mei 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@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
3.测试
#访问两次网站
[root@centos7mei vhost]# curl -x127.0.0.1:80 test3.com/index.html/SDFAS -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Fri, 17 Aug 2018 18:24:04 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html/SDFAS[root@centos7mei vhost]# curl -x127.0.0.1:80 test2.com/index.html/SDFAS -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Fri, 17 Aug 2018 18:24:23 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html/SDFAS
#这里的问题是直接抄视频,没看自己写的具体路径
[root@centos7mei vhost]# cat /tmp/test.com.log
cat: /tmp/test.com.log: No such file or directory
#这下面就能看到访问日志了
[root@centos7mei vhost]# cat /tmp/1.log
127.0.0.1 - [18/Aug/2018:02:24:04 +0800] test3.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"
127.0.0.1 - [18/Aug/2018:02:24:23 +0800] test2.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"
Nginx日志切割
- 日志记录了很多东西,但不是每个都是有用的,所以需要对其做一些操作,去其糟粕取其精华。
- 切割日志需要用到shell脚本
1.添加脚本
[root@centos7mei vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
#脚本内容
[root@centos7mei vhost]# cat /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
## 假设nginx的日志存放路径为/data/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`
domv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
#执行一下脚本
[root@centos7mei vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180817
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls 1.log
+ for log in '`ls *.log`'
+ mv 1.log 1.log-20180817
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 16489
2,测试
#清理文件
[root@centos7mei vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
#这里报错是因为没有满足条件的文件
rm: missing operand
Try 'rm --help' for more information.
#查找下
[root@centos7mei vhost]# find /tmp/ -name *.log-* -type f
/tmp/1.log-20180817
## 最后添加计划任务,清理文件总不能我们自己每天手动清理吧
[root@centos7mei vhost]# crontab -e
#内容
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
静态文件不记录日志和过期时间
- 静态文件可以不记录日志,原因是除了占空间,没别的什么用
ps: 不记录日志就是说我们在访客记录中看不到,访客访问静态文件的信息
- 简单的来说就是添加配置文件,让访问日志不记录静态文件,还有给图片缓存添加过期时间
1.编辑配置文件
#进入虚拟主机配置文件
[root@centos7mei vhost]# vim test.com.conf
#内容
[root@centos7mei vhost]# cat 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前面的括号完location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${#过期时间expires 7d;#表示 不记录访问日志access_log off;}location ~ .*\.(js|css)${expires 12h;access_log off;}access_log /tmp/1.log combined_realip;
}
#检查读写和重新加载服务
[root@centos7mei 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@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
2.测试
#这里我们创建两个图片文件,就是图片格式结尾的文件,然后去访问,最后查看访问日志
[root@centos7mei vhost]# cd /data/wwwroot/test.com/
[root@centos7mei test.com]# ls
index.html
#这里马虎的出现了一个符号错误
[root@centos7mei test.com]# vim 1,gif
[root@centos7mei test.com]# vim 2.gs
#导致访问出现404
[root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/1.gif404 Not Found
#原来是点写成了逗号,根本没那个文件,所以报错了
[root@centos7mei test.com]# ls
1,gif 2.gs index.html
[root@centos7mei test.com]# rm -f 1,gif
[root@centos7mei test.com]# ls
2.gs index.html
[root@centos7mei test.com]# vim 1.gif
[root@centos7mei test.com]# ls
1.gif 2.gs index.html
#连接下
[root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/1.gif
fdsafs:
[root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/2.gs
dasdfsfdadf
[root@centos7mei test.com]# curl -I -x127.0.0.1:80 test.com/1.GIF
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Sat, 18 Aug 2018 01:21:14 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
#添加-I选项能看到更加详细的信息
[root@centos7mei test.com]# curl -I -x127.0.0.1:80 test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sat, 18 Aug 2018 01:21:21 GMT
Content-Type: image/gif
Content-Length: 8
Last-Modified: Sat, 18 Aug 2018 01:04:52 GMT
Connection: keep-alive
ETag: "5b7770b4-8"
Expires: Sat, 25 Aug 2018 01:21:21 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
#这里能看到我们访问的1.gif(小写的)就没有记录到日志里面了
[root@centos7mei test.com]# cat /tmp/1.log
127.0.0.1 - [18/Aug/2018:03:35:40 +0800] test2.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"
127.0.0.1 - [18/Aug/2018:09:21:14 +0800] test.com "/1.GIF" 404 "-" "curl/7.29.0"