# Configuration for the server
server {

  # Running port
  listen <%= node['nginx']['proxy']['listen_port'] %>;

  # Redirect all http requests to https on elb
  if ($http_x_forwarded_proto != "https") {
    return 301 https://$host$request_uri;
  }

  # Proxying the connections connections

  <% node['nginx']['proxy']['location'].each do |location| -%>
  location /<%= location['name'] %> {
    <% if location['proxy_pass'] == 'true' -%>
      proxy_redirect         http:// https://;
      proxy_set_header       Host $host;
      proxy_set_header       X-Real-IP $remote_addr;
      proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header       X-Forwarded-Host $server_name;
      proxy_pass             <%= location['protocol'] %>://<%= location['host'] %><%= location['uri'] %>:<%= location['port'] %>;
      <% if location['extra_configs'] -%>
        <% location['extra_configs'].each do |config| -%>
      <%= config %>
        <% end %>
      <% end %>
    <% else %>
      root  /var/www/;
    <% end %>
  }
  <% end %>

  <% node['nginx']['proxy']['extra_configs'].each do |config| -%>
  <%= config %>
  <% end %>

}

