{%- if "kibana" in nginx_default_sites -%}
  {%- set default_site = "default" -%}
{%- else -%}
  {%- set default_site = "" -%}
{%- endif -%}

upstream elasticsearch_server {
		 server 127.0.0.1:9200;
}

server {
  # Kibana server, templated by ansible

  {% if NGINX_ENABLE_SSL %}

  listen {{ KIBANA_NGINX_PORT }} {{ default_site }};
  listen {{ KIBANA_SSL_NGINX_PORT }} {{ default_site }} ssl;

  ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }};
  ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }};

  {% else %}
  listen {{ KIBANA_NGINX_PORT }} {{ default_site }};
  {% endif %}
  
  # Nginx does not support nested condition or or conditions so
  # there is an unfortunate mix of conditonals here.
  {% if NGINX_REDIRECT_TO_HTTPS %}
     {% if NGINX_HTTPS_REDIRECT_STRATEGY == "scheme" %}
  # Redirect http to https over single instance
  if ($scheme != "https") 
  { 
   set $do_redirect_to_https "true";
  }

     {% elif NGINX_HTTPS_REDIRECT_STRATEGY == "forward_for_proto" %}

  # Forward to HTTPS if we're an HTTP request... and the server is behind ELB 
  if ($http_x_forwarded_proto = "http") 
  {
   set $do_redirect_to_https "true";
  }
  {% endif %}

  # Execute the actual redirect
  if ($do_redirect_to_https = "true")
  {
  return 301 https://$host$request_uri;
  }
  {% endif %}
  
  server_name {{ KIBANA_SERVER_NAME }};

  root {{ kibana_app_dir }}/htdocs;

  access_log {{ nginx_log_dir }}/kibana.access.log;
  error_log {{ nginx_log_dir }}/kibana.error.log error;

  # Access restriction
  {% if KIBANA_ENABLE_BASIC_AUTH|bool %}
    {% include "basic-auth.j2" %}
  {% endif %}

  # Set image format types to expire in a very long time
  location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
      access_log off;
      expires max;
  }

  # Set css and js to expire in a very long time
  location ~* ^.+\.(css|js)$ {
      access_log off;
      expires max;
  }

  # Elastic Search
  location /e {
	  rewrite /e/(.*) /$1 break;
	  proxy_set_header X-Real-IP $remote_addr;
	  proxy_set_header X-Forwarded-For $http_x_forwarded_for;

	  proxy_redirect off;
	  proxy_pass http://elasticsearch_server;
    }

  # Kibana
  location / {
	  root {{ kibana_app_dir }}/htdocs;
	  index index.html;
	  expires 1d;
	  try_files $uri/ $uri;
	  if (-f $request_filename) {
		  break;
	  }
  }
}
