Search code examples
.htaccessmod-rewritenginxwinginx

Nginx Rewrite and FastCGI - Php files get downloaded


I've added this block of directives to my Nginx installation

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    # With php5-cgi alone:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    # With php5-fpm:
    include fastcgi.conf;
    fastcgi_index index.php;
}

If i contact http://myserverip/script.php everything goes fine. I need to use the rewrite engine to rewrite some URLs, after this block of directives i've added many other blocks like this:

location = /sentmessages {
    rewrite ^(.*)$ /sent_messages.php break;
}

(i've used winginx converter for .htaccess rules)

If i contact http://myserverip/sentmessages rewrite goes well, but the PHP script gets downloaded instead of being passed to FastCGI. I don't know how to fix this(tried to change the order of the directives without success.)

How to fix? thanks.


Solution

  • After searching Stackoverflow, solution was to use the "last" at the end of the rewrite rule.

    location = /sentmessages {
        rewrite ^(.*)$ /sent_messages.php last;
    }