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

Nginx automatically deletes cache which is set to be valid longer?

· 52CE 我爱测

I set up a Nginx reverse proxy server serving mp4 files hosted on another server. Everything is working fine now except the cache. Although I have proxy_cache_valid set to 1 day ( proxy_cache_valid any 1d ), the cache will always…

问题原文

原帖链接 · David Tran · CC BY-SA 3.0

I set up a Nginx reverse proxy server serving mp4 files hosted on another server. Everything is working fine now except the cache. Although I have proxy_cache_valid set to 1 day (proxy_cache_valid any 1d), the cache will always be automatically deleted after a short amount of time (5-10 mins I think). My file sizes range from 200 - 1500MB (700MB in average).

I couldn't figure out what's wrong with the configurations. Anything might help.

Here are the configurations:

worker_processes  auto;

worker_rlimit_nofile 100000;

events {
    worker_connections  5000;
    multi_accept on;
    use epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;

    keepalive_timeout  10;
    keepalive_requests 1024;
    client_body_timeout 12;
    client_header_timeout 12;
    send_timeout 10;

    proxy_cache_path /tmp/mycache keys_zone=mycache:10m use_temp_path=off;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    server {
        listen       80;
        server_name  localhost;

    access_log off;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    open_file_cache max=10000 inactive=30s;
    open_file_cache_valid    60s;
    open_file_cache_min_uses 5;
    open_file_cache_errors   on;

    client_body_buffer_size 16K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

        location / {
        proxy_cache mycache;
        proxy_max_temp_file_size 1924m;
        slice              100m;
        proxy_cache_key    $host$uri$slice_range;
        proxy_set_header   Range $slice_range;
        proxy_http_version 1.1;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 
        proxy_cache_valid  any 1d;
        limit_conn addr 5;
        proxy_pass   http://domain2.com/;
        secure_link $arg_md5,$arg_expires;
        secure_link_md5 "secret$secure_link_expires$uri";

        if ($secure_link = "") { return 403; }
                if ($secure_link = "0") { return 410; }
        }

    }

}

已采纳解答

原帖链接 · Jens Bradler · CC BY-SA 3.0

Your issue can be solved by modifying the proxy_cache_path setting in your Nginx config. Here you define your store to hold your cache objects. There is a inactive=time option would fix that:

Cached data that are not accessed during the time specified by the inactive parameter get removed from the cache regardless of their freshness. By default, inactive is set to 10 minutes.

In your example I would extend the minimum time to 2 days:

proxy_cache_path /tmp/mycache keys_zone=mycache:10m use_temp_path=off inactive=2d;

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

资料来源与适用范围

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

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

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