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

# 其他配置...

}
总结:

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

相关推荐
DianSan_ERP2 小时前
如何通过抖店订单接口实现订单状态管理与履约自动化?
运维·自动化
b***25113 小时前
18650电池点焊机:电阻焊技术如何决定电池组的成败|深圳比斯特自动化
运维·自动化
原来是猿3 小时前
网络计算器:理解序列化与反序列化(中)
linux·运维·服务器·网络·tcp/ip
前端老曹3 小时前
Docker 从入门到放弃:完整指南
运维·docker·容器
薛定e的猫咪4 小时前
因果推理研究方向综述笔记
人工智能·笔记·深度学习·算法
AOwhisky4 小时前
虚拟化技术学习笔记
linux·运维·笔记·学习·虚拟化技术
一只机电自动化菜鸟4 小时前
一建机电备考笔记(33) 机电专业技术(起重技术-吊装方案)(含考频+题型)
经验分享·笔记·学习·职场和发展·课程设计
rabbit_pro5 小时前
Docker compose部署Ollama使用模型
linux·运维·docker
小陈phd6 小时前
多模态大模型学习笔记(四十)——从“看字”到“懂结构”:版面分析与表格解析技术全解
笔记·学习
xuhaoyu_cpp_java6 小时前
SpringMVC学习(二)
java·经验分享·笔记·学习·spring