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

I have seen the same problem, and many resolutions for it pointing to adding the type handler, but that was for Apache.

I am using Arch Linux, Nginx, PHP and PHP-FMP and I cannot figure out why php files are downloading instead of running.

Here is my nginx.conf:

#user http;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            try_files $uri =404;
        root           html;
            fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    server {
    listen  80;
    server_name arch;
    access_log  /srv/http/arch/logs/access.log;
    error_log   /srv/http/arch/logs/error.log;
    location / {
        root    /srv/http/arch/httpdocs;
        index   index.html;
    }
    }

}

Thank you

Edit to Add mime.types file:

types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/x-javascript              js;
    application/atom+xml                  atom;
    application/rss+xml                   rss;

    text/mathml                           mml;
    text/plain                            txt;
    text/vnd.sun.j2me.app-descriptor      jad;
    text/vnd.wap.wml                      wml;
    text/x-component                      htc;

    image/png                             png;
    image/tiff                            tif tiff;
    image/vnd.wap.wbmp                    wbmp;
    image/x-icon                          ico;
    image/x-jng                           jng;
    image/x-ms-bmp                        bmp;
    image/svg+xml                         svg;

    application/java-archive              jar war ear;
    application/mac-binhex40              hqx;
    application/msword                    doc;
    application/pdf                       pdf;
    application/postscript                ps eps ai;
    application/rtf                       rtf;
    application/vnd.ms-excel              xls;
    application/vnd.ms-powerpoint         ppt;
    application/vnd.wap.wmlc              wmlc;
    application/vnd.google-earth.kml+xml  kml;
    application/vnd.google-earth.kmz      kmz;
    application/x-7z-compressed           7z;
    application/x-cocoa                   cco;
    application/x-java-archive-diff       jardiff;
    application/x-java-jnlp-file          jnlp;
    application/x-makeself                run;
    application/x-perl                    pl pm;
    application/x-pilot                   prc pdb;
    application/x-rar-compressed          rar;
    application/x-redhat-package-manager  rpm;
    application/x-sea                     sea;
    application/x-shockwave-flash         swf;
    application/x-stuffit                 sit;
    application/x-tcl                     tcl tk;
    application/x-x509-ca-cert            der pem crt;
    application/x-xpinstall               xpi;
    application/xhtml+xml                 xhtml;
    application/zip                       zip;

    application/octet-stream              bin exe dll;
    application/octet-stream              deb;
    application/octet-stream              dmg;
    application/octet-stream              eot;
    application/octet-stream              iso img;
    application/octet-stream              msi msp msm;

    audio/midi                            mid midi kar;
    audio/mpeg                            mp3;
    audio/ogg                             ogg;
    audio/x-realaudio                     ra;

    video/3gpp                            3gpp 3gp;
    video/mpeg                            mpeg mpg;
    video/quicktime                       mov;
    video/x-flv                           flv;
    video/x-mng                           mng;
    video/x-ms-asf                        asx asf;
    video/x-ms-wmv                        wmv;
    video/x-msvideo                       avi;
}
share|improve this question
    
Please show us the output of nginx -V and php-fpm -m? –  quanta Sep 26 '11 at 5:13
add comment

migrated from stackoverflow.com Sep 25 '11 at 3:02

This question came from our site for professional and enthusiast programmers.

5 Answers

up vote 3 down vote accepted

I was able to get this issue fixed, but the problem I had seems to have been related to the fact that I still had Apache setup on the server, and some part of Apache was conflicting with Nginx's ability to server php pages, even though Apache was not running.

If you run into the same issue try to remove Apache, PHP and all related modules, and re-install just what you need.

share|improve this answer
add comment

is this file contains anything ?

(include mime.types;)

because if not so all of your files getting the mime/type of a download file

(default_type application/octet-stream;)

so this is your problem because php is not on the list

solution : add this row to that file and restart ngnix

text/php                             php php5;
share|improve this answer
    
Yes, added it to the original post –  Shane Grant Sep 24 '11 at 23:51
    
okay i added the php mime/type that you should add to the file –  elibyy Sep 25 '11 at 0:06
    
Thanks for the time, but after restarting nginx it is still not working. Any other ideas for me to try? –  Shane Grant Sep 25 '11 at 0:30
    
read this guide adityo.blog.binusian.org/?p=428 and verify that you made all the steps right –  elibyy Sep 25 '11 at 0:36
    
I am using Arch Linux, so that page does not really compare all that much. Here is the page I used: wiki.archlinux.org/index.php/… –  Shane Grant Sep 25 '11 at 1:05
show 2 more comments

Remove the try_files directive from the php location. With it in place, Nginx will hit that line and go look for the file. If it finds it, it will serve it like any other text file. So remove that line so that php file can be processed by fastcgi.

Note however that your configuration stands out as being inefficient and potentially insecure. See http://wiki.nginx.org/Pitfalls

share|improve this answer
1  
Hi, This is not true. Try_files just sets the internal URI pointer and then continues processing the location block. When you send to PHP in the same location it's perfectly fine. –  Martin Fjordvald Sep 25 '11 at 9:39
add comment

My location ~ .php looks like this. Why don't you try it?

location ~ \.php {
        try_files $uri =404; #This line closes a big security hole
                             #see: http://forum.nginx.org/read.php?2,88845,page=3
        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;

        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;

        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;

        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $server_name;

        fastcgi_pass 127.0.0.1:9000;
}

Also go to /etc/php/php-fpm.conf and try:

listen = 127.0.0.1:9000
;listen = /var/run/php-fpm/php-fpm.sock
share|improve this answer
    
I just tried this, it still downloads the file. –  Shane Grant Sep 25 '11 at 15:14
    
@ShaneGrant A user at the arch bbs has the same problem with you, and solved it by removing apache and php (from a previous setup) and reinstalling nginx and php again. Check this out: link –  George Tasioulis Sep 26 '11 at 11:03
add comment

OK crazy note to add to this confusing ordeal:

I had the same problem, couldn't solve it, so I decided to burn it and start from scratch.

Deleted the files, the NGINX config -- I went so far that I even deleted the DNS record on the nameserver

Yet, it still downloaded the index.php when I went to the site

Confused as hell, I opened a different browser, and got the result I expected: Could not connect

So, perhaps there is something in the browser-cache that allows this problem to persist....

share|improve this answer
add comment

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.