13

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;
    }
    }

}

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;
}
1
  • 1
    Please show us the output of nginx -V and php-fpm -m?
    – quanta
    Sep 26, 2011 at 5:13

6 Answers 6

6

Note to add:

I went through the same deal, but after applying the fix, the ol' ctrl+shift+r wasn't good enough to refresh the browser's cache that the page was a download --

After erasing the cache more thoroughly, it worked as expected.

2
  • 1
    I tried everything and out of nowhere comes your "Note to add" ... Just saved me some agonizing hours. Thanks for the note :)
    – RonzyFonzy
    Sep 4, 2014 at 8:42
  • Great - ! I'm glad it helped ^_^
    – rm-vanda
    Sep 4, 2014 at 14:06
5

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.

3

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;
9
  • Yes, added it to the original post
    – Shane Grant
    Sep 24, 2011 at 23:51
  • okay i added the php mime/type that you should add to the file
    – elibyy
    Sep 25, 2011 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, 2011 at 0:30
  • read this guide adityo.blog.binusian.org/?p=428 and verify that you made all the steps right
    – elibyy
    Sep 25, 2011 at 0:36
  • 1
    After adding the correct MIME type, my PHP files are now displayed in the browser instead of getting downloaded, but they still aren't getting executed.
    – nnyby
    Nov 20, 2014 at 0:17
1

Note that your configuration stands out as being inefficient and potentially insecure. See https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

2
  • 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. Sep 25, 2011 at 9:39
  • Indeed. Edited the answer to remove incorrect information
    – Dayo
    Dec 14, 2016 at 17:45
0

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
2
  • I just tried this, it still downloads the file. Sep 25, 2011 at 15:14
  • 1
    @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 Sep 26, 2011 at 11:03
0

Run the following command:

sudo apt-get install php-gettext
sudo nano /etc/nginx/sites-available/default

Compare the following code with your default file:

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/ =404;
    }

    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;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  • Remove cache from your browser
  • Restart Nginx

sudo systemctl restart nginx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .