Nginx Open File Cache

Source: Internet
Author: User
Tags sendfile

Nginx's Open_file_cache-related configuration caches meta-information for static files and can significantly improve performance when these static files are accessed frequently.

The cached file meta information includes:

    • FD, the file is opened once, and FD is reserved for use
    • Size
    • Path
    • Last Modified Time
    • ...

Here is a sample configuration:

Open_file_cache max=64 inactive=30d;open_file_cache_min_uses 8;open_file_cache_valid 3m;

max=64Indicates that the maximum number of settings cache files is 64, and Nginx will discard cold data according to the LRU principle after this number.

inactive=30dAnd open_file_cache_min_uses 8 indicates that if a file is accessed less than 8 times within 30 days, it is removed from the cache.

open_file_cache_valid 3mIndicates that the file meta information in the cache is checked every 3 minutes if it is up to date and is not updated.

2 Why cache only file meta information and not cache file contents?

The key to this problem is sendfile (2).

Nginx in serve static files with the Sendfile (2), of course, if you configure sendfile on , Sendfile (2) directly within the kernel space transfer data, compared with read (2)/write (2) omitted two times Kern A copy of the data between El Space and user space. At the same time, the contents of these frequently read static files are cached by the OS to kernel space. Under such a mechanism, we cache the FD and size with the file, and call Sendfile (2) directly.

If you want to cache the content with Nginx, you need to copy the data from kernel space to user space with read (2) Every time the file changes, and then place it in user space, and then copy from user space to kernel each time you answer the request. Space is then written to the socket. There is no merit in this way than in the previous way.

3 What happens when the file changes during the file cache update cycle?

The above mentioned configuration, 30 days no access discarded, every 3 minutes to do the information validity monitoring, we temporarily call 3 minutes cache update cycle. So what happens when the files change within 3 minutes?

3.1 files are deleted

Because Nginx also holds the original file of FD, so you delete this file, the file will not really disappear, the client can still access the file through the original path. Even if you delete and create a new file with the same name, the contents of the original file will be accessed during the current cache update cycle.

3.2 file contents are modified

The contents of the file can be modified in two ways:

The file size does not change or increase
Since Nginx caches the size of the file and uses the size of this cache to call Sendfile (2), the consequences of this situation are:
  • Changes from the beginning of the file to the original size byte can be seen by the client.
  • The content after the original size is not sent by Sendfile (2), so the client does not see this part of the content.
Reduced file size
In
this case, for the same reason, Nginx in the HTTP header to tell the client file size or the original size, and Sendfile (2) can only send real file data, the length is less than the size set in the HTTP header, so the client Wait until you have timed out or Nginx closes the connection after the Epoll_wait timeout.
4 How to set?
    • If your static file content changes frequently and the timeliness requirements are high, you should generally open_file_cache_valid set the smaller, in order to detect and update timely.
    • If the change is quite infrequent, then you can set a larger one and use reload Nginx to force the cache to be updated after the change.
    • For static file access error and access log does not care, you can turn off the improved efficiency.

Nginx Open File Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.