【问题标题】:Nginx : to show an error page for all the paths excluding a few , for 400 status codeNginx:显示除少数路径外的所有路径的错误页面,状态码为 400
【发布时间】:2017-06-20 20:34:54
【问题描述】:

我需要在 url 中进行模式匹配,它排除了一些文件并在获取错误代码 400 时显示错误页面

例如:我有 URL /test/v1//test/v2/。我需要为所有/test/x 显示一个自定义错误页面(状态400),其中x 可以是任何东西,除了/test/v1//test/v2/

那么我在 nginx.conf 文件中的 location 指令中的正则表达式应该是什么我在下面尝试但失败了。

location ~* /test(?!\/(v1|v2)) {
    proxy_intercept_errors on;
}

error_page  400 /400.html;

【问题讨论】:

  • 这是怎么失败的?你的意思是/test/v1/more/应该被允许?

标签: regex nginx nginx-location


【解决方案1】:

这是一个建议:

\/test\/(v1|v2)\/?

它允许/test/v1//test/v2/(没有最后的斜线也可以)。
/test/nope//nope 时失败。

Test it on regex101

【讨论】:

    【解决方案2】:

    允许 test/(v1|v2)/,并禁用所有其余的:

    location ~* /test(\/(v1|v2)\/) {
        try_files $uri $uri/ =404;
        break;
    }
    
    
    location / {
         return 400 "bad request";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 2021-03-24
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多