Nginx循环定向问题??

问题

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条规则的结果。
阅读 2.9k
1 个回答

自己解决了哦,去掉$is_args$args就好了,因为rewrite重写的是$uri:

location ~ ^/(test|admin) {
    rewrite ^(.*) $uri.php;
}

真是不仔细看文档

Syntax:    rewrite regex replacement [flag];
Default:    —
Context:    server, location, if
If the specified regular expression matches a request URI, URI is
changed as specified in the replacement string. The rewrite directives
are executed sequentially in order of their appearance in the
configuration file.
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题