Z
Z
zzmaster2019-07-24 11:47:40
Nginx
zzmaster, 2019-07-24 11:47:40

Why does nginx set status 500 when uploading a file by a script?

I'm trying to send a video file to a PHP script. Here is the source:

header("HTTP/1.1 200 OK");
 	//header("Content-Type: video/$ext"); 
  header("Content-Type: application/octet-stream"); 
  header("Content-length: " . filesize($file)); 
  header('Content-Disposition: attachment; filename="' . $output_name . '"'); 

  $fp = fopen($file, 'r');
  while (1)
  {
    $chunk = fread($fp, 1024*1024);
    if (!$chunk) {
      break;
    }
    print $chunk;
  }
  fclose($fp);

that is directly I put HTTP/1.1 200 OK. And this is how curl traces it
== Info: About to connect() to getfile.qu port 80 (#0)
== Info:   Trying 1.2.3.4...
== Info: Connected to getfile.qu (1.2.3.4) port 80 (#0)
=> Send header, 118 bytes (0x76)
0000: GET /download?id=ae39ea9005c3c9467f0945492771b344 HTTP/1.1
003c: User-Agent: curl/7.29.0
0055: Host: getfile.qu
0067: Accept: */*
0074: 
<= Recv header, 36 bytes (0x24)
0000: HTTP/1.1 500 Internal Server Error
<= Recv header, 15 bytes (0xf)
0000: Server: nginx
<= Recv header, 37 bytes (0x25)
0000: Date: Wed, 24 Jul 2019 08:08:45 GMT
<= Recv header, 40 bytes (0x28)
0000: Content-Type: application/octet-stream
<= Recv header, 24 bytes (0x18)
0000: Content-Length: 383631
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 24 bytes (0x18)
0000: Keep-Alive: timeout=60
<= Recv header, 26 bytes (0x1a)
0000: X-Powered-By: PHP/5.4.16
<= Recv header, 56 bytes (0x38)
0000: Content-Disposition: attachment; filename="lalafa.mp4"
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 16100 bytes (0x3ee4)
0000: ....ftypmp42....mp42isomavc1....free............................

that is, the status is 500 after which the file is still given. Naturally, the browser does not accept it. The commented Content-Type option didn't help. This trouble happens with both mp4 and webm, the latter is not mentioned in the nginx config.
What is happening and how to fix it?
nginx config
# Server globals
user                    nginx;
worker_processes        auto;
worker_rlimit_nofile    65535;
error_log               /var/log/nginx/error.log crit;
pid                     /var/run/nginx.pid;


# Worker config
events {
        worker_connections  1024;
        use                 epoll;
        multi_accept        on;
}


http {
    # Main settings
    sendfile                        on;
    tcp_nopush                      on;
    tcp_nodelay                     on;
    client_header_timeout           1m;
    client_body_timeout             1m;
    client_header_buffer_size       2k;
    client_body_buffer_size         256k;
    client_max_body_size            256m;
    large_client_header_buffers     4   8k;
    send_timeout                    30;
    keepalive_timeout               60 60;
    reset_timedout_connection       on;
    server_tokens                   off;
    server_name_in_redirect         off;
    server_names_hash_max_size      512;
    server_names_hash_bucket_size   512;


    # Log format
    log_format  main    '$remote_addr - $remote_user [$time_local] $request '
                        '"$status" $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  bytes   '$body_bytes_sent';
    #access_log          /var/log/nginx/access.log main;
    access_log off;


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


    # Compression
    gzip                on;
    gzip_comp_level     9;
    gzip_min_length     512;
    gzip_buffers        8 64k;
    gzip_types          text/plain text/css text/javascript text/js text/xml application/json application/javascript application/x-javascript application/xml application/xml+rss application/x-font-ttf image/svg+xml font/opentype;
    gzip_proxied        any;
    gzip_disable        "MSIE [1-6]\.";


    # Proxy settings
    proxy_redirect      off;
    proxy_set_header    Host            $host;
    proxy_set_header    X-Real-IP       $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_header   Set-Cookie;
    proxy_connect_timeout   90;
    proxy_send_timeout  90;
    proxy_read_timeout  90;
    proxy_buffers       32 4k;




 

    # Error pages
    error_page          403          /error/403.html;
    error_page          404          /error/404.html;
    error_page          502 503 504  /error/50x.html;


    # Cache settings
    proxy_cache_path /var/cache/nginx levels=2 keys_zone=cache:10m inactive=60m max_size=1024m;
    proxy_cache_key "$host$request_uri $cookie_user";
    proxy_temp_path  /var/cache/nginx/temp;
    proxy_ignore_headers Expires Cache-Control;
    proxy_cache_use_stale error timeout invalid_header http_502;
    proxy_cache_valid any 1d;


    # Cache bypass
    map $http_cookie $no_cache {
        default 0;
        ~SESS 1;
        ~wordpress_logged_in 1;
    }


    # File cache settings
    open_file_cache          max=10000 inactive=30s;
    open_file_cache_valid    60s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   off;


    # Wildcard include
    include             /etc/nginx/conf.d/*.conf;
}


server {
    listen      1.2.3.4:80;
    server_name getfile.qu www.getfile.qu;
    error_log  /var/log/httpd/domains/getfile.qu.error.log error;

    location / {
        proxy_pass      http://1.2.3.4:8080;
        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|mkv|mov|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {
            root           /home/admin/web/getfile.qu/public_html;
            access_log     /var/log/httpd/domains/getfile.qu.log combined;
            access_log     /var/log/httpd/domains/getfile.qu.bytes bytes;
            expires        max;
            try_files      $uri @fallback;
        }
    }

    location /error/ {
        alias   /home/admin/web/getfile.qu/document_errors/;
    }

    location @fallback {
        proxy_pass      http://1.2.3.4:8080;
    }

    location ~ /\.ht    {return 404;}
    location ~ /\.svn/  {return 404;}
    location ~ /\.git/  {return 404;}
    location ~ /\.hg/   {return 404;}
    location ~ /\.bzr/  {return 404;}

    include /home/admin/conf/web/nginx.getfile.qu.conf*;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-07-24
@zzmaster

header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
header('Content-Type: application/force-download');
header('Content-Description: inline; File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' . $output_name . '";', false);
header('Content-Length: ' . filesize($file));

$upload = new SplFileObject($file, 'rb');
while (!$upload->eof()) {
    echo($upload->fgets());
}

// тут возможно сначала надо закрыть, потом сбросить буфер
header('Connection: close'); 
flush();
exit;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question