【笔记】Nginx配置类似Tomcat请求接口链路access_log日志

项目部署在tomcat容器中,请求的接口会被记录在文件名:localhost_access_log.2024-03-22.log的文件中,如果使用Nginx也需要记录请求接口,该如何做呢?步骤如下
步骤1:

打开nginx.conf,在 http 块中添加自定义的 map和log_format,例如:

复制代码
map $time_iso8601 $log_date {
        "~^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})" $year-$month-$day;
    }

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

步骤2:

在location添加请求接口日志写入文件路径下的文件名

复制代码
 access_log /var/log/nginx/localhost_access_log.$log_date.log custom_format;

完整如下:

http {

log_format api_log 'remote_addr - r e m o t e u s e r \[ remote_user \[ remoteuser\[time_local\] "request" $status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer" " h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""request_uri"';

复制代码
server {
    listen 80;
    server_name example.com;

    location / {
        access_log /var/log/nginx/api_access.log api_log;
        # 其他配置...
    }
}

# 其他配置...

}
总结:

有时候记录每一次请求的链路过程并落盘还是很有必要,以上仅供参考,亲测有效.

相关推荐
Nobody__18 分钟前
解决多台服务器 UID/GID 做对齐后,文件系统元数据未更新的情况
运维·服务器
im_AMBER17 分钟前
Leetcode 99 删除排序链表中的重复元素 | 合并两个链表
数据结构·笔记·学习·算法·leetcode·链表
创作者mateo19 分钟前
PyTorch 入门笔记配套【完整练习代码】
人工智能·pytorch·笔记
米汤爱学习28 分钟前
stable-diffusion-webui【笔记】
笔记·stable diffusion
珂玥c38 分钟前
windows系统nfs挂载
运维·windows·ssh·remmina·cygwin
菜择贰40 分钟前
在linux(wayland)中禁用键盘
linux·运维·chrome
创作者mateo1 小时前
PyTorch 入门学习笔记(基础篇)一
pytorch·笔记·学习
霸气十足+拼命+追梦少年1 小时前
服务器挂载U盘或硬盘
运维·服务器
慕容雪_1 小时前
运维笔记-笔记本无线网卡连接wifi,有线网卡连接内网
运维·内网·远程
hkNaruto1 小时前
【AI】AI学习笔记:关于嵌入模型的切片大小,实际的业务系统中如何选择
人工智能·笔记·学习