### proxy-version: 1.0.3


worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 8192;
}

http {
    resolver 8.8.8.8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format proxy_modul '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"'
					   '$request_time, $request_length, $bytes_sent, $upstream_response_time';

    access_log /var/log/nginx/access.log proxy_modul buffer=32k flush=1m;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_min_length 500;
    gzip_types text/css text/javascript application/x-javascript application/javascript text/plain text/xml text/x-component text/json application/json application/atom image/svg+xml;
    gzip_disable "msie6";

    proxy_cache_path /tmp levels=1:2 keys_zone=hls_cache:10m max_size=10g inactive=60m use_temp_path=off;
    proxy_connect_timeout 3s;
	proxy_send_timeout 60s;
	proxy_read_timeout 60s;

    server {

        listen 80;
        server_name example.com;

        location / {
            return 301 https://example.com$request_uri;
            }

        listen 443 ssl;
        server_name example.com;

        ssl_certificate /etc/nginx/ssl.crt;
        ssl_certificate_key /etc/nginx/ssl.key;

        location ~ /(.*m3u8) {
            proxy_pass https://stream-cdn0.kontur.host/$1;
            proxy_cache hls_cache;
            proxy_cache_lock on;
            proxy_cache_valid 200 1s;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            }

        location ~ /(.*) {
            proxy_pass https://stream-cdn0.kontur.host/$1;

            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*' always;
                add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
                add_header 'Access-Control-Allow-Headers' '*' always;
                add_header 'Access-Control-Allow-Private-Network' 'true' always;
                add_header 'Access-Control-Max-Age' 7200;
                add_header 'Timing-Allow-Origin' '*' always;
                add_header 'Content-Length' 0;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                return 204;
                }

            proxy_cache hls_cache;
            proxy_cache_lock on;
            proxy_cache_valid 200 1h;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        }
    }
}
