Rewrite before regex location

Joyce Babu joyce at
Wed May 4 21:26:27 UTC 2016

I am trying to migrate to Nginx + PHP-FPM from Apache + mod_php. I am
currently using mod_rewrite to rewrite some virtual URIs ending in .php to
actual PHP scripts. This works perfectly when using mod_php. But with
with Nginx
+ FPM, since we have to use proxy_pass, this is not working. When I add a
regex location block to match .php extension, it gets higher precedence,
and my rewrite rules are not applied.

How can I resolve this?

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_index index.php;
    include fastcgi_params;}

Joyce Babu

NB: This is a sample configuration. There are several such rewrite rules,
hence I cannot use a exact match or regular expression location block to
override the php proxy location block.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list