#
# {{ ansible_managed }}
#


{% if nginx_default_sites is defined and "neo4j" in nginx_default_sites %}
  {% set default_site = "default" %}
{% else %}
  {% set default_site = "" %}
{% endif %}

server {
  server_name {{ NEO4J_SERVER_NAME }};

  {% if NGINX_ENABLE_SSL %}

  listen 443 ssl;
  ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }};
  ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }};
  # request the browser to use SSL for all connections
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

  {% else %}
  listen 80 {{ default_site }};
  {% endif %}


  location / {
    try_files $uri @proxy_to_app;
  }

  location @proxy_to_app {
    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;
    proxy_set_header Host $http_host;

    proxy_redirect off;
    proxy_pass http://127.0.0.1:{{ neo4j_http_port }};
  }

  # Forward to HTTPS if we're an HTTP request...
  if ($http_x_forwarded_proto = "http") {
    set $do_redirect "true";
  }

  # Run our actual redirect...
  if ($do_redirect = "true") {
    rewrite ^ https://$host$request_uri? permanent;
  }
}
