# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
upstream xserver_app_server {
    {% for host in nginx_xserver_gunicorn_hosts %}
        server {{ host }}:{{ xserver_gunicorn_port }} fail_timeout=0;
    {% endfor %}
}

server {
  listen {{ XSERVER_NGINX_PORT }} default_server;

  location / {
    {% if XSERVER_ENABLE_BASIC_AUTH|bool %}
      {% include "basic-auth.j2" %}
    {% endif %}
    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;

    # 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://xserver_app_server;
  }

  # Used for xserver healthcheck
  location /index.html {
    try_files $uri @proxy_to_app;
  }
}
