proxy_cache_path /tmp/nginx-uc-cache levels=1:2 keys_zone=uc-cache:10m max_size=10g inactive=300s;

log_format cache '$remote_addr - $host [$time_local] "$request" $status '
	'$body_bytes_sent "$http_referer" '
	'rt=$request_time ut="$upstream_response_time" '
	'cs=$upstream_cache_status';

server {
	listen 80 default_server;
	listen 443 default_server ssl;
	server_name cache;

	root /var/www/cache/;

	access_log /var/log/nginx/access_cache.log cache;
	error_log /var/log/nginx/error_cache.log;

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

	location /crossdomain {
	}

	location /streaming/ {
		# Live; expiration headers are defined by upstream (nginx/wowza)
		rewrite ^/(.*)$ /$1? break;
		proxy_pass http://undefined;
		proxy_cache uc-cache;
		# 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;
	}
	location /resources/ {
		# VOD
		location ~ \.(m3u8|ts|mp4|mp3|webm|oga|ogv|ogg|mov|flv)$ {
			rewrite ^/(.*)$ /$1? break;
			proxy_pass http://undefined;
			proxy_cache uc-cache;
			# 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 7d;
			# instruct browser to cache this
			expires 7d;
			# headers
			proxy_ignore_headers "Cache-Control" "X-Accel-Expires" "Expires";
			add_header X-Cache $upstream_cache_status;
		}
	}
	location / {
		# only urls to video and audio files are allowed, discard any requested path for other urls
		rewrite ^/(.*)$ /index.html? break;
	}
}
