【笔记】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;
        # 其他配置...
    }
}

# 其他配置...

}
总结:

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

相关推荐
-Springer-11 分钟前
STM32 学习 —— 个人学习笔记1(STM32简介)
笔记·stm32·学习
故乡de云20 分钟前
2026年谷歌云价格走势深度分析:企业如何在高成本时代保持竞争力?
运维·kubernetes·云计算
j_xxx404_26 分钟前
Linux:自主shell命令行解释器附源码
linux·运维·服务器
何以不说话1 小时前
堡垒机jumpserver
运维·sql
开开心心就好1 小时前
开源免费高速看图工具,支持漫画大图秒开
linux·运维·服务器·安全·ruby·symfony·1024程序员节
林深现海1 小时前
【刘二大人】PyTorch深度学习实践笔记 —— 第三集:梯度下降(凝练版)
pytorch·笔记·深度学习
悠哉悠哉愿意1 小时前
【物联网学习笔记】按键
笔记·单片机·嵌入式硬件·物联网·学习
三水不滴1 小时前
对比一下RabbitMQ和RocketMQ
经验分享·笔记·分布式·rabbitmq·rocketmq
困死,根本不会1 小时前
OpenCV摄像头实时处理:九宫格棋盘检测与棋子识别
笔记·opencv·学习
小Pawn爷1 小时前
13.virtualbox安装ubuntu
linux·运维·ubuntu