#
# {{ ansible_managed }}
#
{# This prevents the injected comment from eating the server
   directive.  There's probably a better way of doing this,
   but I don't know it currently.
#}
{% raw %}

{% endraw %}

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

{% if devstack %}
{# Connects to webbrick on port 4567 typically. Appropriate for development deployments #}

upstream forum_app_server {
  server localhost:{{ forum_unicorn_port }} fail_timeout=0;
}
{% else %}
{# Connects to unicorn over a unix socket.  Appropriate for production deployments #}

upstream forum_app_server {
  server unix:{{ forum_data_dir }}/forum.sock fail_timeout=0;
}
{% endif %}

server {

  server_name forum.*;
  listen {{ FORUM_NGINX_PORT }} {{ default_site }};
  client_max_body_size 1M;
  keepalive_timeout 5;

  location / {
    try_files $uri @proxy_to_app;
  }

  {% include "robots.j2" %}

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 %}
    proxy_set_header Host $http_host;

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