# required to run in a container
daemon off;

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    # basic settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;

    include /opt/nginx/conf/mime.types;
    default_type application/octet-stream;
    {{ if .deis_router_gzip }}
    gzip {{ .deis_router_gzip }};
    gzip_comp_level {{ or .deis_router_gzipCompLevel "5" }};
    gzip_disable {{ or .deis_router_gzipDisable "\"msie6\"" }};
    gzip_http_version {{ or .deis_router_gzipHttpVersion "1.1" }};
    gzip_min_length {{ or .deis_router_gzipMinLength "256" }};
    gzip_types {{ or .deis_router_gzipTypes "application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component" }};
    gzip_proxied {{ or .deis_router_gzipProxied "any" }};
    gzip_vary {{ or .deis_router_gzipVary "on" }};
    {{ end }}
    client_max_body_size {{ or (.deis_router_bodySize) "1m" }};

    # send logs to STDOUT so they can be seen using 'docker logs'
    access_log /opt/nginx/logs/access.log;
    error_log  /opt/nginx/logs/error.log;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    ## start deis-controller
    {{ if .deis_controller_host }}
    upstream deis-controller {
        server {{ .deis_controller_host }}:{{ .deis_controller_port }};
    }

    server {
        server_name ~^deis\.(?<domain>.+)$;
        include deis.conf;

        location / {
            proxy_buffering             off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect              off;
            proxy_connect_timeout       {{ or (.deis_router_controller_timeout_connect) "10s" }};
            proxy_send_timeout          {{ or (.deis_router_controller_timeout_send) "20m" }};
            proxy_read_timeout          {{ or (.deis_router_controller_timeout_read) "20m" }};

            proxy_pass                  http://deis-controller;
        }
    }{{ end }}
    ## end deis-controller

    ## start deis-store-gateway
    {{ if .deis_store_gateway_host }}
    upstream deis-store-gateway {
        server {{ .deis_store_gateway_host }}:{{ .deis_store_gateway_port }};
    }

    server {
        server_name ~^deis-store\.(?<domain>.+)$;
        include deis.conf;
        
        client_max_body_size            0;

        location / {
            proxy_buffering             off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect              off;
            proxy_connect_timeout       10s;
            proxy_send_timeout          1200s;
            proxy_read_timeout          1200s;

            proxy_pass                  http://deis-store-gateway;
        }
    }{{ end }}
    ## end deis-store-gateway

    ## start service definitions for each application
    {{ $useSSL := or .deis_router_sslCert "false" }}
    {{ $domains := .deis_domains }}{{ range $service := .deis_services }}{{ if $service.Nodes }}
    upstream {{ Base $service.Key }} {
        {{ range $upstream := $service.Nodes }}server {{ $upstream.Value }};
        {{ end }}
    }

    server {
        server_name ~^{{ Base $service.Key }}\.(?<domain>.+)${{ range $app_domains := $domains }}{{ if eq (Base $service.Key) (Base $app_domains.Key) }} {{ $app_domains.Value }}{{ end }}{{ end }};
        include deis.conf;

        location / {
            proxy_buffering             off;
            proxy_set_header            Host $host;
            {{ if ne $useSSL "false" }}
            proxy_set_header            X-Forwarded-Proto $scheme;
            {{ end }}
            proxy_set_header            X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_redirect              off;
            proxy_connect_timeout       10s;
            proxy_send_timeout          1200s;
            proxy_read_timeout          1200s;
            proxy_http_version          1.1;
            proxy_set_header            Upgrade           $http_upgrade;
            proxy_set_header            Connection        $connection_upgrade;

            proxy_next_upstream         error timeout http_502 http_503 http_504;

            add_header                  X-Deis-Upstream   $upstream_addr;

            proxy_pass                  http://{{ Base $service.Key }};
        }
    }
    {{ end }}{{ end }}
    ## end service definitions for each application

    # healthcheck
    server {
        listen 80 default_server;
        location /health-check {
            default_type 'text/plain';
            access_log off;
            return 200;
        }
    }
}

## start builder
{{ if .deis_builder_host }}
tcp {
    access_log /opt/nginx/logs/git.log;
    tcp_nodelay on;
    timeout {{ or (.deis_router_builder_timeout_tcp) "1200000" }};

    # same directive names, but these are in miliseconds...
    proxy_connect_timeout       {{ or (.deis_router_builder_timeout_connect) "10000" }};
    proxy_send_timeout          {{ or (.deis_router_builder_timeout_send) "1200000" }};
    proxy_read_timeout          {{ or (.deis_router_builder_timeout_read) "1200000" }};

    upstream builder {
        server {{ .deis_builder_host }}:{{ .deis_builder_port }};
    }

    server {
        listen 2222;
        proxy_pass builder;
    }
}{{ end }}
## end builder
