How to configure nginx to serve static contents from RAM?

I want to set up Nginx as my web server. I want to have image files cached in the memory (RAM) rather than disk. I am serving a small page and want a few images always served from RAM. I don't wish to use Varnish (or any other such tools) for this as I believe Nginx has a capability to cache contents into RAM. I am not sure as to how I can configure Nginx for this? I did try a few combinations but they didn't work. Nginx uses disk all the time to get the images.

For example, when I tried Apache benchmark to test with following command:

ab -c 500 -n 1000 http://localhost/banner.jpg

I get the following error:

socket: Too many open files (24)

I guess this means Nginx is trying to open too many files simultaneously from the disk and OS is not allowing this operation. Can anyone please suggest me a correct configuration?


Solution 1:

If it's static content then it will be cached in memory by default (unless there isn't any memory left), just not by nginx, but OS - all that will be left disk-side will be stat().

If you want 100% memory solution you can just configure ramdisk and serve data from there.

Solution 2:

Once the server has read a file from the disk, it will be cached to ram (and will be replaced by diffrent file if you are out of ram), the problem is with your account limit, you can't open so much files (run 'ulimit -a'),

If you want to change this limit - read about /etc/security/limits.conf