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

# 其他配置...

}
总结:

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

相关推荐
likfishdn11 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean30 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=32 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水41 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
崔lc4 小时前
linux中安装部署Jenkins,成功构建springboot项目详细教程
运维·jenkins
浪九天4 小时前
Nginx系列05(负载均衡、动静分离)
nginx·运维开发·持续部署
luojiaao4 小时前
【Jenkins】个人向-Jenkinsfile如何写
运维·jenkins