nginx配置ws日志(nginx 日志文件)
nginx 日志文件
服务器上的tomcat的catalina.out文件越来越大,查看起来很不方便,以前每次都是想起来的时候手工清理一下(cat/dev/null > catalina.out),后来发现了logratate这个工具,Ubuntu下的mysql,nginx好像也是用的这个工具还定期整理log的。配置文件为/etc/logrotate.conf,和很多其它ubuntu下的工具一下,也可以把配置文件写在/etc/logrotate.d/下面。如对我们的tomcat的log文件进行整理,sudo vi/etc/logrotate.d/tomcat,/opt/tomcat/logs/catalina.out {rotate 14dailycopytruncatecompressnotifemptymissingok}其中:rotate 7 表示保留7天的备份文件daily 表示每天整理一次copytruncate 表示先复制log文件的内容,然后再清空compress 表示压缩备分文件missingok 表示如果找不到log文件也没OKnotifempty 表示如果log文件是空的,就不进行rotate可以通过/usr/sbin/logrotate -f /etc/logrotate.conf来执行。Ubuntu有/etc/cron.daily/logrotate文件,内容为:#!/bin/shtest -x /usr/sbin/logrotate || exit 0/usr/sbin/logrotate /etc/logrotate.conf表示每天会定时执行一次这个命令通过ntp同步服务器的时间根据 Ubuntu 的文档 有两种方式可以用来使服务器的时间和ntp server同步。一种是通过ntpdate命令,如ntpdate ntp.Ubuntu .com然后在/etc/cron.daily/下新建一个文件来每天执行一次这个命令另一种是通过ntpd来更新。sudo apt-get installntp。配置文件/etc/ntp.conf,可以通过修改配置文件为改变ntp server,我们用的是210.72.145.44这个server对于Linux 的系统安全来说,日志文件是极其重要的工具。系统管理员可以使用logrotate程序用来管理系统中的最新的事件,对于Linux 的系统安全来说,日志文件是极其重要的工具。系统管理员可以使用logrotate程序用来管理系统中的最新的事件。logrotate 还可以用来备份日志文件,本篇将通过以下几部分来介绍 日志文件的管理: 1、logrotate 配置 2、缺省配置 logrotate 3、使用include 选项读取其他配置文件 4、使用include 选项覆盖缺省配置 5、为指定的文件配置转储参数一、logrotate 配置 logrotate程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过cron 程序来执行。 logrotate 程序还可以用于压缩日志文件,以及发送日志到指定的E-mail 。 logrotate 的配置文件是 /etc/logrotate.conf。主要参数如下表: 参数 功能 compress 通过gzip 压缩转储以后的日志 nocompress 不需要压缩时,用这个参数 copytruncate 用于还在打开中的日志文件,把当前日志备份并截断 nocopytruncate 备份日志文件但是不截断 create mode owner group 转储文件,使用指定的文件模式创建新的日志文件 nocreate 不建立新的日志文件 delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。 errors address 专储时的错误信息发送到指定的Email 地址 ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。 notifempty 如果是空文件的话,不转储 mail address 把转储的日志文件发送到指定的E-mail 地址 nomail 转储时不发送日志文件 olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 noolddir 转储后的日志文件和当前日志文件放在同一个目录下 prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行 postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行 daily 指定转储周期为每天 weekly 指定转储周期为每周 monthly 指定转储周期为每月 rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig,.rpmsave, v, 和 ~ size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB(sizek)或者MB (sizem).
nginx日志大量502
打开网站有时出现502 Bad Gateway可以这样解决:
1、502 Bad Gateway错误的原因是网站到客户端的链路网关路由异常。
2、要解决502 Bad Gateway错误服务端服务器问题,要先找到nginx配置的路径。然后找到nginx所在的error日志文件来查看具体原因。
3、如果是客户端浏览器配置的问题,以360浏览器为例,出现502 Bad Gateway可能是设置了代 代理导致的。
4、找到360浏览器右上角的设置,看看代理配置是不是勾选了。
5、取消浏览器代理之后,刷新一下就可以访问了。
6、如果是edge浏览器配置的问题,先找到edge浏览器,然后找到右上角选项,点击更多连接设置。
7、然后点击代理连接设置,打开IE的服务器代理就可以了。
8、如果是IE浏览器的配置问题,打开IE浏览器,找到工具栏中的internet 选项,点击internet选项进入配置页面。
9、然后点击连接,找到局域网连接设置,然后关闭代理设置。
这就是打开网站有时出现502 Bad Gateway的解决步骤。
nginx配置日志文件格式
日志对于统计排错来说非常有利的。本文总结了nginx日志相关的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
nginx有一个非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令来定义。ngx_http_log_module是用来定义请求日志格式的。
1. access_log指令
语法: access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off;
默认值: access_log logs/access.log combined;
配置段: http, server, location, if in location, limit_except
gzip压缩等级。
buffer设置内存缓存区大小。
flush保存在缓存区中的最长时间。
不记录日志:access_log off;
使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;
2. log_format指令
语法: log_format name string …;
默认值: log_format combined “…”;
配置段: http
name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式,如下所示:
log_format combined '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
log_formatcombined'$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。 $remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录 客户端IP地址和客户端请求的服务器地址。PS: 获取用户真实IP 参见http://www.ttlsa.com/html/2235.html如下所示:
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
log_formatporxy'$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
日志格式允许包含的变量注释如下:
$remote_addr, $http_x_forwarded_for 记录客户端IP地址
$remote_user 记录客户端用户名称
$request 记录请求的URL和HTTP协议
$status 记录请求状态
$body_bytes_sent 发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
$bytes_sent 发送给客户端的总字节数。
$connection 连接的序列号。
$connection_requests 当前通过一个连接获得的请求数量。
$msec 日志写入时间。单位为秒,精度是毫秒。
$pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer 记录从哪个页面链接访问过来的
$http_user_agent 记录客户端浏览器相关信息
$request_length 请求的长度(包括请求行,请求头和请求正文)。
$request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_iso8601 ISO8601标准格式下的本地时间。
$time_local 通用日志格式下的本地时间。
$remote_addr,$http_x_forwarded_for记录客户端IP地址
$remote_user记录客户端用户名称
$request记录请求的URL和HTTP协议
$status记录请求状态
$body_bytes_sent发送给客户端的字节数,不包括响应头的大小;该变量与Apache模块mod_log_config里的“%B”参数兼容。
$bytes_sent发送给客户端的总字节数。
$connection连接的序列号。
$connection_requests当前通过一个连接获得的请求数量。
$msec日志写入时间。单位为秒,精度是毫秒。
$pipe如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer记录从哪个页面链接访问过来的
$http_user_agent记录客户端浏览器相关信息
$request_length请求的长度(包括请求行,请求头和请求正文)。
$request_time请求处理时间,单位为秒,精度毫秒;从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_iso8601ISO8601标准格式下的本地时间。
$time_local通用日志格式下的本地时间。
[warning]发送给客户端的响应头拥有“sent_http_”前缀。 比如$sent_http_content_range。[/warning]
实例如下:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$gzip_ratio" $request_time $bytes_sent $request_length';
log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';
open_log_file_cache max=1000 inactive=60s;
server {
server_name ~^(www\.)?(.+)$;
access_log logs/$2-access.log main;
error_log logs/$2-error.log;
location /srcache {
access_log logs/access-srcache.log srcache_log;
}
}
}
http{
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$gzip_ratio" $request_time $bytes_sent $request_length';
log_formatsrcache_log'$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';
open_log_file_cachemax=1000inactive=60s;
server{
server_name~^(www\.)?(.+)$;
access_loglogs/$2-access.logmain;
error_loglogs/$2-error.log;
location/srcache{
access_loglogs/access-srcache.logsrcache_log;
}
}
}
3. open_log_file_cache指令
语法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
默认值: open_log_file_cache off;
配置段: http, server, location
对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下:
参数注释如下:
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid:设置检查频率,默认60s
off:禁用缓存
实例如下:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
1
open_log_file_cachemax=1000inactive=20svalid=1mmin_uses=2;
4. log_not_found指令
语法: log_not_found on | off;
默认值: log_not_found on;
配置段: http, server, location
是否在error_log中记录不存在的错误。默认是。
5. log_subrequest指令
语法: log_subrequest on | off;
默认值: log_subrequest off;
配置段: http, server, location
是否在access_log中记录子请求的访问日志。默认不记录。
6. rewrite_log指令
由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南
语法: rewrite_log on | off;
默认值: rewrite_log off;
配置段: http, server, location, if
启用时将在error log中记录notice级别的重写日志。
7. error_log指令
语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默认值: error_log logs/error.log error;
配置段: main, http, server, location
配置错误日志。
nginx 日志位置
我的回答:nginx要学习好多天。
可以学习nginx日志切割、反向代理、正则表达式等内容,工作中会用到。
nginx 配置日志
nginx配置好负载分担后,测试的时候,如何查看负载分担情况:通过设置nginx日志显示:nginx服务器日志相关指令主要有两条,一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径、格式和缓存大小,一般在nginx的配置文件中日记配置(/usr/local/nginx/conf/nginx.conf)。nginx的log_format有很多可选的参数用于指示服务器的活动状态,默认的是:log_formataccess'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"';
本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.