Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache a static file in memory forever on Nginx?

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?

like image 478
Golo Roden Avatar asked Aug 07 '16 10:08

Golo Roden


People also ask

How long will nginx cache CUR files?

It caches RSS and ATOM feeds for 1 hour, Javascript and CSS files for 1 year, and other static files ( images and media ) for 1 month. The caches are all set to "public", so that any system can cache them. Setting them to private would limit them to being cached by private caches, such as our browser.

Where does nginx store cache?

/var/cache/nginx – the path to the local disk directory for the cache. levels – defines the hierarchy levels of a cache, it sets up a two-level directory hierarchy under /var/cache/nginx.


2 Answers

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

like image 200
Suresh Prajapati Avatar answered Sep 21 '22 18:09

Suresh Prajapati


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.

like image 36
VBart Avatar answered Sep 19 '22 18:09

VBart