Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have recently set up a nginx server with PHP running as FastCGI process. The server is running with HTML files however PHP files are downloading instead of displaying and PHP code is not processed.

This is what I have in nginx.conf:

server {
    listen 80;
    server_name pubserver;
    location ~ \.php$ {
        root          /usr/share/nginx/html;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        include       fastcgi_params;
    }
}

The command netstat -tulpn | grep :9000 displays the following which indicates php-fastcgi is running and listening on port 9000:

tcp     0     0 127.0.0.1:9000       0.0.0.0:*      LISTEN      2663/php-cgi

If it's if any importance my server is running on CentOS 6 and I installed nginx and PHP using the repositories from The Fedora Project.

share|improve this question
    
Are you sure that the correct virtual host is being used? Do you have other server blocks defined? Are you accessing the site as http://pubserver? –  mgorven Feb 11 '13 at 18:00

5 Answers 5

PHP short tags are not the issue. Even with a PHP file that contains only HTML code it does the same.

I included a location for index files changed all paths in nginx.conf to absolute paths, now the file looks like this:

server {
    listen 80;
    server_name  onedesigns;
    root         /usr/share/nginx/html;
    location / {
        index  index.html index.htm index.php;
    }
    location ~ \.php$ {
        include        /etc/nginx/fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    }
}
share|improve this answer
    
I have tried al possible combinations found on the internet and still the PHP files are downloading. My guess is FastCGI is not compiling the files and it's sending them as binary. Don't know if there's any way to check this though. –  Daniel Jun 1 '12 at 18:31

Random guess but do you use short opening tags and have it disabled in your php.ini file?

If your code uses <? instead of <?phpthen try changing that.

If not that then check the headers returned and see if it has an X-Powered-By header saying PHP and report back.

Edit: You added an answer instead of a reply here so I never got a notice. You managed to trip up yourself, though. The code is most likely being processed but you just haven't checked the content of the downloaded file.

Now here's why that happens. In your Nginx config you have the following default_type octet-stream and your PHP does not return a content type, thus nginx uses octet-stream. Merely delete that directive and it should work.

share|improve this answer

As @Martin pointed out, the default data type being received as header by the browser is not text/html or something that the browser can display. But the data type setting is with php, because it's a FastCGI script. If the script doesn't output a content-type header, nginx would return octet-stream.

You should be able to execute the script as:

curl http://server/script.php

This will work, because curl accepts /, while browsers like chrome, firefox, etc. have different rules for different content types.

You might want to review your php.ini to check the default content type.

Also, you can retrieve header information using

curl -I http://server/script.php
share|improve this answer

Try changing the location :

location ~ ^(.+\.php)(.*)$ {
share|improve this answer

Try with this:

http{
 # .....
 index              index.php index.html index.htm;
 # ....
}

server {
    listen       80;
    server_name pubserver;
    root html;

    location ~ \.php$ {
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass php;
    }
}

Hope this help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.