简体   繁体   English

为什么此PHP脚本在apache而不是在NGINX上有效?

[英]Why this PHP script is working on apache and not on NGINX?

I did not get any solution, so trying to ask here, maybe someone can point me, why this script below does not work on NGINX+php5.3.21, but works fine on apache+php5.2.17, maybe its something in php.ini or just am I missing something in nginx.conf? 我没有任何解决方案,因此尝试在这里提出问题,也许有人可以指出我,为什么下面的脚本在NGINX + php5.3.21上不起作用,但是在apache + php5.2.17上却可以正常工作,也许在php.ini中就可以了还是我在nginx.conf中缺少某些内容?

It should force headers to download a file and trace if it was cancelled or finished, then do something. 它应强制标题下载文件并跟踪文件是否已取消或完成,然后执行某些操作。

Just to make it start downloading in nginx I have to uncomment last flush(); 只是为了使其开始在nginx中下载,我必须取消注释last flush();。 but it still does not trace of the file "progress" if its cancelled or done. 但如果取消或完成了文件“ progress”,它仍然不会跟踪。 Apache is fine :(( Thanks! Apache很好:((谢谢!

<?php
$FOLDER_PROGRESS = 'progress/';
$FOLDER_FILES = 'files/';

switch($_REQUEST['acc']){

case 'download':

    $file = trim($_REQUEST['n']);

    if(!file_exists($FOLDER_FILES.$file)){
        echo '<script>alert("Error: The file do not exists.");</script>';
    } else {
        $continue = 'OK';
    }

    if($continue=='OK'){

        $result = fopen($FOLDER_FILES.$file,'r');
        $bytes = filesize($FOLDER_FILES.$file);

        if($bytes<1){
            //header('Location: /');
            echo '<script>alert("Error: The file bytes length is 0.");</script>';
        } else {

            $fp1 = fopen($FOLDER_PROGRESS.trim($_REQUEST['tmp']).'.html','w');
            fwrite($fp1,'Downloading');
            fclose($fp1);

            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-type: application/force-download");
            header("Content-Disposition: attachment; filename=".$file); 
            header("Content-Type: application/octet-stream");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ".$bytes."");

            ignore_user_abort(true);

            while ( !feof($result)) {
                echo fread($result, 4096); //handle
                if (connection_status()!=0 || connection_aborted()){
                    $bytes_transferred = ftell($result); //handle
                    if($bytes_transferred<$bytes) $accion = 'Canceled'; else $accion = 'Done!'; //Done here not run.
                    file_put_contents($FOLDER_PROGRESS.trim($_REQUEST['tmp']).'.html',$accion);

                    sleep(2);
                    file_put_contents($FOLDER_PROGRESS.trim($_REQUEST['tmp']).'.html','Waiting');   
                    flush();
                die;
                } else {
                    $cuenta = ftell($result) / $bytes * 100;
                    if($cuenta>=100){
                        $cuenta = 'Done!';

                    } else {
                        $cuenta = 'Downloaded '.round($cuenta).'%';
                    }
                    file_put_contents($FOLDER_PROGRESS.trim($_REQUEST['tmp']).'.html',$cuenta);
                    if($cuenta=='Done!'){
                        sleep(2);
                        file_put_contents($FOLDER_PROGRESS.trim($_REQUEST['tmp']).'.html','Waiting');
                        flush();
                    }
                }
                //Activate this for delay download.
                //flush();
                //sleep(1);
            }
            fclose($result);

        }

    }

break;

case 'tmp':         

    //create file control

    //$temp = '__'.time();
    $temp = '__'.$_REQUEST['nf'];
    $fp = fopen($FOLDER_PROGRESS.$temp.'.html','w');
    fwrite($fp,'Waiting');
    fclose($fp);                            

    echo trim($temp);

break;                         

} 
?>

php works fine, phpinfo() returns all the positive information. php工作正常,phpinfo()返回所有肯定信息。 nginx.conf nginx.conf

user www-data;
worker_processes 8;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    # multi_accept on;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 0;
    client_max_body_size 2048m;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }

and mysite.conf 和mysite.conf

server {
            listen 80;
            server_name mysite.com www.mysite.com;

            access_log /var/log/nginx/mysite.access_log;
            error_log /var/log/nginx/mysite.error_log;

            root /var/www/www.mysite.com;
            index index.php index.htm index.html;

            location ~ \.php$ {
              fastcgi_pass   127.0.0.1:9000;
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME /var/www/www.mysite.com$fastcgi_script_name;
              include fastcgi_params;
            }

   }         

Solved! 解决了! Everything was behing fastcgi settings! 一切都表现为fastcgi设置! FastCGI does not output buffer to the client until it finishes processing; 在完成处理之前,FastCGI不会将缓冲区输出到客户端。 so something like flush is 'unknown'. 所以类似冲洗的事情是“未知的”。 For others with same problem: you need to set the following settings 对于其他有相同问题的用户:您需要设置以下设置

 fastcgi_keep_conn on;
 fastcgi_read_timeout 600;
 fastcgi_buffer_size 1k;
 fastcgi_buffers 128 1k;
 fastcgi_max_temp_file_size 0;
 gzip off;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM