Skip to content
Snippets Groups Projects
mediacache.j2 2.63 KiB
# Cache configuration
#  Maximum retention of 30 days
#  1G of RAM for keys zone (one megabyte can store about 8 thousand keys)
#  wost case is up to 2800 hours of 720p video (50M files) on a 2 TB cache
#  WARNING: keys_zone=name:size size does not accept g units, only m
#  https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_path
proxy_cache_path {{ mediacache_folder }} levels=1:2 keys_zone=mediacache:1000m max_size={{ mediacache_size }}g inactive=30d;

server {
    listen 80 default_server;

    server_name {{ mc_url }};

    location /.well-known/acme-challenge {
        default_type "text/plain";
        root /var/www/letsencrypt;
    }

    location / {
        return 301 https://$host$request_uri;
    }

}

server {
    listen 443 default_server ssl backlog=15000;

    server_name {{ mc_url }};
    root /var/www/mediacache/;

    location /crossdomain {

    }

    location /streaming/ {
        # Live; expiration headers are defined by upstream (nginx/wowza)
        rewrite ^/(.*)$ /$1? break;
        proxy_pass https://{{ ms_url }};
        proxy_cache mediacache;

        # do not consider secure urls as new files
        proxy_cache_key $scheme$proxy_host$uri;

        # only one request at a time will be allowed to populate a new cache element
        proxy_cache_lock on;

        # hide upstream X-Cache header
        proxy_hide_header X-Cache;

        # add own X-Cache header
        add_header X-Cache $upstream_cache_status;

        # rm cookie
        proxy_hide_header       Set-Cookie;
        proxy_ignore_headers    Set-Cookie;

    }

    location /resources/ {
        # VOD
        location ~ \.(m3u8|ts|mp4|mp3|oga|ogv|ogg|mov|flv)$ {
            rewrite ^/(.*)$ /$1? break;
            proxy_pass https://{{ ms_url }};
            proxy_cache mediacache;

            # do not consider secure urls as new files
            proxy_cache_key $scheme$proxy_host$uri;

            # only one request at a time will be allowed to populate a new cache element
            proxy_cache_lock on;

            # how long should the data be kept in the cache
            proxy_cache_valid 200 30d;

            # instruct browser to cache this
            expires 7d;

            # headers
            proxy_ignore_headers "Cache-Control" "X-Accel-Expires" "Expires";
            add_header X-Cache $upstream_cache_status;

            # rm cookie
            proxy_hide_header       Set-Cookie;
            proxy_ignore_headers    Set-Cookie;

        }

    }

    location / {
        # only urls to video and audio files are allowed, discard any requested path for other urls
        rewrite ^/(.*)$ /index.html? break;

    }

}