访问日志:用户访问网站的记录
主要就是配置路径加格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
对应变量含义:
变量 | 含义 |
---|---|
$remote_addr | 客户端IP(公网IP) |
$http_x_forwarded_for | 代理服务器的IP |
$time_local | 服务器本地时间 |
$host | 访问主机名(域名) |
$request_uri | 访问的url地址 |
$status | 状态码 |
$http_referer | referer |
$http_user_agent | user_agent |
长用全局变量汇总:https://blog.csdn.net/arsenal4life/article/details/102616484
2)、虚拟主机配置 /etc/nginx/conf.d/bbs.ars4life.com.conf 的最下方,加入一行 access_log
access_log /data/logs/bbs.ars4life.com.access.log main;
用于定义访问日志
2、改好配置后,需要创建 /data/logs/ 目录,否则会报错
[root@alexis-01 ~]# mkdir /data/logs/
[root@alexis-01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@alexis-01 ~]# nginx -s reload
[root@alexis-01 ~]# cat /data/logs/bbs.ars4life.com.access.log
3、访问网页后,日志会产生以下内容
[root@alexis-01 ~]# cat /data/logs/bbs.ars4life.com.access.log
192.168.194.1 - user1 [17/Oct/2019:22:13:47 +0800] "GET /forum.php?mod=forumdisplay&fid=2 HTTP/1.1" 200 26614 "http://444444.com/forum.php?gid=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - user1 [17/Oct/2019:22:13:47 +0800] "GET /data/cache/style_1_forum_forumdisplay.css?fv2 HTTP/1.1" 304 0 "http://444444.com/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - - [17/Oct/2019:22:13:47 +0800] "GET /static/image/common/fall.png HTTP/1.1" 301 169 "http://444444.com/data/cache/style_1_forum_forumdisplay.css?fv2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - user1 [17/Oct/2019:22:13:47 +0800] "GET /static/image/common/fall.png HTTP/1.1" 200 843 "http://444444.com/data/cache/style_1_forum_forumdisplay.css?fv2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - user1 [17/Oct/2019:22:13:47 +0800] "GET /misc.php?mod=seccode&action=update&idhash=cSgMt1Yd&0.14961814848941724&modid=forum::forumdisplay HTTP/1.1" 200 1553 "http://444444.com/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - user1 [17/Oct/2019:22:13:47 +0800] "GET /misc.php?mod=seccode&update=44442&idhash=cSgMt1Yd HTTP/1.1" 200 665 "http://444444.com/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - user1 [17/Oct/2019:22:13:47 +0800] "GET /misc.php?mod=patch&action=pluginnotice&inajax=1&ajaxtarget=plugin_notice HTTP/1.1" 200 76 "http://444444.com/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
在访问日志里,过滤掉一些图片、js、css类的请求日志
因为此类请求没有多大用处,而且会占用很大的磁盘空间
location ~* \.(png|jpeg|gif|js|css|bmp|flv)
{
access_log off;
}
清空访问日志,再去访问网页,之后查看访问日志,就不会出现以上后缀名的记录了
[root@alexis-01 ~]# tail /data/logs/bbs.ars4life.com.access.log
192.168.194.1 - - [17/Oct/2019:23:04:24 +0800] "GET /install/index.php HTTP/1.1" 200 1345 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - - [17/Oct/2019:23:04:25 +0800] "GET /favicon.ico HTTP/1.1" 304 0 "http://bbs.ars4life.com/install/index.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
192.168.194.1 - - [17/Oct/2019:23:04:59 +0800] "GET /favicon.ico HTTP/1.1" 200 5558 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"
192.168.194.1 - - [17/Oct/2019:23:05:00 +0800] "GET / HTTP/1.1" 200 12310 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"
192.168.194.1 - - [17/Oct/2019:23:05:00 +0800] "GET /home.php?mod=misc&ac=sendmail&rand=1571324700 HTTP/1.1" 200 5 "http://bbs.ars4life.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"
192.168.194.1 - - [17/Oct/2019:23:05:00 +0800] "GET / HTTP/1.1" 200 12137 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
补充:
tail -f /data/logs/bbs.ars4life.com // -f 可以动态查看一个文件的内容
> 可以清空一个文件内容
~* 表示不区分大小写的匹配,后面跟正则表达式
. 表示任意字符
日志切割目的: 访问日志有很多,日志会越来越大,可能有一天就会把整个磁盘写满,100G的日志日和查看???
[root@alexis-01 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
[root@alexis-01 ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log /data/logs/*.log{
daily
dateext
missingok
rotate 7
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
daily 每天
dateext 以日期为后缀
missingok 忽略错误
rotate 7 保留7天
compress 压缩
delaycompress 延后压缩(今天切割,昨天有一个文件,前天的文件可以压缩,昨天的不压缩)
notifempty 如果是空文件,不进行切割
create 640 nginx adm 创建回滚文件的权限,用户和用户组
sharedscripts 执行脚本
[root@alexis-01 ~]# logrotate -v /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /var/log/nginx/*.log after 1 days (52 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
log does not need rotating (log has been rotated at 2019-10-17 12:12, that is not day ago yet)
considering log /var/log/nginx/error.log
log does not need rotating (log has been rotated at 2019-10-17 12:12, that is not day ago yet)
not running postrotate script, since no logs were rotated
set default create context
[root@alexis-01 ~]# ll /data/logs/
总用量 4
-rw-r--r--. 1 root root 1648 10月 17 23:11 bbs.ars4life.com.access.log
[root@alexis-01 ~]# ll /var/log/nginx/
总用量 284
-rw-r-----. 1 nginx adm 98028 10月 17 23:05 access.log
-rw-r-----. 1 nginx adm 88509 10月 17 12:06 access.log-20191017
-rw-r-----. 1 nginx adm 22806 10月 17 23:05 error.log
-rw-r-----. 1 nginx adm 77693 10月 17 12:05 error.log-20191017
/var/log/nginx 下的日志已切割
服务端静态文件已经更新了,但是一直在请求旧的缓存,这样不合理,需要设定过期时间
[root@alexis-01 ~]# curl -x127.0.0.1:80 -I http://bbs.ars4life.com/static/image/common/logo_88_31.gif
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Fri, 18 Oct 2019 13:05:36 GMT
Content-Type: image/gif
Content-Length: 2528
Last-Modified: Wed, 16 Oct 2019 15:33:50 GMT
Connection: keep-alive
ETag: "5da7385e-9e0"
Accept-Ranges: bytes
[root@alexis-01 ~]# curl -I http://www.apelearn.com/src/images/lesson_16.png
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 18 Oct 2019 12:58:46 GMT
Content-Type: image/png
Content-Length: 5524
Connection: keep-alive
Last-Modified: Wed, 22 Nov 2017 09:10:54 GMT
ETag: "5a153f1e-1594"
Expires: Sun, 17 Nov 2019 12:58:46 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
max-age=2592000,单位是秒
也就是服务器上的过期时间为30天
location ~* \.(png|jpeg|gif|js|css|bmp|flv)$
{
expires 1d;
access_log off;
}
[root@alexis-01 ~]# curl -x127.0.0.1:80 -I http://bbs.ars4life.com/static/image/common/logo_88_31.gif
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Fri, 18 Oct 2019 13:09:21 GMT
Content-Type: image/gif
Content-Length: 2528
Last-Modified: Wed, 16 Oct 2019 15:33:50 GMT
Connection: keep-alive
ETag: "5da7385e-9e0"
Expires: Sat, 19 Oct 2019 13:09:21 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes
设置完成后,过期时间为 24 小时
总结:
curl -x 用来指定目标服务器的IP和端口,例如:curl -x127.0.0.1:80 -I www.baidu.com
bc 是一个linux系统下的计算器,yum install -y bc
防盗链:
两个网站 A和 B,A 网站引用了 B 网站上的图片,这种行为就叫做盗链。
防盗链,就是要防止 A 应用 B 的图片
编辑虚拟主机配置文件,添加以下一段
location ~ \.(png|jpg|gif|jpeg|bmp|mp3|mp4|flv)$
{
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
}
}
valid_referers 指的是白名单的 referer
none 表示空 referer,图片是打开的而不是跳转的
blocked 非法的链接,正常链接是 http:// 或 https:// 开头,如果不以他们开头的链接也可以成为合法的 referer,用的不多
server_names 就是上方定义的 server_name
*.test.com 就是白名单
1、准备: 先在 blog.ars4life.com 中上传一张图片,图片链接为 http://blog.ars4life.com/gakki.jpg
我们在bbs.ars4life.com 中发帖,并上传 http://blog.ars4life.com/gakki.jpg 图片到论坛中
此时我们还未进行防盗链配置,图片是可以正常显示的
location ~ \.(png|jpg|gif|jpeg|bmp|mp3|mp4|flv)$
{
valid_referers none blocked server_names www.ars4life.com ;
if ($invalid_referer) {
return 403;
}
access_log off;
expires 1d;
}
(以上字段只是防盗链部分,其余部分不在这里展示)
[root@alexis-01 blog.ars4life.com]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@alexis-01 blog.ars4life.com]# nginx -s reload
4、刷新页面,发现其他人已经无法查看到图片
5、防盗链成功!
补充:
rz 上传文件,yum install lrzsz
sz filename 这样去吧文件推送到win机器里
测试防盗链: curl -I -e “http://www.aaa.com/1.txt” http://www.ars4life.com/1.png
curl -e 指定自定义的 referer,只有白名单referer才会是200