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

# 其他配置...

}
总结:

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

相关推荐
新新学长搞科研19 分钟前
第五届电子、集成电路与通信技术国际学术会议(EICCT 2026)
运维·人工智能·自动化·集成测试·信号处理·集成学习·电气自动化
桌面运维家33 分钟前
Windows/Linux双启动:BIOS/UEFI多配置桌面创建指南
linux·运维·windows
無法複制39 分钟前
debian安装Postgresql-14.x
运维·postgresql·debian
IMPYLH1 小时前
Linux 的 chroot 命令
linux·运维·服务器
一殊酒1 小时前
【Docker】Docker Desktop 安装指南及汉化教程
运维·docker·容器
克莱因3581 小时前
Linux Cent OS7 at定时任务
linux·运维·服务器
RisunJan1 小时前
Linux命令-make(GNU的工程化编译工具)
linux·运维·gnu
闲猫1 小时前
Linux 历史命令(history)
linux·运维·chrome
Memory_荒年2 小时前
TiDB 单机部署与监控完整指南
运维·数据库·后端
耗子会飞2 小时前
小白学习centos7安装RocketMQ
运维