22

I have Nginx running in a Docker container, and it serves some static files. The files will never change at runtime - if they actually do change, the container will be stopped, the image will be rebuilt, and a new container will be started.

So, to improve performance, it would be perfect if Nginx would read the static files only one single time from disk and then server it from memory forever. I have found some configuration options to configure caching, but at least from what I have seen none of them provided this "forever" behavior that I'm looking for.

Is this possible at all? If so, how do I need to configure Nginx to achieve this?

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 1
    I guess yo tried this SO answer already? http://stackoverflow.com/a/20026096/90800 – Alexander Zeitler Aug 09 '16 at 11:10
  • No, but this was *exactly* what I have been looking for (and what I was not able to find). So, if you turn your comment into an answer, I will happily accept it. Thanks :-)! – Golo Roden Aug 09 '16 at 11:16
  • It's me again: Sorry, but I'm not that sure any more whether it was actually what I have been looking for: If I get this correct, this enables caching on the client-side, but I am looking for caching on the server-side (I don't want Nginx to touch the file system more than once ever). Is this achieved by this, or am I missing something here? – Golo Roden Aug 09 '16 at 11:22
  • I'm not sure. What if you place your file in a RAM disk and point nginx to this particular "volume" ? – Alexander Zeitler Aug 09 '16 at 11:27
  • Yes, that would probably be doable. The question is: Is there an option already in Nginx that I can use to achieve the same thing? – Golo Roden Aug 09 '16 at 11:32
  • It looks like nginx does in memory caching by default as the answers from here suggest: http://serverfault.com/questions/397378/how-to-configure-nginx-to-serve-static-contents-from-ram – Alexander Zeitler Aug 09 '16 at 11:35
  • Hmmmm… maybe ;-). One answer says it doesn't do that :-( – Golo Roden Aug 09 '16 at 12:29

2 Answers2

19

Nginx as an HTTP server cannot do memory-caching of static files or pages.

Nginx is a capable and mature HTTP and proxy server. But there seems to be some confusion about its capabilities with respect to caching. Nginx server cannot memory-cache files when running as a pure Web server. And…wait what!? Let me rephrase: Nginx HTTP server cannot memory-cache files or pages.

Possible Workaround

The Nginx community’s answer is: no problem, let the OS do memory caching for you! The OS is written by smart people (true) and knows the what, when, where, and how of caching (a mere opinion). So, they say, cat your static files to /dev/null periodically and just trust it to cache your stuff for you! For those who are wondering and pondering, what’s the cat /dev/null reference has to do with caching? Read on to find out more (hint: don’t do it!).

How does it work?

It turns out that Linux is a fine-tuned beast that’s hawk-eyed about what goes in and out of its cache thingy. That cache thingy is called the Page Cache. The Page Cache is the memory store where frequently-accessed files are partially or entirely stored so they’re quickly accessible. The kernel is responsible for keeping track of files that are cached in memory, when they need to be updated, or when they need to be evicted. The more free RAM that’s available the larger the page cache the “better” the caching.

Please refer below diagram for more depth explanation:

enter image description here

Suresh Prajapati
  • 3,991
  • 5
  • 26
  • 38
15

Operating system does in memory caching by default. It's called page cache. In addition, you can enable sendfile to avoid copying data between kernel space and user space.

VBart
  • 14,714
  • 4
  • 45
  • 49