[nginx]日志中记录自定义请求头

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: [nginx]日志中记录自定义请求头

前言

假设在请求中自定义了一个请求头,key为"version",参数值为“1.2.3”,需要在日志中捕获这个请求头。

nginx日志配置

只需要用变量http_version就能捕获到自定义的version请求头。示例:

log_format main '{"@timestamp": "$time_iso8601", '
      '"connection": "$connection", '
      '"version": "$http_version", '
      '"remote_addr": "$remote_addr", '
      '"remote_user": "$remote_user", '
      '"request_method": "$request_method", '
      '"request_uri": "$request_uri", '
      '"request_length": "$request_length", '
      '"server_protocol": "$server_protocol", '
      '"status": "$status", '
      '"body_bytes_sent": "$body_bytes_sent", '
      '"http_referer": "$http_referer", '
      '"http_user_agent": "$http_user_agent", '
      '"http_x_forwarded_for": "$http_x_forwarded_for", '
      '"upstream_addr": "$upstream_addr", '
      '"request_time": "$request_time"}';

测试

  1. 使用curl请求,指定请求头
curl -I -s http://127.0.0.1 -H 'version:123456' -o /dev/null
  1. 观察nginx请求日志是否记录到version值
{"@timestamp": "2023-04-12T15:14:13+00:00", "connection": "12", "version": "123456", "remote_addr": "172.1.7.1", "remote_user": "-", "request_method": "HEAD", "request_uri": "/", "request_length": "90", "server_protocol": "HTTP/1.1", "status": "200", "body_bytes_sent": "0", "http_referer": "-", "http_user_agent": "curl/7.74.0", "http_x_forwarded_for": "-", "upstream_addr": "-", "request_time": "0.000"}
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
12天前
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
41 5
|
18天前
|
Docker 容器
docker nginx-proxy 添加自定义https网站
docker nginx-proxy 添加自定义https网站
22 4
|
1月前
|
Shell Python
salt自定义模块内使用日志例子
salt自定义模块内使用日志例子
|
2月前
|
应用服务中间件 nginx
nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
【8月更文挑战第27天】nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
167 6
|
2月前
|
Kubernetes 数据安全/隐私保护 容器
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
|
2月前
|
开发框架 .NET Docker
【Azure 应用服务】App Service .NET Core项目在Program.cs中自定义添加的logger.LogInformation,部署到App Service上后日志不显示Log Stream中的问题
【Azure 应用服务】App Service .NET Core项目在Program.cs中自定义添加的logger.LogInformation,部署到App Service上后日志不显示Log Stream中的问题
|
2月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
2月前
|
存储 监控 应用服务中间件
查看nginx日志文件
器性能和提高网站可用性。掌握日志文件的路径、查看方法和基本分析技能对于任何服务器管理员来说都是必备技能。
99 1
|
2月前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
27 4
|
2月前
|
Ubuntu 搜索推荐 应用服务中间件
如何在 Ubuntu 14.04 上配置 Nginx 使用自定义错误页面
如何在 Ubuntu 14.04 上配置 Nginx 使用自定义错误页面
47 2