# Nginx uses high ram

**URL:** <https://www.ruby-forum.com/t/nginx-uses-high-ram/201651>\
**Category:** NGINX\
**Created:** [January 12, 2011, 11:33am UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651 "2011-01-12T11:33:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 12, 2011, 11:33am UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651/1 "2011-01-12T11:33:00Z")

</div>

nginx uses high ram

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

waiting for response 🙂

Posted at Nginx Forum:

> **[nginx uses high ram](https://forum.nginx.org/read.php?2,165534,165534#msg-165534)**
>
> nginx uses high ram on my server when user download any files. php read file and echo on the browser but nginx uses high ram any one know why nginx uses high ram is there any solution to minimize ram usage waiting for response :)

---

<div class="post-metadata">

**Author:** ![khizar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khizar](https://www.ruby-forum.com/u/khizar)\
**Post date:** [January 12, 2011, 1:02pm UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651/2 "2011-01-12T13:02:00Z")

</div>

Hi,

> 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  
http\_send\_file().

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,  
John

[1] [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/XSendfile)

---

<div class="post-metadata">

**Author:** ![khizar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khizar](https://www.ruby-forum.com/u/khizar)\
**Post date:** [January 12, 2011, 1:15pm UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651/3 "2011-01-12T13:15:50Z")

</div>

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

```auto

<?php
header('some require header');
$fp="filename.file";
while(!feof($fp))
echo fread($fp,8192);
fclose($fp);
?>

```

Posted at Nginx Forum:

> **[Re: nginx uses high ram](https://forum.nginx.org/read.php?2,165534,165546#msg-165546)**
>
> nginx uses high ram on my server when user download any files. php read file and echo on the browser but nginx uses high ram any one know why nginx uses high ram is there any solution to minimize ram usage waiting for response :)

---

<div class="post-metadata">

**Author:** ![khizar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khizar](https://www.ruby-forum.com/u/khizar)\
**Post date:** [January 12, 2011, 1:41pm UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651/4 "2011-01-12T13:41:38Z")

</div>

> sorry in above code  
> $fp=fopen(‘filename.file’,‘rb’);

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

---

<div class="post-metadata">

**Author:** ![khizar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khizar](https://www.ruby-forum.com/u/khizar)\
**Post date:** [January 12, 2011, 1:17pm UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651/5 "2011-01-12T13:17:44Z")

</div>

## khizar Wrote:

> echo fread($fp,8192);  
> fclose($fp);  
> ?\>

sorry in above code  
$fp=fopen(‘filename.file’,‘rb’);

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,165534,165547#msg-165547](http://forum.nginx.org/read.php?2,165534,165547#msg-165547)

---

<div class="post-metadata">

**Author:** ![khizar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khizar](https://www.ruby-forum.com/u/khizar)\
**Post date:** [January 12, 2011, 2:08pm UTC](https://www.ruby-forum.com/t/nginx-uses-high-ram/201651/6 "2011-01-12T14:08:10Z")

</div>

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  
wrong:

- 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  
above!

location /downloads/ {  
internal;  
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"); ?\>

Regards,  
John
