赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 日志服务
  4. 操作指南
  5. 数据加工
  6. 数据加工(新版)
  7. 最佳实践
  8. 文本解析
  9. 使用SPL的正则表达式解析Nginx日志

使用SPL的正则表达式解析Nginx日志

  • 文本解析
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

Nginx访问日志记录了用户访问的详细信息,解析Nginx访问日志对业务运维具有重要意义。本文介绍如何使用正则表达式函数解析Nginx访问日志。

日志服务支持通过SPL的正则表达式解析Nginx日志。现以一条Nginx成功访问日志为例,介绍如何使用正则表达式解析Nginx成功访问日志。

  • 原始日志

    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.254.254
    __tag__:__receive_time__:  1563443076
    content: 192.168.0.2 - - [04/Jan/2019:16:06:38 +0800] "GET http://example.aliyundoc.com/_astats?application=&inf.name=eth0 HTTP/1.1" 200 273932 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.example.com/bot.html)"
  • 解析需求

    • 需求1:从Nginx日志中提取出code、ip、datetime、protocol、request、sendbytes、referer、useragent、verb信息。

    • 需求2:对request进行再提取,提取出uri_proto、uri_domain、uri_param信息。

    • 需求3:对解析出来的uri_param进行再提取,提取出uri_path、uri_query信息。

  • SLS SPL编排

    • 总编排

      """第一步:初步解析Nginx日志"""
      * | parse-regexp content, '(\d+\.\d+\.\d+\.\d+) - - \[([\s\S]+)\] \"([A-Z]+) ([\S]*) ([\S]+)["] (\d+) (\d+) ["]([\S]*)["] ["]([\S\s]+)["]' as ip, datetime,verb,request,protocol,code,sendbytes,refere,useragent
      
      """第二步:解析第一步得到的request"""
      * | parse-regexp request, '(\w+):\/\/([a-z0-9.]*[^\/])((.+)$)' as uri_proto, uri_domain, uri_param
      
      """第三步:解析第二步得到的uri_param参数"""
      * | parse-regexp uri_param, '([^?]*)\?(.*)' as uri_path, uri_query
    • 细分编排及对应加工结果

      • 针对需求1解析Nginx日志的加工编排如下。

        * | parse-regexp content, '(\d+\.\d+\.\d+\.\d+) - - \[([\s\S]+)\] \"([A-Z]+) ([\S]*) ([\S]+)["] (\d+) (\d+) ["]([\S]*)["] ["]([\S\s]+)["]' as ip, datetime,verb,request,protocol,code,sendbytes,refere,useragent

        对应结果:

        __source__:  192.168.0.1
        __tag__:  __receive_time__:  1563443076
        code:  200
        content:  192.168.0.2 - - [04/Jan/2019:16:06:38 +0800] "GET http://example.aliyundoc.com/_astats?application=&inf.name=eth0 HTTP/1.1" 200 273932 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.example.com/bot.html)"httpversion:  1.1
        datetime:  04/Jan/2019:16:06:38 +0800
        ip:  192.168.0.2
        protocol:  HTTP/1.1
        refere:  -
        request:  http://example.aliyundoc.com/_astats?application=&inf.name=eth0
        sendbytes:  273932
        useragent:  Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.example.com/bot.html)
        verb:  GET
    • 针对需求2解析request,SPL编排如下。

      * | parse-regexp request, '(\w+):\/\/([a-z0-9.]*[^\/])((.+)$)' as uri_proto, uri_domain, uri_param

      对应结果:

      uri_param: /_astats?application=&inf.name=eth0
      uri_domain: example.aliyundoc.com
      proto: httpuri_domain: example.aliyundoc.com
    • 针对需求3解析uri_param,SPL编排如下。

      * | parse-regexp uri_param, '([^?]*)\?(.*)' as uri_path, uri_query

      对应结果:

      uri_path: /_astats
      uri_query: application=&inf.name=eth0
  • SPL最终处理结果

    __source__:  192.168.0.1
    __tag__:  __receive_time__:  1563443076
    code:  200
    content:  192.168.0.2 - - [04/Jan/2019:16:06:38 +0800] "GET http://example.aliyundoc.com/_astats?application=&inf.name=eth0 HTTP/1.1" 200 273932 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.example.com/bot.html)"httpversion:  1.1
    datetime:  04/Jan/2019:16:06:38 +0800
    ip:  192.168.0.2
    protocol:  HTTP/1.1
    refere:  -
    request:  http://example.aliyundoc.com/_astats?application=&inf.name=eth0
    sendbytes:  273932
    uri_domain:  example.aliyundoc.com
    uri_proto:  http
    uri_param: /_astats?application=&inf.name=eth0
    uri_path: /_astats
    uri_query: application=&inf.name=eth0
    useragent:  Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.example.com/bot.html)
    verb:  GET
相关文章

解析CSV格式日志 2025-04-22 10:55

本文档介绍正常形式的CVS格式日志和非正常形式的CVS格式日志如何解析。 正常形式的CSV格式日志

使用SPL的正则表达式解析Nginx日志 2025-04-22 10:55

Nginx访问日志记录了用户访问的详细信息,解析Nginx访问日志对业务运维具有重要意义。本文介绍如何使用正则表达式函数解析Nginx访问日志。 日志服务支持通过SPL的正则表达式解析Nginx日志。现以一条Nginx成功访问日志为例,介绍如何使用正则表达式解析Nginx成功访问日志。

解析Java报错日志 2025-04-22 10:55

在大数据、高并发场景下的Java应用中,分析Java报错日志并提供运维指导,可降低运营维护成本。日志服务支持采集各云产品的Java报错日志,本文介绍使用新版数据加工解析Java报错日志。 前提条件 已采集各SLS、OSS、SLB、RDS的Java错误日志到cloud_product_error_lo

提取字符串动态键值对 2025-04-22 10:55

本文档介绍如何使用SPL语句提取字符串键值对。 关键字提取 字符串动态键值对提取分为关键字提取、值提取、关键字加工和值加工。常用方案为采用parse-kv指令。以k1:

解析复杂日志中的字段 2025-04-22 10:55

本文档主要为您介绍如何使用日志服务的数据加工功能,从复杂字符串中解析出JSON对象。 从复杂字符串中解析出JSON对象,并展开特定字段

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号