Nginx日志目录没有创建,导致无法访问的问题
自己安装nginx时没太注意这点,因为安装默认会在nginx.conf中配置error.log的位置
但是我们可能会遇到安装的版本没有指定日志目录这个时候我们要手动去为其指定日志目录
我理解为主要是配置两种类型的日志:
访问日志access_log
错误日志error_log
访问日志
格式为
关键字 日志文件 格式标签
access_log <file> <name>
需要关注的点是,格式标签是通过log_format指令来指定的
关键字 格式标签 日志格式
log_format <NAME> <String>
这里的格式标签就是
acccess_log中要使用的格式标签
log_format格式变量:
$remote_addr #记录访问网站的客户端地址
$remote_user #远程客户端用户名
$time_local #记录访问时间与时区
$request #用户的http请求起始行信息
$status #http状态码,记录请求返回的状态码,例如:200、301、404等
$body_bytes_sent #服务器发送给客户端的响应body字节数
$http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。
$http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等
$http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置
通过代码来理解
#定义 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$upstream_addr $upstream_response_time $request_time ';
#指定日志保存位置
access_log logs/access.log main;
access_log参数的标签段位置: http, server, location, if in location, limit_except
错误日志
感觉这个相对更好理解一点
关键字 文件 错误级别
error_log <FILE> <LEVEL>
#error_log logs/error.log error;
错误日志级别
常见的错误日志级别有[debug | info | notice | warn | error | crit | alert | emerg],级别越高记录的信息越少
error_log参数的标签段位置: main, http, server, location