{%- if "grafana" in nginx_default_sites -%}
  {%- set default_site = "default_server" -%}
{%- else -%}
  {%- set default_site = "" -%}
{%- endif -%}

#
# {{ ansible_managed }}
#

upstream grafana_app_server {
  server 127.0.0.1:3000 fail_timeout=0;
}

server {
  server_name grafana.*;
  listen {{ GRAFANA_NGINX_PORT }} {{ default_site }};
  client_max_body_size 1M;
  keepalive_timeout 5;

  location / {
    try_files $uri @proxy_to_app;
  }

  location @proxy_to_app {
    {% if NGINX_SET_X_FORWARDED_HEADERS %}
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-For $remote_addr;
    {% else %}
    proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header X-Forwarded-Port $http_x_forwarded_port;
    proxy_set_header X-Forwarded-For $http_x_forwarded_for;
    {% endif %}

    # newrelic-specific header records the time when nginx handles a request.
    proxy_set_header X-Queue-Start "t=${msec}";

    proxy_set_header Host $http_host;

    proxy_redirect off;
    proxy_pass http://grafana_app_server;
  }
}
