Nginx 笔记与总结(4)配置 server 访问日志

打开 nginx.conf:

[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf

在默认的 server 段中包含以下内容:

#access_log  logs/access.log  main;

表示该 server 的访问日志文件是 logs/access.log

Nginx 允许针对不同的 server 做不同的 log

main 表示日志使用的格式是 'main' 格式,还可以自定义其他格式。

在 http 端中,有如下内容:

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

该内容表示 main 类型的日志记录的 remote_addr ... http_x_forward_for 等选项。

打开 logs/access.log 文件进行观察:

$remote_addr:远程 ip 地址,这里是 192.168.254.1

$remote_user[$time_local]:远程用户[访问的时间],访问的时候 http 的头信息未必带了 remote_user,所以这里显示 -

$request:请求类型,这里几个都是 GET 请求,使用的是 HTTP/1.1 协议

$status:请求状态,这里看到了 200 和 304

$body_bytes_sent:响应返回了多少字节

$http_referer:用户来自(上一个浏览地址)

$http_user_agent:用户代理(浏览器信息或者蜘蛛信息,查看 http://www.baidu.com/robots.txt

$http_x_forward_for:在经过代理时,代理把本来的 IP 加在此头信息中,传输原始的 IP

服务器会选择以 $remote_addr 还是 $http_x_forwad_for 来判断用户的 ip

配置访问日志:

【例】

配置 dee.com 的访问日志

[root@localhost conf]# vim nginx.conf 

① 把 log_format  main 前面的 # 去掉:

② 添加 access_log

保存退出。

平滑重启 Nginx。

进入 logs 目录:

此时 dee.com.access.log 大小是 0 

访问 dee.com,查看 dee.com.access.log:

宿主机的 ip 是 192.168.254.1:

如果发现网站访问量急剧增加,首先检查该日志文件,判断是蜘蛛爬行造成的还是来自对手的攻击,从而选择是封对方 ip 还是暂停敏感页面。

原文地址:https://www.cnblogs.com/dee0912/p/4676833.html