12

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?

kasperd
  • 31,086

5 Answers5

9

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.

c2h5oh
  • 1,499
9

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

7

So I know this is really old but, here goes.

  1. Nginx doesn't do memory caching out of the box, you'll want to look at memcache for this, I would recommend the openresty pack for this: http://openresty.org/. What you do get out of the box(as was answered above is page cache)
  2. That error message, I'm almost certain, is from ab not from nginx, nginx errors for file limits look like this "failed (24: Too many open files)". Remember in unix sockets are files too, so for the user you are running ab as, needs to have its ulimit adjusted for that session to run ab. Since you said your limit was 256, you are asking ab to use 500 connections, this is maxing out your limit.
2

A file cached in RAM is still a file !

Try to use Memcached cache module of Nginx instead. But still, 1000 concurrent connections is huge, do you think is your case?

Maxime
  • 215
0

By default nginx uses file system to store it's cache.

You can use the nginx caching alongside the tmpfs (.i.e create a memory backed file system) in order to use memory as your cache storage

check this out for further information:

https://wtit.com/blog/2022/09/27/nginx-caching/