鍍金池/ 問答/Linux  網(wǎng)絡(luò)安全/ Nginx循環(huán)定向問題??

Nginx循環(huán)定向問題??

問題

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;
}

想把a(bǔ)ccount|admin開頭的請求后面加個(gè).php后綴(兼容老系統(tǒng)),問題是訪問這些目錄時(shí)出現(xiàn)

500 Internal Server Error,看日志是循環(huán)定向問題
路徑/test/game.php應(yīng)該會被上一個(gè)location處理吧,看來學(xué)藝不精。。。

Nginx官方location優(yōu)先順序

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.
=前綴的指令嚴(yán)格匹配這個(gè)查詢。如果找到,停止搜索。
所有剩下的常規(guī)字符串,最長的匹配。如果這個(gè)匹配使用^?前綴,搜索停止。
正則表達(dá)式,在配置文件中定義的順序。
如果第3條規(guī)則產(chǎn)生匹配的話,結(jié)果被使用。否則,使用第2條規(guī)則的結(jié)果。
回答
編輯回答
別硬撐

自己解決了哦,去掉$is_args$args就好了,因?yàn)閞ewrite重寫的是$uri:

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

真是不仔細(xì)看文檔

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.
2017年2月25日 06:41