﻿
    ## All static files will be served directly.
    location /banner.png{
            access_log off;
            log_not_found     off;
    }
    location ~ ^/(system/temp)/ {
        location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {
            access_log off;
            log_not_found     off;
            expires 30d;
            add_header Cache-Control public;

            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;

            ## Set the OS file cache.
            open_file_cache max=3000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|eot|svg|ttf|otf)$ {
            access_log        off;
            log_not_found     off;
            expires 365d;
        }
    }

    #
    # Alias
    #
    location /pub {
        alias /usr/local/Somnode/content/public;
        autoindex off;
        location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {
            access_log off;
            log_not_found     off;
            expires 30d;
            add_header Cache-Control public;

            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;

            ## Set the OS file cache.
            open_file_cache max=3000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|eot|svg|ttf|otf)$ {
            access_log        off;
            log_not_found     off;
            expires 365d;
        }
    }

    location /covers {
        alias /usr/local/Somnode/content/covers;
        autoindex off;
        location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {
            access_log off;
            log_not_found     off;
            expires 30d;
            add_header Cache-Control public;

            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;

            ## Set the OS file cache.
            open_file_cache max=3000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|eot|svg|ttf|otf)$ {
            access_log        off;
            log_not_found     off;
            expires 365d;
        }
    }





    #
    # Redirections
    #
    location /xml {
      rewrite ^/xml/stats\.xml$ /system/mpm.php$1 last;
    }
    location /connectioncounts {
        rewrite ^/connectioncounts\.xml$ /system/mpmdigest.php$1 last;
        rewrite ^/connectioncounts(.*)$ /system/mpmdigest.php$1 last;
    }
    location ~ ^/(public|start)/([^/]+)/?([^/]+)?$ {
        include proxy_cache_settings;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=PublicPage&slug=$2&unique_id=$2&url=$3;
    }
    location ~ ^/public-json/([^/]+)?$ {
        include proxy_cache_settings;
        proxy_cache_valid 200 20s;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=PublicPage&ServerID=$1&a=publicData;
    }
    location ~ ^/ondemand-feed/([^/]+)/([^/]+)/([^/]+)?$ {
        include proxy_cache_settings;
        proxy_cache_valid 200 20s;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=OndemandPlaylist&ServerID=$2&a=feed$1&id=$3;
    }

    # Competitor panel compatibility links
    location ~ ^/([0-9]+)/([A-Za-z0-9_\-]+) {
        set $stream_url https://127.0.0.1:$1/$2;
        proxy_buffering off;
        proxy_ignore_client_abort off;
        proxy_intercept_errors off;
        proxy_redirect off;
        proxy_next_upstream error timeout invalid_header;
        proxy_pass_request_headers on;
        proxy_set_header Cache-Control no-cache;
        proxy_set_header User-Agent "$http_user_agent [ip:$remote_addr]";
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_connect_timeout 5;
        proxy_send_timeout 15;
        proxy_read_timeout 15;
        proxy_max_temp_file_size 0;
        proxy_pass $stream_url;
        expires off;
        client_max_body_size 1M;
        tcp_nodelay on;
    }

    # Tune in links
    rewrite ^/tunein/([^/]+)/([^/]+)(\.|/)([a-zA-Z0-9_\-]+)$ /system/playlist.php?slug=$1&unique_id=$1&mount=$2&type=$4 last;
    rewrite ^/tunein/([^/]+)(\.|/)([a-zA-Z0-9_\-]+)$ /system/playlist.php?slug=$1&unique_id=$1&type=$3 last;
    rewrite ^/stream/([A-Za-z0-9_\-]+)\.pls$ /system/playlist.php?slug=$1&unique_id=$1&type=$3&mount=$arg_mp last;

    rewrite ^/js/streaminfo/([^/]+)$ /system/misc/scripts/streaminformation.js.php?slug=$1&unique_id=$1 last;
    location = /system/misc/scripts/streaminformation.js.php {
        add_header X-Cache-Status $upstream_cache_status;
        fastcgi_pass unix:/usr/local/Somnode/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_cache fastcgi_cache;
        fastcgi_cache_valid 200 15s;
    }

    location ~ ^/json/stream/([^/]+)$ {
        include proxy_cache_settings;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=StreamInfo&slug=$1&unique_id=$1;
    }
    location ~ ^/map/([^/]+)/connections$ {
        include proxy_cache_settings;
        proxy_cache_valid 200 30s;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=Map&slug=$1&unique_id=$1&a=connections;
    }
	location ~ ^/map/(.*) {
	    # Attempt to access /map/index.php (<= 2.10.x) or intercept error and fallback to new default
		fastcgi_pass unix:/usr/local/Somnode/php/php-fpm.sock;
		fastcgi_index  index.php;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		fastcgi_intercept_errors on;
		include        fastcgi_params;
		error_page 404 = @newmap;
    }
	location @newmap {
        include proxy_cache_settings;
        proxy_cache_valid 200 1s;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=Map&slug=$query_string$1&unique_id=$query_string$1;
	}
    location ~ ^/country-stats/([^/]+)/$ {
        include proxy_cache_settings;
        proxy_cache_valid 200 30s;
        proxy_pass $scheme://127.0.0.1:2021/system/country-stats.php?unique_id=$1&header=$arg_header&version=2;
    }

    # Social Media Callbacks
    rewrite ^/socialmediapost/twitter/([^/]+)/?$ /system/misc/social/twitter/callback.php?id=$1 last;
    rewrite ^/socialmediapost/facebook/([^/]+)/?$ /system/misc/social/facebook/login-callback.php?id=$1 last;

    # Media Preview
    location ~ ^/media-preview/([0-9]+) {
        set $stream_url https://127.0.0.1:2000/controller/Media/$1/preview;
        gzip off;
        proxy_redirect off;
        proxy_pass_request_headers on;
        proxy_pass $stream_url;
        expires off;
        client_max_body_size 1M;
        tcp_nodelay on;
    }

    location ~ ^/media-preview-internal/([0-9]+)/(.+) {
        internal;
        alias "/usr/local/Somnode/content/user_$1/music/$2";
    }

    # Audio Player
    location ~ ^/AudioPlayer/([^/]+)/?(playerInfo|playerConfig|albumCover)?/?$ {
        add_header X-Cache-Status $upstream_cache_status;
        include proxy_cache_settings;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=AudioPlayer&slug=$1&unique_id=$1&a=$2&showSettingsLink=$arg_showSettingsLink&mount=$arg_mount;
    }

    # Video Player
    location ~ ^/VideoPlayer/([^/]+)/?(index|playerInfo)?/?$ {
        add_header X-Cache-Status $upstream_cache_status;
        include proxy_cache_settings;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=VideoPlayer&slug=$1&unique_id=$1&a=$2&id=$3&autoplay=$arg_autoplay;
    }
    location ~ ^/VideoPlayer/([^/]+)/index/([0-9]+)$ {
        add_header X-Cache-Status $upstream_cache_status;
        include proxy_cache_settings;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=VideoPlayer&slug=$1&unique_id=$1&a=index&id=$2&autoplay=$arg_autoplay;
    }

    # Dashboard Stats Caching
    location /dashboard-stats {
        add_header X-Cache-Status $upstream_cache_status;
        include proxy_cache_settings;
        proxy_cache_valid 200 10s;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=Statistics&a=index;
    }

	# WHMCS
    location ~ ^/whmcs-status/([^/]+)/index.php$ {
        include proxy_cache_settings;
        proxy_pass $scheme://127.0.0.1:2021/index.php?page=c&c=WhmcsStatus&key=$1;
    }

    # API controller rewrite
    rewrite ^/api/?([0-9]+)?/([^/]+)/?([^/]+)?/?([^/]+)?/?$ /system/api/index.php?c=$2&ServerID=$1&a=$3&id=$4 last;
	location ~ /api/ {
        limit_req zone=api_rate_limit burst=10 delay=5;
		limit_req_status 429;

        fastcgi_pass unix:/usr/local/Somnode/php/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
	}

    # Generic Controller Rewrite
    rewrite ^/controller/([^/]+)/?([0-9]+)?/?([^/]+)?/?([^/]+)?/?$ /index.php?page=c&c=$1&ServerID=$2&a=$3&id=$4 last;
    rewrite ^/controller/([^/]+)/?([^/]+)?/?([^/]+)?/?([^/]+)?/?$ /index.php?page=c&c=$1&a=$2&id=$3 last;
    rewrite ^/page/([^/]+)/?([0-9]+)?/?$ /index.php?page=$1&id=$2 last;
    rewrite ^/page/([^/]+)/?([^/]+)?/?([0-9]+)?/?$ /index.php?page=$1&m=$2&id=$3 last;
    rewrite ^/page/([^/]+)/?([^/]+)?/?([^/]+)?/?([0-9]+)?/?$ /index.php?page=$1&m=$2&action=$3&id=$4 last;
    rewrite ^/page/([^/]+)/?([^/]+)?/?([0-9]+)?/?([^/]+)?/?([0-9]+)?/?$ /index.php?page=$1&m=$2&ServerID=$3&action=$4&id=$5 last;

    # Setup Rewrites
    rewrite ^/setup/([^.]+)(?!(php|css|js))$ /setup/setup.php?method=$1 last;


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass unix:/usr/local/Somnode/php/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        access_log        off;
        log_not_found     off;
        expires 365d;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }

    # PHP-FPM Status page
    location /status {
        allow 127.0.0.1;
        deny all;
        include fastcgi_params;
        fastcgi_param REQUEST_METHOD    $request_method;
        fastcgi_param QUERY_STRING      $query_string;
        fastcgi_param SCRIPT_NAME       /status;   # see notes below
        fastcgi_param SCRIPT_FILENAME   "";        # see notes below
        fastcgi_pass unix:/usr/local/Somnode/php/php-fpm.sock;
    }


	# Custom locations
	include ../conf.d/locations[.]custom;

