user479947
user479947

Reputation:

My nginx + fastcgi configuration downloads php files instead of executing them

I'm using this configuration on a fresh install of php5-fpm and nginx on ubuntu 13.04:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
            try_files $uri $uri/ /index.html;
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
    }

    error_page 404 /404.html;

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-cgi alone:
            fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

But, my web browser is seeing php as text instead of the executed results. Where should I look to troubleshoot?

Upvotes: 11

Views: 20175

Answers (3)

DevOops
DevOops

Reputation: 955

I was able to fix this by updating my nginx vhost by changing

default_type application/octet-stream;

to

default_type text/html;

Upvotes: 0

Mark Stosberg
Mark Stosberg

Reputation: 13381

It sounds like you are getting the wrong Content-Type header set. You can check this with various tools. For example, open the Developer Tools "Network" tab in Chrome, and then request the page. You'll see the "Content Type" returned in one of the columns, and you can click on the request in the left column to see the full Response headers. I suspect you'll find the header being returned is either "text/plain" or "application/octet-stream" instead of text/html, which is probably want you want.

Nginx usually sets a default Content-Type header based on the extension. This is done with the types directive, which I don't see mentioned above, so you may wish to check your settings there to confirm that the php extension is mapped to text/html. Explicitly setting a Content-Typeheader in your application may also help.

Upvotes: 1

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42789

Your php code is being displayed directly because it's not being sent to the php engine, that means the location block is being matched and the php file is being served, but the php file isn't being captured by the php block, so your problem is in the php block.

In that block you have 2 fastcgi_pass, one with a port (9000) and the other to a unix socket, you can't have both together, but since you've tagged your question with fastcgi so I'll assume you are using fastcgi, try commenting this line

#fastcgi_pass unix:/var/run/php5-fpm.sock;

Upvotes: 6

Related Questions