Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to set up nginx as my web server. I want to have image files cached in the memory (RAM) rather then disk. I am serving a small page and want few images always served from RAM. I dont 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 how may I configure nginx for this? I did try few combinations but they didn't work. nginx uses disk all the time to get 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 to many files simultaneously from the disk and OS is not allowing this operation. Can anyone please suggest me a correct configuration?

share|improve this question
    
You're almost certainly guessing wrong. Also, where are you getting the error from? –  womble Jun 11 '12 at 0:28
    
@womble As I comment below problem could be concurrency. There are many threads trying simultaneously before the content is available in memory. Can you please explain why do think I am guessing it wrong? –  Vijayendra Tripathi Jun 11 '12 at 18:17

4 Answers 4

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

share|improve this answer
    
You are right, open file limit was a problem as the limit is 256 on my system. But I still need to figure out how to configure Nginx to read files from memory rather then disk. I dont wish to change my open file limit and rather wish to achieve cache usage for subsequent requests. –  Vijayendra Tripathi Jun 12 '12 at 17:36
2  
"I don't wish to change my open file limit" -- you're doing it wrong. Oh, so very, very wrong. –  womble Jun 13 '12 at 4:31
    
@womble I understand to fix the issue the easiest solution is to change the limit. The reason I dont want to change this limit is to see what best I can do with configuration without changing the limit. I thing I can improve things by changing some other aspects of caching and configuration. And by the way, can you please give you reasons/suggestions while claiming why somebody is wrong. Even in your first comment you did this without giving any reason. This is a place to give professional suggestions, please stop treating it as your facebook wall. –  Vijayendra Tripathi Jun 13 '12 at 17:13

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.

share|improve this answer
    
Yes, I understand that contents are cached in memory by OS. But the assumption is that file is already available in memory. Problem could be concurrency. There are many threads trying simultaneously before the content is available from memory. What do you think? –  Vijayendra Tripathi Jun 11 '12 at 18:10
1  
That's only a problem if the number of requests goes from 0 to 500 in an instant - once the file is read it will be stored there until the memory is needed (least recently used file is removed from cache if memory is full and new file needs to be cached). Odds of that happening in real life are slim to none. If you are sure that's a real risk use the ramdisk solution instead, just make sure to restore the data on ramdisk after each reboot - ramdisk isn't persistent storage by definition. –  c2h5oh Jun 11 '12 at 19:22
    
Ok, I will try ramdisk and see how that works. Thanks for the feedback. –  Vijayendra Tripathi Jun 12 '12 at 17:25

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.
share|improve this answer

À file cached in ram is still à file !

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.