大网数据广告活动
服务器运维问答

Stop NGINX from locally caching a directory

· 52CE 我爱测

So, the issue I am currently facing is that NGINX on my system keeps caching huge video files in memory when a HTTP request is made. This is causing NGINX to be using 20GB+ of memory and me having to run sync; echo 1 > /proc/sys/…

问题原文

原帖链接 · Steven Seagul · CC BY-SA 4.0

So, the issue I am currently facing is that NGINX on my system keeps caching huge video files in memory when a HTTP request is made. This is causing NGINX to be using 20GB+ of memory and me having to run sync; echo 1 > /proc/sys/vm/drop_caches (clearing pagecache) frequently to stop constant swapping. I have provided below the relevant configuration.

nginx.conf

location /protected/ {
    internal;
    alias /usr/share/nginx/html/videos/;
}

script.php

function Download(){
    global $path, $fname;
    $file = "$path/$fname";
    header("Content-Type: video/mp4");
    header("Content-Length: " . filesize($file)); 
    header('Content-Disposition: attachment; filename="'.$fname.'"');
    header("X-Accel-Redirect: /protected/$fname");
    exit;
}

What I have tried in nginx.conf:

sendfile off;
if_modified_since off;
expires off;
etag off;
proxy_no_cache 1;
proxy_cache_bypass 1;
open_file_cache off;

P.S: I am calculating the memory usage for these files using pcstat and fincore, these MP4 files are being 100% cached when being watched by a user and each are 5GB+ in size.

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

One can disable operating system caching for files served by nginx by using directio <size> setting. <size> is the threshold for file size, bigger files than that will be copied using DMA directly, and the files are not cached.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-06-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。