nginx 重写错误问题

nginx rewrite error issue

我的要求 url 是: http://localhost/swim/api/v1/member/xx

nginx 错误日志消息是:

2015/06/30 18:58:57 [error] 42780#0: *9 rewrite or internal redirection cycle while processing "/swim/api/v1/index.php/index.php/index.php/index.php/i    ndex.php/index.php/index.php/index.php/index.php/index.php/index.php/member/xx", client: 127.0.0.1, server: localhost, request: "GET /swim/api/v1/memb    er/xx HTTP/1.1", host: "localhost

nginx.conf 重写规则为:

    location ~ ^/swim/api/(.*) {
      if ($uri ~* "/swim/api/v(\d+)/(\d+)/(.*)$") {
          set $ver ;
          set $con ;
      }
      rewrite ^/swim/api/(.*)$ /swim/api/v$ver/index.php/$con last;
  }

确保您配置了一个 更高优先级 位置,该位置将匹配重写的 url。

例如:

location ~ ^/swim/api/v(\d+)/index.php/(\d+).* {
  echo 'Hello ' ;
  #proxy pass to your real upstream server here
}

location ~ ^/swim/api/v(\d+)/(\d+)/(.*)$ {
  set $ver ;
  set $con ;
  rewrite ^/swim/api/(.*)$ /swim/api/v$ver/index.php/$con last;
}

curl http://127.0.0.1/swim/api/v10/007/bond

输出:

Hello 007