nginx日志自定义配置
问题
有时nginx默认的日志满足不了需求,比如网站绑定了多个域名,想知道用于访问的是具体域名,这时候就需要自定义nginx日志了。
nginx 中access log 记录了用户访问的页面以及用户浏览器、ip和其他的访问信息
log_format,用来设置日志格式,access_log,用来指定日志文件的存放路径、格式和缓存大小
- log_format语法
log_format name [escape=default|json] string ...;
name 格式名称。在access_log指令中引用。
escape 设置变量中的字符编码方式是json还是default,默认是default。
string 要定义的日志格式内容。该参数可以有多个。参数中可以使用Nginx变量
示例
log_format main
'$remote_addr [$time_local] $http_host "$request" '
'$status "$http_referer" '
'$http_user_agent $request_body [$body_bytes_sent,$request_time] ';
配置方法
编辑配置文件nginx.conf
添加自定义日志命令,要写在server外面http下
宝塔面板如下设置
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;events{use epoll;worker_connections 51200;multi_accept on;}http{include mime.types;#include luawaf.conf;include proxy.conf;log_format main'$remote_addr $request_uri [$time_local] $http_host "$request" '
'$status "$http_referer" '
'$http_user_agent $request_body [$body_bytes_sent,$request_time] ';default_type application/octet-stream;server_tokens off;access_log off;server{listen 888;server_name phpmyadmin;index index.html index.htm index.php;root /www/server/phpmyadmin;location ~ /tmp/ {return 403;}#error_page 404 /404.html;include enable-php.conf;access_log /www/wwwlogs/access.log main;}
include /www/server/panel/vhost/nginx/*.conf;
}
- 具体可以设置的格式如下
参数 说明 示例
$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.it300.com/192.168.100.100
$status HTTP请求状态 200
$upstream_status upstream状态 200
$body_bytes_sent 发送给客户端文件内容大小 1547
$http_referer url跳转来源 https://www.baidu.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
vhost配置
在需要记录日志的站点的配置里access_log后面加个main(跟上面同一个名字就可以)
access_log path