upstream rails_app {

server web:3000;

}

server {

listen 80;
listen 443 ssl;

# define your domain
# server_name www.example.com;

ssl_certificate     /<%= @app_home %>/certs/<%= app_name %>.crt;
ssl_certificate_key /<%= @app_home %>/certs/<%= app_name %>.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers         HIGH:!aNULL:!MD5;

# define the public application root
root /<%= @app_home %>/public;
index index.html;

# define where Nginx should write its logs
access_log /<%= @app_home %>/log/nginx.access.log;
error_log /<%= @app_home %>/log/nginx.error.log;

# serve static (compiled) assets directly if they exist
location ~ ^/(assets|packs)/ {
  try_files $uri =404;

  access_log off; # don't save asset requests to access logs
  gzip_static on; # to serve pre-gzipped version

  # Cache assets for as long as possible
  expires max;
  add_header Cache-Control public;

  # Some browsers still send conditional-GET requests if there's a
  # Last-Modified header or an ETag header even if they haven't
  # reached the expiry date sent in the Expires header.
  add_header Last-Modified "";
  add_header ETag "";
  break;
}

# Serve Sidekiq UI along with its assets
location /sidekiq/ {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;

  proxy_redirect off;
  proxy_pass http://rails_app/sidekiq/;
}

# Defines the internal location to support the X-Sendfile feature
# for the delivery of static files from the folder: /<%= @app_home %>/storage
# http://wiki.nginx.org/XSendfile
location /__storage/ {
  internal;
  alias /<%= @app_home %>/storage/;
}

# send non-static file requests to the app server
location / {
  try_files $uri @rails;
}

location @rails {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;

  proxy_set_header X-Sendfile-Type X-Accel-Redirect;
  proxy_set_header X-Accel-Mapping /<%= @app_home %>/storage/=/__storage/;

  proxy_redirect off;
  proxy_pass http://rails_app;
}

error_page 404 /404.html;
error_page 500 502 503 504 /500.html;
# client_max_body_size 1m;
# keepalive_timeout 10;

}