Nginx – PHP file downloaded instead of executed with nginx try_files

nginxPHPphp-fpmUbuntu

I'm using Ubuntu 14.10, nginx 1.6.2, php 5.5.12. I have a site set up like this:

server {
    root /usr/share/nginx/www/oliviataussig;
    index index.php;

    server_name oliviataussig.com www.oliviataussig.com;

    location / {
        try_files $uri $uri/ /index.php =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

When I go to the homepage (index.php), it works fine. However, when I click on any of the links on the page (this is a WordPress-like CMS) I want to fall back to /index.php, and it does that, but index.php gets downloaded instead of executed. How do I fix that?

Best Answer

As Hrvoje suggested, Wordpres ->Nginx codex gives examples on most of possible situations (http://codex.wordpress.org/Nginx)

In your case, the config file should look like below:

server {
    root /usr/share/nginx/www/oliviataussig;
    index index.php;

    server_name oliviataussig.com www.oliviataussig.com;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;


    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}