【问题标题】:Nginx regexp all paths expectNginx 正则表达式的所有路径,除了
【发布时间】:2019-04-11 03:17:56
【问题描述】:

我有 3 种路径 /api//some_path_here

如果用户请求的页面路径为空 (/),我想重定向到登录。

所以我有一个像这样的配置文件

 location /api {
          try_files $uri $uri/ /index.php?$args;
        }


        location /[0-9a-z]  {
          try_files $uri $uri/ /index.html;
          root /var/www/cabinet/client/dist;
        }

        location / {
          return 301 https://my_domain.com/login;
        }

但是当尝试请求 https://my_domain.com/ 时,它会重定向到 https://my_domain.com/login 并出现错误 ERR_TOO_MANY_REDIRECTS

我该如何解决这个错误?

【问题讨论】:

  • 哪个给定位置应该处理/login
  • 包含正则表达式的位置
  • 那么你必须表明你希望它被视为正则表达式,在表达式前面添加~*。
  • 同样的结果。
  • 您的正则表达式当前匹配斜线和小写字母或数字。

标签: nginx redirect nginx-location


【解决方案1】:

这是一个解决方案

location /api {
          try_files $uri $uri/ /index.php?$args;
        }


        location / {
          try_files $uri $uri/ /index.html;
          root /var/www/cabinet/client/dist;
        }

        location = / {
          return 301 $scheme://$server_name/login/;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2022-11-22
    • 2021-01-15
    • 2018-01-08
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    相关资源
    最近更新 更多