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;
}
030
  • 5,901
  • 13
  • 68
  • 110
Shane Grant
  • 305
  • 1
  • 3
  • 12

6 Answers6

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.

rm-vanda
  • 257
  • 8
  • 18
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.

Jonathan
  • 203
  • 2
  • 8
Shane Grant
  • 305
  • 1
  • 3
  • 12
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;
Eli Y
  • 133
  • 4
  • Yes, added it to the original post –  Sep 24 '11 at 23:51
  • okay i added the php mime/type that you should add to the file –  Sep 25 '11 at 00:06
  • Thanks for the time, but after restarting nginx it is still not working. Any other ideas for me to try? –  Sep 25 '11 at 00:30
  • read this guide http://adityo.blog.binusian.org/?p=428 and verify that you made all the steps right –  Sep 25 '11 at 00:36
  • I am using Arch Linux, so that page does not really compare all that much. Here is the page I used: https://wiki.archlinux.org/index.php/NginX#New_method_.28As_of_PHP_5.3.3.29 –  Sep 25 '11 at 01:05
  • i'm gonna ask a stupid one... is php-fpm is running ? (ps -au root| grep php) –  Sep 25 '11 at 01:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3759/discussion-between-elibyy-and-shane-grant) –  Sep 25 '11 at 01:29
  • 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 '14 at 00:17
  • @nnyby lol, I'm having the same problem right now – olfek Jan 01 '21 at 12:43
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/

Dayo
  • 1,826
  • 1
  • 15
  • 12
  • 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 09:39
  • Indeed. Edited the answer to remove incorrect information – Dayo Dec 14 '16 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
George Tasioulis
  • 2,019
  • 2
  • 17
  • 17
  • I just tried this, it still downloads the file. – Shane Grant Sep 25 '11 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](https://bbs.archlinux.org/viewtopic.php?pid=995184) – George Tasioulis Sep 26 '11 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

Sanaulla
  • 101
  • 2