Nginx日志格式设置
1.访问日志
nginx服务器日志相关指令主要有两条,一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径、格式和缓存大小,一般在nginx的配置文件中日记配置(/usr/local/nginx/conf/nginx.conf
)。
nginx的log_format有很多可选的参数用于指示服务器的活动状态,默认的是:
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
想要记录更详细的信息需要自己设置log_format,具体可设置的参数格式及说明如下:
参数 | 说明 | 示例 |
---|---|---|
$remote_addr |
客户端地址 | 211.28.65.253 |
$remote_user |
客户端用户名称 | - |
$time_local |
访问时间和时区 | 18/Jul/2012:17:00:01 +0800 |
$request |
请求的URI和HTTP协议 | “GET /article-10000.html HTTP/1.1” |
$http_host |
请求地址,即浏览器中你输入的地址(IP或域名) | www.gmtgo.com/192.168.100.100 |
$status |
HTTP请求状态 | 200 |
$upstream_status |
upstream状态 | 200 |
$body_bytes_sent |
发送给客户端文件内容大小 | 1547 |
$http_referer |
url跳转来源 | https://www.gmtgo.com/ |
$http_user_agent |
用户终端浏览器等信息 | “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; |
$ssl_protocol |
SSL协议版本 | TLSv1 |
$ssl_cipher |
交换数据中的算法 | RC4-SHA |
$upstream_addr |
后台upstream的地址,即真正提供服务的主机地址 | 10.10.10.100:80 |
$request_time |
整个请求的总时间 | 0.205 |
$upstream_response_time |
请求过程中,upstream响应时间 | 0.002 |
1.1 举例说明
1.1.1 配置文件
#vim /usr/local/nginx/conf/nginx.conf
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for '
'"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';
include /usr/local/nginx/conf/vhost/*.conf;
1.1.2 vhost中配置文件
#vim /usr/local/nginx/conf/vhost/web.confserver
{
listen 80 default;
server_name www.it300.com;
index index.html index.htm index.php;
root /data/httpd/it300.com;
location ~ .*\.php?$
{
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /data/logs/it300.com.log access;
}
2.错误日志
错误日志主要记录客户端访问Nginx出错时的日志格式不支持自定义。通过错误日志你可以得到系统某个服务或server的性能瓶颈等。因此将日志好好利用你可以得到很多有价值的信息。
错误日志由指令error_log来指定具体格式如下:
- error_log path(存放路径) level(日志等级)
- path含义同access_loglevel表示日志等级具体如下:
- [ debug | info | notice | warn | error | crit ]
- 从左至右日志详细程度逐级递减即debug最详细crit最少。
2.1 举例说明
error_log logs/error.log info;
需要注意的是error_log off并不能关闭错误日志而是会将错误日志记录到一个文件名为off的文件中。
正确的关闭错误日志记录功能的方法如下
error_log /dev/null;
上面表示将存储日志的路径设置为“垃圾桶”。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!