Latest News : 亮瞎双眼的那些年!

Nginx’s Open file cache

乱弹 admin 772 views 0 comments

When it comes to serving static content, Nginx is quite a beast!
当谈到提供静态内容时,Nginx 真是一个野兽!

You can further improve static content handling by enabling open_file_cache in nginx.
您可以通过在 nginx 中启用 open_file_cache 来进一步改进静态内容处理。

Please note that open_file_cache will cache metadata about files only. Not actual content of files. So performance gain by this kind of cache may not be noticeable.
请注意,open_file_cache 将仅缓存有关文件的元数据。不是文件的实际内容。因此,这种缓存带来的性能提升可能并不明显。

Since this is part of Nginx’s core HTTP module, you no need to worry about setup/installation.
由于这是 Nginx 核心 HTTP 模块的一部分,因此您无需担心设置/安装。

open_file_cache 打开文件缓存

Just open /etc/nginx/nginx.conf 只需打开 /etc/nginx/nginx.conf

Add following lines in HTTP block:
在 HTTP 块中添加以下行:

open_file_cache          max=10000 inactive=5m;
open_file_cache_valid    2m;
open_file_cache_min_uses 1;
open_file_cache_errors   on;

Explanation 解释

If you have way too many files, change max from 10000 to more appropriate value.
如果文件太多,请将 max 从 10000 更改为更合适的值。

If files don’t change much often, or accesses less frequently, you can change inactive duration from 5m to something else. inactive andopen_file_cache_min_uses works together.
如果文件更改频率不高,或者访问频率较低,您可以将非活动持续时间从 5m 更改为其他值。 inactive 和 open_file_cache_min_uses 一起工作。

Above sample tells nginx to cache a file information as long as minimum 2 requests are made during 5m window.
上面的示例告诉 nginx 只要在 5m 窗口内发出至少 2 个请求就缓存文件信息。

open_file_cache_valid tell nginx to check if information it is holding is valid every 2 minutes.
open_file_cache_valid 告诉 nginx 每 2 分钟检查一次它所保存的信息是否有效。

open_file_cache_errors tell nginx to cache errors like 404 (file not found). If you are using nginx as load-balancer, leave this off.
open_file_cache_errors 告诉 nginx 缓存 404(文件未找到)等错误。如果您使用 nginx 作为负载平衡器,请关闭此选项。

For more details, check nginx docs.
有关更多详细信息,请查看 nginx 文档。

Restart 重新开始

Don’t forget to restart nginx.
不要忘记重新启动 nginx。

nginx -t && service nginx restart

Please indicate: 无趣的人生也产生有意思的事件 » Nginx’s Open file cache

Hi, you must log in to comment !