My nginx rewrite is downloading the php file

willdanceforfun asked:

I have the following rewrite rule I’m trying:

            location / {
                    index index.php index.html;
                    rewrite  ^/test$  /test.php  break;
            }

And I also have:

            location ~ .php$ {
                    fastcgi_pass    127.0.0.1:9000;
                    include         fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME /home/sites/default$fastcgi_script_name;
                    fastcgi_index   index.php;
            }

Normal php files work ok, but it seems my redirect is just downloading the php file I’m redirecting to, rather than parsing it.

What am I missing? I got the file to load, instead of download by putting the fastcgi stuff in the first location block, but it still didn’t parse as php.

My answer:


Change break to last.

Because the new URL will need to be processed by a different location, you need to have nginx restart processing for the new rewritten URL, which is only done when you use last in the rewrite rule.

See the docs for a fuller explanation.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.