2016-05-04 90 views
3

我想從Apache + mod_php遷移到Nginx + PHP-FPM。我目前使用mod_rewrite來重寫一些以.php結尾的虛擬URI到實際的PHP腳本。這在使用mod_php時非常有效。但與Nginx + FPM,因爲我們必須使用proxy_pass,這是行不通的。當我添加一個正則表達式位置塊以匹配.php擴展名時,它獲得最高優先級,並且我的重寫規則不適用。在nginx中同時使用重寫和php-fpm .php擴展名

我該如何解決這個問題?

location /test/ { 
    rewrite "^/test/([a-z]+).php$" test.php?q=$1 last; 
} 

location ~ [^/]\.php(/|$) { 
    fastcgi_split_path_info ^(.+?\.php)(/.*)$; 

    set $fastcgi_script_name_custom $fastcgi_script_name; 
    if (!-f $document_root$fastcgi_script_name) { 
     set $fastcgi_script_name_custom "/cms/index.php"; 
    } 

    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php; 
    include fastcgi_params; 
} 

回答

1

您可以將rewrite規則之上的locationserver塊的範圍內。或者,您可以將rewrite規則放入與URI匹配的location塊內。

所以,你可以使用這個:

rewrite "^/test/([a-z]+).php$" /test.php?q=$1 last; 

location/{ ... } 

location ~ [^/]\.php(/|$) { ... } 

或者這樣:

location/{ ... } 

location ~ [^/]\.php(/|$) { 
    rewrite "^/test/([a-z]+).php$" /test.php?q=$1 break; 
    ... 
} 

注意,重寫的URI需要(與Apache的約定)的領先/

有關詳細信息,請參閱this document