##
# You should look at the following URL's in order to grasp a solid understanding
# 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.
##

# Default server configuration
#
server {
  listen 80 default_server deferred;
  listen [::]:80 default_server deferred ipv6only=on;

  server_name <%= fetch(:server) %>;

  passenger_enabled on;
  rails_env <%= fetch(:stage) %>;
  root <%= fetch(:deploy_to) %>/current/public;

  error_page 404 /404.html;
  error_page 422 /422.html;
  error_page 500 502 503 504 /500.html;

<% if fetch(:stage) == :production %>
  # SSL configuration
  #
  # listen 443 ssl default_server deferred;
  # listen [::]:443 ssl default_server deferred ipv6only=on;
  #
  # ssl_certificate /etc/ssl/certs/$server_name.chained.crt;
  # ssl_certificate_key /etc/ssl/private/$server_name.key;
  #
  # Self signed certs generated by the ssl-cert package
  # Don't use them in a production server!
  #
  # include snippets/snakeoil.conf;
<% end %>

  location /assets/ {
    gzip_static on;
    expires 1M;
    add_header Cache-Control public;
    access_log    off;
    log_not_found off;
  }

<% fetch(:nginx_public_dirs).each do |folder| %>
  location /<%= folder %>/ {
    expires 1M;
    add_header Cache-Control public;
    access_log    off;
    log_not_found off;
  }
<% end %>

<% fetch(:nginx_public_files).each do |file| %>
  location = /<%= file %> {
    expires 1M;
    add_header Cache-Control public;
    access_log    off;
    log_not_found off;
  }
<% end %>
}

<% if fetch(:nginx_redirects).any? %>
server {
  listen 80;
  listen 443 ssl;

  server_name <%= fetch(:server) %>;

<% fetch(:nginx_redirects).each do |src, dst| %>
  location = /<%= src.sub(/^\//, '') %> {
    return 301 $scheme://$server_name/<%= dst.sub(/^\//, '') %>;
  }
<% end %>
}
<% end %>