From 773b073631b976db96f99a5125ed326fc71244a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florent=20Thi=C3=A9ry?= <florent.thiery@ubicast.eu>
Date: Thu, 9 Feb 2017 11:12:45 +0100
Subject: [PATCH] optimize cache conf, fixes #20420

---
 2.Common_services/5.Nginx/vhost_cache.conf    | 41 ++++++++++---------
 .../5.Nginx/vhost_mediaserver-msuser.conf     | 32 +++++----------
 2 files changed, 32 insertions(+), 41 deletions(-)

diff --git a/2.Common_services/5.Nginx/vhost_cache.conf b/2.Common_services/5.Nginx/vhost_cache.conf
index 0339e19a..906c4175 100644
--- a/2.Common_services/5.Nginx/vhost_cache.conf
+++ b/2.Common_services/5.Nginx/vhost_cache.conf
@@ -23,19 +23,21 @@ server {
 			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;
-			proxy_cache_min_uses 1;
-			proxy_cache_use_stale updating;
-			# how long should the data be kept in the cache
-			proxy_cache_valid 200 1s;
-			# instruct browser never to cache this
-			expires -1;
-			# headers
+			# ignore upstream headers
+            # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
 			proxy_ignore_headers Cache-Control;
-			proxy_ignore_headers Set-Cookie;
 			proxy_ignore_headers X-Accel-Expires;
 			proxy_ignore_headers Expires;
-			proxy_hide_header Pragma;
+            # hide upstream X-Cache header
+			proxy_hide_header X-Cache;
+            # add own X-Cache header
 			add_header X-Cache $upstream_cache_status;
+
+            # m3u8-specific
+			# how long should the data be kept in this cache
+			proxy_cache_valid 200 2s;
+			# instruct browsers never to cache m3u8
+			expires -1;
 		}
 		location ~ \.ts$ {
 			rewrite ^/(.*)$ /$1? break;
@@ -45,19 +47,21 @@ server {
 			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;
-			proxy_cache_min_uses 1;
-			proxy_cache_use_stale updating;
-			# how long should the data be kept in the cache
-			proxy_cache_valid 200 10s;
-			# instruct browser to cache this
-			expires 2s;
-			# headers
+			# ignore upstream headers
+            # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
 			proxy_ignore_headers Cache-Control;
-			proxy_ignore_headers Set-Cookie;
 			proxy_ignore_headers X-Accel-Expires;
 			proxy_ignore_headers Expires;
-			proxy_hide_header Pragma;
+            # hide upstream X-Cache header
+			proxy_hide_header X-Cache;
+            # add own X-Cache header
 			add_header X-Cache $upstream_cache_status;
+
+            # ts-specific
+			# how long should the data be kept in this cache
+			proxy_cache_valid 200 10s;
+			# instruct browsers to cache this for 6s
+			expires 6s;
 		}
 	}
 	location /resources/ {
@@ -81,7 +85,6 @@ server {
 			proxy_ignore_headers Set-Cookie;
 			proxy_ignore_headers X-Accel-Expires;
 			proxy_ignore_headers Expires;
-			proxy_hide_header Pragma;
 			add_header X-Cache $upstream_cache_status;
 		}
 	}
diff --git a/2.Common_services/5.Nginx/vhost_mediaserver-msuser.conf b/2.Common_services/5.Nginx/vhost_mediaserver-msuser.conf
index f520a326..3461de3f 100644
--- a/2.Common_services/5.Nginx/vhost_mediaserver-msuser.conf
+++ b/2.Common_services/5.Nginx/vhost_mediaserver-msuser.conf
@@ -55,28 +55,16 @@ server {
 		}
 	}
 	location /streaming/ {
-		location ~ \.m3u8$ {
-			rewrite ^/streaming/(.*)$ /live/$1? break;
-			proxy_pass http://localhost:1935;
-			proxy_cache livecache;
-			proxy_cache_key $scheme$proxy_host$uri;
-			proxy_cache_lock on;
-			proxy_cache_min_uses 1;
-			proxy_cache_use_stale updating;
-			proxy_cache_valid 200 1s;
-		}
-		location ~ \.ts$ {
-			rewrite ^/streaming/(.*)$ /live/$1? break;
-			proxy_pass http://localhost:1935;
-			proxy_cache livecache;
-			proxy_cache_key $scheme$proxy_host$uri;
-			proxy_cache_lock on;
-			proxy_cache_min_uses 1;
-			proxy_cache_valid 200 10s;
-		}
-		# only urls to ts and m3u8 files are allowed, discard any requested path for other urls
-		rewrite ^/streaming/(.*)$ /live/? break;
-		proxy_pass http://localhost:1935/live/;
+		rewrite ^/streaming/(.*)$ /live/$1? break;
+		proxy_pass http://localhost:1935;
+        proxy_cache livecache;
+        # 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;
+        # show if playlist or fragment has been cached from wowza
+        add_header X-Cache $upstream_cache_status;
+        # wowza is defining expiration headers (m3u8 is 1s, ts is 3600)
 	}
 	location / {
 		uwsgi_pass unix:///home/msuser/mstmp/uwsgi.sock;
-- 
GitLab