nginx serves some (but not all) php files as downloads

I’m installing WordPress on my Ubuntu server running nginx. The installation went pretty smoothly (following Installing LEMP and Installing WordPress tutorials – took me through mysql, php5-fpm and wordpress setup), and seems to mostly work. I can view the WordPress admin page, create blog posts and even view those posts. But when I try to access the homepage of my blog (eg. index.php), nginx serves the file as a download rather than executing it. I have already tried the configurations in Nginx serves .php files as downloads, instead of executing them to no avail.

Here is my virtual server file:

Read More
server {
    listen 80;
    server_name my.domain.com;

    root /my/wordpress/home/dir;
    index index.php index.html index.htm;

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

        rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ redirect;
        if (-e $request_filename) {
            rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 break;
        }
        rewrite ^/([_0-9a-zA-Z-]+/)?(.*.php)$ /$2 break;
        rewrite ^(.*)$ /index.php break;
    }
}

And nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

I removed commented lines for clarity. sudo nginx -t reports no errors.

How can I get index.php to execute instead of being served as a download? Thanks for your help.

Edit: Looks like it was a browser caching issue. I tried deleting cached data from the past 24 hours but it didn’t change anything. After deleting everything it now loads properly instead of downloading. It also loads on other browsers/computers just fine.

Related posts

1 comment

  1. For running WordPress behind Nginx, this works for me:

        location / {
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }
    
        location /wp-content/updraft {
          deny all;
        }
    
        error_page 404 /404.html;
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }
    
        location ~ .php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    

    I also recommend reviewing the sample files from the WordPress-Nginx project. In particular, you may also be interested the globals/restrictions.conf file, which hardens your installation somewhat. If you have several WordPress sites on the same server, they can all share these “global” configuration files, making your life as a WordPress admin easier.

Comments are closed.