【问题标题】:nginx location zero or one trailing slash at the end of URLURL 末尾的 nginx 位置零或一个斜杠
【发布时间】:2017-08-01 05:57:06
【问题描述】:
location ~ ^/random/?$ {
       try_files $uri $uri/ /api/random.php;
    }

我的 nginx 配置文件中有这个规则,我希望能够像 /random/random/ 这样调用这个 URL

问题是这个规则允许在结尾处使用无限的斜杠,例如 /random///// for ex。也可以。

【问题讨论】:

    标签: regex nginx nginx-location


    【解决方案1】:

    nginx 在匹配 location 指令时使用规范化 URI,因此在测试您的正则表达式时,多个连续的 /s 已经减少为单个 /

    您可以通过测试 $request_uri 变量来拒绝多个连续的 /s,该变量包含原始 URI 和查询字符串(如果有)。例如:

    if ($request_uri ~ //) { return 403; }
    

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 1970-01-01
      • 2012-10-27
      • 2016-06-27
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多