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

# 其他配置...

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

相关推荐

  1. Nginx

    2024-03-27 07:44:03       36 阅读
  2. 如何在 Nginx配置访问的格式?

    2024-03-27 07:44:03       22 阅读
  3. spring web接收请求,动态调整

    2024-03-27 07:44:03       63 阅读
  4. linux 查看nginx

    2024-03-27 07:44:03       39 阅读
  5. Spring Boot项目中,对接口请求参数打印

    2024-03-27 07:44:03       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 07:44:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 07:44:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 07:44:03       82 阅读
  4. Python语言-面向对象

    2024-03-27 07:44:03       91 阅读

热门阅读

  1. 数据结构-----顺序队列和链式队列的应用

    2024-03-27 07:44:03       42 阅读
  2. 使用docker的好处???(docker的优势)

    2024-03-27 07:44:03       42 阅读
  3. MongoDB面试专题

    2024-03-27 07:44:03       35 阅读
  4. Spring框架与Spring Boot的区别和联系

    2024-03-27 07:44:03       35 阅读
  5. 【前端学习——js篇】7.函数缓存

    2024-03-27 07:44:03       44 阅读
  6. 一些常见的Docker问题和答案

    2024-03-27 07:44:03       48 阅读
  7. spring中的Singleton Bean(单例bean)是否为单例模式

    2024-03-27 07:44:03       38 阅读
  8. 浅析回归算法和分类算法的异同点

    2024-03-27 07:44:03       41 阅读