一、Nginx 访客日志功能
日志对于程序员很重要的,可以用于问题排错,记录程序运行的状态,一个好的日志配置,能够给与运维人员,开发人员精准定位
Nginx开启日志功能只需要在nginx.conf里面找到log_format参数,定义日志的格式,以及定义日志的存储位置以及日志格式、路径、缓存大小
可以定义在全局配置中
nginx.conf web功能核心配置点
http { include mime.types; default_type application/octet-stream; # 定义日志的内容格式 (记录内容的详细程度) log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main; ... }
Nginx 的访客日志内容如下:
185.7.214.104 - - [29/Jul/2022:16:29:04 +0800] "GET /console/ HTTP/1.1" 404 185 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" 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地址 $remote_user 记录远程客户端的访客用户名 $time_local 记录访问的时间和地区信息 $request 记录用户的http请求首行信息 $status 记录用户的http请求状态,也就是请求发出之后,响应的状态 如200 301 404 502 $body_bytes_sent 记录服务器发给客户端的响应体数据字节大小 $http_referer 记录本次请求是从哪个链接过来的,可以根据referer信息来进行防盗链 $http_user_agent 记录客户端的访问信息,如浏览器信息,手机浏览器信息 $http_x_forwarded_for 捉到藏在代理服务器后面的真实客户端ip信息代理ip