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

upstream lms-backend {
    {% for host in nginx_lms_gunicorn_hosts %}
        server {{ host }}:{{ edxapp_lms_gunicorn_port }} fail_timeout=0;
    {% endfor %}
}

{%- if EDXAPP_ENABLE_RATE_LIMITING -%}
# Make Zone
limit_req_zone $cookie_{{ EDXAPP_SESSION_COOKIE_NAME }} zone=cookies:10m rate={{ EDXAPP_COURSES_REQUEST_RATE }};
{%- endif -%}

server {
  # LMS configuration file for nginx, templated by ansible

  {% if NGINX_EDXAPP_ENABLE_S3_MAINTENANCE %}

  # Do not include a 502 error in NGINX_ERROR_PAGES when 
  # NGINX_EDXAPP_ENABLE_MAINTENANCE is enabled.

  error_page 502 @maintenance;

    {% include "s3_maintenance.j2" %}
  
  {% endif %}

  # error pages
  {% for k, v in NGINX_EDXAPP_ERROR_PAGES.iteritems() %}
error_page {{ k }} {{ v }};
  {% endfor %}


  {% if NGINX_ENABLE_SSL %}

  listen {{ EDXAPP_LMS_NGINX_PORT }} {{ default_site }};
  listen {{ EDXAPP_LMS_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 }};
  # request the browser to use SSL for all connections
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

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

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

  # CS184 requires uploads of up to 4MB for submitting screenshots.
  # CMS requires larger value for course assest, values provided
  # via hiera.
  client_max_body_size 4M;

  rewrite ^(.*)/favicon.ico$ /static/images/favicon.ico last;

  location @proxy_to_lms_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://lms-backend;
  }

  location / {
    {% include "basic-auth.j2" %}
    try_files $uri @proxy_to_lms_app;
  }
  # No basic auth for /segmentio/event
  location /segmentio/event {
    try_files $uri @proxy_to_lms_app;
  }

  location /notifier_api {
    try_files $uri @proxy_to_lms_app;
  }

  # No basic auth security on the github_service_hook url, so that github can use it for cms
  location /github_service_hook {
    try_files $uri @proxy_to_lms_app;
  }

  # No basic auth security on oath2 endpoint
  location /oauth2 {
    try_files $uri @proxy_to_lms_app;
  }

  # No basic auth security on the heartbeat url, so that ELB can use it
  location /heartbeat {
    try_files $uri @proxy_to_lms_app;
  }

  location /courses {
    {%- if EDXAPP_ENABLE_RATE_LIMITING -%}
    # Set Limit
    limit_req zone=cookies burst={{ EDXAPP_COURSE_REQUEST_BURST_RATE }};
    error_page  503 = /server/rate-limit.html;
    {%- endif -%}

    {%- include "basic-auth.j2" %}
    try_files $uri @proxy_to_lms_app;
  }

  {% include "robots.j2" %}
  {% include "static-files.j2" %}

  # 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;
  }
}
