Nginx 日志配置说明

编程教程 > nginx (5) 2025-04-24 16:42:22

Nginx 配置日志

Nginx配置文件

nginx默认的日志配置在nginx.conf配置文件。

Nginx 默认日志配置格式

log_format有一个默认的、无须设置的combined日志格式设置,相当于Apache的combined日志格式,其具体参数如下:


log_format main  '$remote_addr-$remote_user [$time_local]'
                                ' "$request" $status $body_bytes_sent'
                                ' "$http_referer" "$http_user_agent" ';

 

自定义格式【推荐格式】

直接修改main的

log_format main  '$http_x_forwarded_for $remote_addr [$time_local] "http://$host" "$request" '
                          '$status $body_bytes_sent "$http_referer" "$http_user_agent" "$remote_user" ';

自定义一个新的

log_format access  '$http_x_forwarded_for $remote_addr [$time_local] "http://$host" "$request" '
                          '$status $body_bytes_sent "$http_referer" "$http_user_agent" "$remote_user" ';


access_log  /data/logs/nginx/my_access.log access;

 

更多日志内置变量

参数                      说明                                         示例
$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.wang.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

 

打印HTTP 头部参数

打印HTTP协议Header头部 自定义头参数和非自定义头

需求参考:

Nginx 日志配置说明_图示-39cae1f10a404cfe9cd04529c5a71e3c.png

日志打印部分示例参考:

log_format main  '$http_token - $http_access_token';

 

 


评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
Nginx 配置日志nginx默认的日志配置在nginx.conf配置文件。
nginx日志默认格式如下:$remote_addr-$remote_user[$time_local]"$request"$status$body_bytes_sent"$http_refer...
Nginx 安装Nginx 安装参考:Linux yum nginx安装和nginx入门配置方法-XQLEE'BlogAlpine Linux nginx安装配
Linux(centos) nginx安装和nginx入门配置方法1.切换至root用户$su - 2.执行在线安装命令#yum install nginx -y 如果上面提示以下内容[root...
alpineLinux安装nginx更新软件apkupdate安装nginxapkaddnginx查看nginx版本nginx-vnginx启停启动停止servicenginxstop重启ser...
nginx常规使用时作为http协议的反向代理,这里要讲的是非http的Mysql端口代理,理论支持其他协议nginx.conf配置如下:user nginx; worker_process...
最近博客升级总是nginx原生的502页面,感觉有些不友好,特此研究了下502页面的配置nginx 502配置502页面核心配置 #502 page error_...
需求将静态资源如css/js/图片缓存到不用每次都到后端服务去拿取,减少性能消耗。 配置nginx缓存配置缓存文件存放信息在nginx配置文件,httpd内部配
nginx开启gzip压缩配置配置参考:server{listen443ssl;server_namet.example.com;#charsetkoi8-r;#access_log/var/l...
打开任务管理器发现一大堆nginx进程
最近网站被恶意访问了,了解了一些基础防护手段,nginx屏蔽ua访问 编写文件agent_deany.conf #使用方法 #创建目录 /etc/nginx/conf.d/deny...
PVE配置nginx代理访问。nginx配置:server { listen 10101 ssl; server_name pve.ok.top; #ssl on; #从腾...
1.新增nginx屏蔽配置文件文件暂时为/etc/nginx/conf.d/deny_ua.config说明:文件名deny_ua.config,后缀为.config非.conf,原因是.con...
Nginx 配置robots.txt禁止收录,在对应的server里面添加以下配置 location =/robots.txt { default_type t
解决centos系统使用yum安装nginx启动时报错[::]:80 failed (97: Address family not supported by protocol)一般默认的使用yu...