##  Nginx rules to cache shindig concat, static js contents in reverse proxy.
# Cache the /gadgets/concat requests and revalidate upstream every 2 minutes

location ^~ /gadgets/concat {
    proxy_pass $scheme://$backend;
    proxy_ignore_headers Set-Cookie Cache-Control Expires X-Accel-Expires;
    # Bypass concat request depending on nocache=1 query param.
    # Referrer header will not have window url and will have ifr url.
    # Removing proxy_no_cache, the values fetched using bypass should update the cache.
    proxy_cache_bypass $shindig_cache_bypass;
    # Cachekey includes all queryparams as concatenated filenames info is passed via query params.
    # All that info is required to pass correct cached response from cache.
    proxy_cache_key "$request_uri";
    proxy_cache $shindig_cache;
    add_header X-ReverseProxy-Cache-Status $upstream_cache_status;
    proxy_cache_revalidate on;
    proxy_cache_valid 200 302 2m;
    add_header Cache-Control "must-revalidate";
    etag on;
}

# Cache the /gadgets/js/core:rpc.js requests and revalidate upstream every 2 minutes
location ^~ /gadgets/js/core:rpc.js {
    # rate limiting set for shindig requests
    limit_req zone=finesseratelimit burst=${NGX_FIN_SHINDIG_CORE_RPC_REQUEST_BURST_LIMIT} nodelay;
    proxy_pass $scheme://$backend;
    proxy_ignore_headers Set-Cookie Cache-Control Expires X-Accel-Expires;
    # Bypass core:rpc request depending on nocache=1 query param.
    # Referrer header will not have window url and will have ifr url.
    # Removing proxy_no_cache, the values fetched using bypass should update the cache.
    proxy_cache_bypass $shindig_cache_bypass;
    # Cache key will not have query params.
    proxy_cache_key "$uri";
    proxy_cache $shindig_cache;
    #expires 30d;
    add_header X-ReverseProxy-Cache-Status $upstream_cache_status;
    proxy_cache_revalidate on;
    proxy_cache_valid 200 302 2m;
    add_header Cache-Control "must-revalidate";
    etag on;
}

# Cache the /gadgets/js/shindig-container requests and revalidate upstream every 2 minutes
location ^~ /gadgets/js/shindig-container {
    proxy_pass $scheme://$backend;
    proxy_ignore_headers Set-Cookie Cache-Control Expires X-Accel-Expires;
    # Bypass shindig container request depending on whether bypassServerCache=true
    # or nocache is present in referer header (window url).
    # Removing proxy_no_cache, the values fetched using bypass should update the cache.
    proxy_cache_bypass $shindig_cache_bypass;
    # Cachekey includes all queryparams.
    proxy_cache_key "$request_uri";
    proxy_cache $shindig_cache;
    #expires 30d;
    add_header X-ReverseProxy-Cache-Status $upstream_cache_status;
    proxy_cache_revalidate on;
    proxy_cache_valid 200 302 2m;
    add_header Cache-Control "must-revalidate";
    etag on;
}

# Cache the /gadgets/ifr requests and revalidate upstream every 2 minutes
location ^~ /gadgets/ifr {
    # rate limiting set for shindig requests
    limit_req zone=finesseratelimit burst=${NGX_FIN_SHINDIG_IFR_REQUEST_BURST_LIMIT} nodelay;
    proxy_pass $scheme://$backend;
    proxy_ignore_headers Set-Cookie Cache-Control Expires X-Accel-Expires;
    # Removing proxy_no_cache, the values fetched using bypass should update the cache.
    # Bypass ifr request depending on nocache=1 query param.
    # Referrer header will not have window url and will have ifr url.
    proxy_cache_bypass $shindig_cache_bypass;
    proxy_cache_key "$uri?url=$arg_url&up_locale=$arg_up_locale&up_hostPort=$arg_up_hostPort";
    proxy_cache $shindig_cache;
    #expires 24h;
    add_header X-ReverseProxy-Cache-Status $upstream_cache_status;
    proxy_cache_revalidate on;
    proxy_cache_valid 200 302 2m;
    add_header Cache-Control "must-revalidate";
    etag on;
}

# Allow only POST requests on makeRequest and metadata
location ~* ^/gadgets/(makeRequest|metadata)$ {
    limit_except POST OPTIONS { deny all; }
    proxy_pass $scheme://$backend;
    proxy_no_cache 1;
    proxy_cache_bypass 1;
}


location ~* ^/gadgets/proxy(/.*)?$ {
    limit_except GET OPTIONS { deny all; }
    proxy_pass $scheme://$backend;
    proxy_no_cache 1;
    proxy_cache_bypass 1;
}

location = /tomcat_running_check.html {
    limit_except GET OPTIONS { deny all; }
    proxy_pass $scheme://$backend;
    proxy_no_cache 1;
    proxy_cache_bypass 1;
}
