Nginx uses high ram

nginx uses high ram

on my server when user download any files. php read file and echo on the
but nginx uses high ram
any one know why nginx uses high ram
is there any solution to minimize ram usage

waiting for response :slight_smile:

Posted at Nginx Forum:


php read file and echo on the browser
but nginx uses high ram

since we have no detail on how you measured this and did not show us the
PHP code (looking at crystal ball), I suspect it’s the common problem:

If you use readfile(), make sure to disable output buffering or PHP will
happily read large files into memory until it hits the memory limit. As
an alternative, implement a chunking readfile() or use something like

The most efficient way however is to use X-Accel-Redirect [1] (also
known as X-SendFile). Skip piping the file through PHP and let nginx
serve it directly!

Hope this helps,

[1] XSendfile | NGINX

here the situation is i have to read file using php
m using the below code

header('some require header');
echo fread($fp,8192);

Posted at Nginx Forum:

sorry in above code

So it’s problems of your code, as mentioned before. Reread mail from
John until full understanding.

khizar Wrote:

echo fread($fp,8192);

sorry in above code

Posted at Nginx Forum:

As mentioned before, you have two options to handle application
controlled downloads:

*** 1 ***
Fix your PHP settings and code.
This is more painful than you might think(!), and many people get it

  • disable PHP output buffering for the script (often enabled by
    default), either via php.ini, htaccess, within the script, or
    alternatively go ahead and explicitely flush buffers within the loop.

  • make sure to disable PHP zlib output compression (similar effect)

  • make sure to configure PHP max_execution_time and similar timeouts

  • configure nginx settings for the download script location:
    gzip*, fastcgi_buffer*, proxy_buffer* and various timeouts come to mind,
    depending on how you communicate with PHP.

*** 2 ***
As suggested, stop useless piping of data through the PHP process and
make use of nginx’s X-Accel-Redirect. Compare this to all of the stuff

location /downloads/ {
alias /path/to/the/dir/containing/filename.file/;

<?php header("Content-Type: application/force-download"); // or correct one header("X-Accel-Redirect: /downloads/filename.file"); ?>