账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Nginx循环定向问题??
    36
    0

    问题

    nginx.conf

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    
    location ~ ^/(test|admin) {
        rewrite ^(.*) $uri.php$is_args$args;
    }

    想把account|admin开头的请求后面加个.php后缀(兼容老系统),问题是访问这些目录时出现

    500 Internal Server Error,看日志是循环定向问题
    路径/test/game.php应该会被上一个location处理吧,看来学艺不精。。。

    Nginx官方location优先顺序

    Directives with the = prefix that match the query exactly. If found, searching stops.
    All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
    Regular expressions, in order of definition in the configuration file.
    If #3 yielded a match, that result is used. Else the match from #2 is used.
    =前缀的指令严格匹配这个查询。如果找到,停止搜索。
    所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
    正则表达式,在配置文件中定义的顺序。
    如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。
    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 火木耳 普通会员 1楼

      Nginx循环定向,也称为循环重定向或循环跳转,通常发生在Nginx配置中设置的重定向规则之间形成了一个无限循环。例如,当请求A被重定向到B,然后B又被重定向回A,就会导致这种情况。

      例如:

      ```nginx server { listen 80; server_name example.com;

      location / {
          return 301 https://www.example.com$request_uri;
      }
      

      }

      server { listen 443 ssl; server_name www.example.com;

      location / {
          return 301 http://example.com$request_uri;
      }
      

      } ```

      在这个例子中,对http://example.com的请求会被重定向到https://www.example.com,然后这个请求又会被重定向回http://example.com,从而形成一个无尽的循环。

      解决方法是确保你的重定向规则不会互相冲突,形成闭环,并且在适当的地方终止重定向。例如,你可能只想将所有HTTP请求重定向到HTTPS,那么上述配置可以修改为:

      ```nginx server { listen 80; server_name example.com www.example.com;

      return 301 https://$host$request_uri;
      

      }

      server { listen 443 ssl; server_name example.com www.example.com;

      # 此处是HTTPS服务器的正常配置...
      

      } ```

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部