LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

nginx中应用正则表达式

admin
2025年6月28日 22:30 本文热度 24

  1. ^ 匹配一行开头
    • nginx配置
      # ^测试
      location ~ ^/imgs {
       return 200 "匹配/imgs开头的正则表达式";
      }
    • 测试
      ### ngxinx响应:匹配/imgs开头的正则表达式
      GET http://localhost:85/imgs/123
  2. $ 匹配一行结尾
    • nginx配置
      # $测试
      location ~ \.js$ {
          return 200 "匹配js文件";
      }
    • 测试
      ### ngxinx响应:匹配js文件
      GET http://localhost:85/http.js
  3. [abc] 匹配[]内的任意一个字符
    • nginx配置
      # [abc]测试
      location ~ [a-zA-Z].html$ {
          return 200 "匹配英文字母的html";
      }
    • 测试
      ### ngxinx响应:匹配英文字母的html
      GET http://localhost:85/demo.html

      ### ngxinx响应:404 Not Found。因为demo1中含有数据不合规则
      GET http://localhost:85/demo1.html
  4. ? 前面的字符匹配0或1次
    • nginx配置
      location ~ ^/users?$ {
          return 200 "匹配user结尾 或 users结尾";
      }
    • 测试
      ### ngxinx响应:匹配user结尾 或 users结尾
      GET http://localhost:85/user

      ### ngxinx响应:匹配user结尾 或 users结尾
      GET http://localhost:85/users

      ### ngxinx响应:匹配user结尾 或 users结尾。因为nginx的locaton匹配的是URI(统一资源标识符),不是URL(统一资源定位符),URL比URI范围更广,URL还包含查询参数如?id=123、片段标识符如#section1
      GET http://localhost:85/user?name=abc

      ### ngxinx响应:404 Not Found。不符合匹配规则。
      GET http://localhost:85/usernames
  5. + 前面的字符匹配1或多次
    • nginx配置
      # +测试、\w测试,匹配多个路径参数,/\w+表示路径参数,(/\w+)+表示1个以个路径参数,((/\w+)+)+将多个路径参数分组
      location ~ ^/api((/\w+)+)+$ {
          return 200 $1;
      }
    • 测试
      ### ngxinx响应:404 Not Found。没有路径参数匹配不到
      GET http://localhost:85/api


      ### ngxinx响应:/user。匹配1个路径/user
      GET http://localhost:85/api/user


      ### ngxinx响应:/user/books
      GET http://localhost:85/api/user/books


      ### ngxinx响应:/user/book/12
      GET http://localhost:85/api/user/book/12
  6. * 前面的字符匹配0或多次
    • nginx配置
      # *测试、\w测试,匹配0或多个路径参数
      location ~ ^/dev-api((/\w+)+)*$ {
          return 200 $1;
      }
    • 测试
      ### ngxinx响应: Response code: 200 (OK) <Response body is empty>。匹配0个参数路径
      GET http://localhost:85/dev-api

      ### ngxinx响应:/user
      GET http://localhost:85/dev-api/user


      ### ngxinx响应:/user/books
      GET http://localhost:85/dev-api/user/books


      ### ngxinx响应:/user/book/123
      GET http://localhost:85/dev-api/user/book/123
  7. . 匹配除换行符以外的所有字符
    • nginx配置
      # .测试 匹配换行符以外的其它字符
      location ~ ^/others/(.+)+$ {
          return 200 $1;
      }
    • 测试
      ### ngxinx响应:asdfas2435!@
      GET http://localhost:85/others/asdfas2435!@#

      ### ngxinx响应:404 Not Found。因为包含%0a,%0a为urlencode后的换行符
      GET http://localhost:85/others/asdfas%0a2435!@#
  8. \w 匹配所有数字、字母、下划线
  9. \W \w的补集
    • nginx配置
      # \W测试,匹配非数字、非字母、非下划线
      location ~ ^/W/(\W+)$ {
          return 200 $1;
      }
    • 测试
      ### nginx响应:!@#$%^&*()
      GET http://localhost:85/W/!%40%23%24%25%5E%26*()


      ### nginx响应:404 Not Found。不符合规则,含有数字和字母
      GET http://localhost:85/W/!%40%23%24%25%5E%26*()123abc
  10. \d 匹配所有数字
    • nginx配置
      # \d测试,匹配数字
      location ~ ^/num/(\d+)$ {
          return 200 $1;
      }
    • 测试
      ### nginx响应:123
      GET http://localhost:85/num/123

      ### nginx响应:404 Not Found。不符合规则
      GET http://localhost:85/num/abc
  11. \s 匹配所有空白字符,如空格、制表符、换行符
    • nginx配置
      # \s测试,匹配\n和\t
      location ~ ^/empty((/\s+)+)+$ {
          return 200 $1;
      }
    • 测试
      ### nginx响应:/        %0a:换行符
      GET http://localhost:85/empty/%0a

      ### nginx响应:/ /    %09:制表符
      GET http://localhost:85/empty/%09/%09
  12. 分组
    • 把正则表达式用括号括起来
    • 分组是为了使用分组中捕获的内容。
    • 分组编号:从左至右从1开始号,每遇到一个左括号编号就加1
    • 使用分组:$n表示第n个分组中的内容


阅读原文:原文链接


该文章在 2025/7/1 23:40:17 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved