github.com/jiasir/deis@v1.12.2/router/rootfs/etc/confd/templates/nginx.conf (about)

     1  # required to run in a container
     2  daemon off;
     3  
     4  user nginx;
     5  worker_processes {{ or (getv "/deis/router/workerProcesses") "auto" }};
     6  pid /run/nginx.pid;
     7  
     8  events {
     9      worker_connections {{ or (getv "/deis/router/maxWorkerConnections") "768" }};
    10      # multi_accept on;
    11  }
    12  
    13  
    14  http {
    15      # basic settings
    16      vhost_traffic_status_zone;
    17  
    18      sendfile on;
    19      tcp_nopush on;
    20      tcp_nodelay on;
    21  
    22      # The Timeout value must be greater than the front facing load balancers timeout value.
    23      # Default is the deis recommended timeout value for ELB - 1200 seconds + 100s extra.
    24      {{ $defaultTimeout := or (getv "/deis/router/defaultTimeout") "1300" }}
    25      keepalive_timeout {{ $defaultTimeout }};
    26  
    27      types_hash_max_size 2048;
    28      server_names_hash_max_size {{ or (getv "/deis/router/serverNameHashMaxSize") "512" }};
    29      server_names_hash_bucket_size {{ or (getv "/deis/router/serverNameHashBucketSize") "64" }};
    30  
    31      include /opt/nginx/conf/mime.types;
    32      default_type application/octet-stream;
    33      {{ if exists "/deis/router/gzip" }}
    34      gzip {{ getv "/deis/router/gzip" }};
    35      gzip_comp_level {{ or (getv "/deis/router/gzipCompLevel") "5" }};
    36      gzip_disable {{ or (getv "/deis/router/gzipDisable") "\"msie6\"" }};
    37      gzip_http_version {{ or (getv "/deis/router/gzipHttpVersion") "1.1" }};
    38      gzip_min_length {{ or (getv "/deis/router/gzipMinLength") "256" }};
    39      gzip_types {{ or (getv "/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" }};
    40      gzip_proxied {{ or (getv "/deis/router/gzipProxied") "any" }};
    41      gzip_vary {{ or (getv "/deis/router/gzipVary") "on" }};
    42      {{ end }}
    43  
    44      {{ $useFirewall := or (getv "/deis/router/firewall/enabled") "false" }}{{ if eq $useFirewall "true" }}# include naxsi rules
    45      include     /opt/nginx/firewall/naxsi_core.rules;
    46      include     /opt/nginx/firewall/web_apps.rules;
    47      include     /opt/nginx/firewall/scanner.rules;
    48      include     /opt/nginx/firewall/web_server.rules;{{ end }}
    49      {{ $firewallErrorCode := or (getv "/deis/router/firewall/errorCode") "400" }}
    50      client_max_body_size "{{ or (getv "/deis/router/bodySize") "1m" }}";
    51  
    52      {{ $useProxyProtocol := or (getv "/deis/router/proxyProtocol") "false" }}{{ if ne $useProxyProtocol "false" }}
    53      set_real_ip_from {{ or (getv "/deis/router/proxyRealIpCidr") "10.0.0.0/8" }};
    54      real_ip_header proxy_protocol;
    55      {{ end }}
    56  
    57      log_format upstreaminfo '[$time_local] - {{ if ne $useProxyProtocol "false" }}$proxy_protocol_addr{{ else }}$remote_addr{{ end }} - $remote_user - $status - "$request" - $bytes_sent - "$http_referer" - "$http_user_agent" - "$server_name" - $upstream_addr - $http_host - $upstream_response_time - $request_time';
    58  
    59      # send logs to STDOUT so they can be seen using 'docker logs'
    60      access_log /opt/nginx/logs/access.log upstreaminfo;
    61      error_log  /opt/nginx/logs/error.log {{ or (getv "/deis/router/errorLogLevel") "error" }};
    62  
    63      map $http_upgrade $connection_upgrade {
    64          default upgrade;
    65          ''      close;
    66      }
    67  
    68      # trust http_x_forwarded_proto headers correctly indicate ssl offloading
    69      map $http_x_forwarded_proto $access_scheme {
    70        default $http_x_forwarded_proto;
    71        ''      $scheme;
    72      }
    73  
    74      ## HSTS instructs the browser to replace all HTTP links with HTTPS links for this domain until maxAge seconds from now
    75      {{ $enableHSTS := or (getv "/deis/router/hsts/enabled") "false" }}
    76      {{ $maxAgeHSTS := or (getv "/deis/router/hsts/maxAge") "10886400" }}
    77      {{ $includeSubdomainsHSTS := or (getv "/deis/router/hsts/includeSubDomains") "false" }}
    78      {{ $preloadHSTS := or (getv "/deis/router/hsts/preload") "false" }}
    79      map $access_scheme $sts {
    80        'https' 'max-age={{ $maxAgeHSTS }}{{ if eq $includeSubdomainsHSTS "true" }}; includeSubDomains{{ end }}{{ if eq $preloadHSTS "true" }}; preload{{ end }}';
    81      }
    82  
    83      ## since HSTS headers are not permitted on HTTP requests, 301 redirects to HTTPS resources are also necessary
    84      {{ $enforceHTTPS := or (getv "/deis/router/enforceHTTPS") $enableHSTS "false" }}
    85  
    86      {{/* Enabling the enforceWhitelist option deny all connections except those from IPs explicitly allowed */}}
    87      {{ $enforceWhitelist := or (getv "/deis/router/enforceWhitelist") "false" }}
    88  
    89      ## start deis-controller
    90      {{ if exists "/deis/controller/host" }}
    91      upstream deis-controller {
    92          server {{ getv "/deis/controller/host" }}:{{ getv "/deis/controller/port" }};
    93      }
    94      {{ end }}
    95  
    96      server {
    97          server_name ~^{{ or (getv "/deis/controller/subdomain") "deis" }}\.(?<domain>.+)$;
    98          include deis.conf;
    99  
   100          {{/* IP Whitelisting */}}
   101          {{ $controllerHasWhitelist := exists "/deis/router/controller/whitelist" }}
   102          {{ if $controllerHasWhitelist }}
   103          ## Only connections from the following addresses are allowed
   104          {{ $whitelist := getv "/deis/router/controller/whitelist" }}
   105          {{ range $whitelist_entry := split $whitelist "," }}
   106          {{ $whitelist_detail := split $whitelist_entry ":" }}
   107          allow {{index $whitelist_detail 0}};{{if eq (len $whitelist_detail) 2}}  # {{index $whitelist_detail 1}}{{ end }}
   108          {{ end }}
   109          {{ end }}
   110          {{ if or (eq $enforceWhitelist "true") $controllerHasWhitelist }}
   111          deny all;
   112          {{ end }}
   113  
   114          {{ if exists "/deis/controller/host" }}
   115          location / {
   116              {{ if eq $useFirewall "true" }}include                     /opt/nginx/firewall/active-mode.rules;{{ end }}
   117              proxy_buffering             off;
   118              proxy_set_header            Host $host;
   119              {{ if ne $useProxyProtocol "false" }}
   120              proxy_set_header            X-Forwarded-For $proxy_protocol_addr;
   121              {{ else }}
   122              proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
   123              {{ end }}
   124              proxy_redirect              off;
   125              proxy_connect_timeout       {{ or (getv "/deis/router/controller/timeout/connect") "10s" }};
   126              proxy_send_timeout          {{ or (getv "/deis/router/controller/timeout/send") "20m" }};
   127              proxy_read_timeout          {{ or (getv "/deis/router/controller/timeout/read") "20m" }};
   128  
   129              proxy_pass                  http://deis-controller;
   130          }
   131          {{ else }}
   132          location / {
   133              return 503;
   134          }
   135          {{ end }}
   136  
   137          {{ if eq $useFirewall "true" }}location /RequestDenied {
   138              return {{ $firewallErrorCode }};
   139          }
   140          {{ end }}
   141  
   142          {{ if eq $enforceHTTPS "true" }}
   143          if ($access_scheme != "https") {
   144            return 301 https://$host$request_uri;
   145          }
   146          {{ end }}
   147  
   148          {{ if eq $enableHSTS "true" }}
   149          add_header Strict-Transport-Security $sts always;
   150          {{ end }}
   151      }
   152      ## end deis-controller
   153  
   154      ## start deis-store-gateway
   155      {{ if exists "/deis/store/gateway/host" }}
   156      upstream deis-store-gateway {
   157          server {{ getv "/deis/store/gateway/host" }}:{{ getv "/deis/store/gateway/port" }};
   158      }
   159      {{ end }}
   160  
   161      server {
   162          server_name ~^deis-store\.(?<domain>.+)$;
   163          include deis.conf;
   164  
   165          client_max_body_size            0;
   166  
   167          {{ if exists "/deis/store/gateway/host" }}
   168          location / {
   169              {{ if eq $useFirewall "true" }}include                     /opt/nginx/firewall/active-mode.rules;{{ end }}
   170              proxy_buffering             off;
   171              proxy_set_header            Host $host;
   172              {{ if ne $useProxyProtocol "false" }}
   173              proxy_set_header            X-Forwarded-For $proxy_protocol_addr;
   174              {{ else }}
   175              proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
   176              {{ end }}
   177              proxy_redirect              off;
   178              proxy_connect_timeout       10s;
   179              proxy_send_timeout          {{ $defaultTimeout }}s;
   180              proxy_read_timeout          {{ $defaultTimeout }}s;
   181  
   182              proxy_pass                  http://deis-store-gateway;
   183          }
   184          {{ else }}
   185          location / {
   186              return 503;
   187          }
   188          {{ end }}
   189      }
   190      ## end deis-store-gateway
   191      {{ $domains := ls "/deis/domains" }}
   192      ## start service definitions for each application
   193      {{ range $app := lsdir "/deis/services" }}
   194      {{ $upstreams := printf "/deis/services/%s/*" $app}}
   195      upstream {{ $app }} {
   196          {{ if exists "/deis/router/affinityArg" }}
   197          hash $arg_{{ getv "/deis/router/affinityArg" }} consistent;
   198          {{ end }}
   199          {{ range gets $upstreams }}server {{ .Value }};
   200          {{ end }}
   201      }
   202      {{ $appContainers := gets $upstreams }}{{ $appContainerLen := len $appContainers }}
   203      ## server entries for custom domains
   204      {{ range $app_domain := $domains }}{{ if eq $app (getv (printf "/deis/domains/%s" $app_domain)) }}
   205      server {
   206          server_name {{ $app_domain }};
   207          {{/* if a SSL certificate is installed for this domain, use SSL */}}
   208          {{/* NOTE (bacongobbler): domains are separate from the default platform domain, */}}
   209          {{/* so we can't rely on deis.conf as each domain is an island */}}
   210          {{ if exists (printf "/deis/certs/%s/cert" $app_domain) }}
   211          server_name_in_redirect off;
   212          port_in_redirect off;
   213          listen 80{{ if ne $useProxyProtocol "false" }} proxy_protocol{{ end }};
   214          listen 443 ssl http2{{ if ne $useProxyProtocol "false" }} proxy_protocol{{ end }};
   215          ssl_certificate /etc/ssl/deis/certs/{{ $app_domain }}.cert;
   216          ssl_certificate_key /etc/ssl/deis/keys/{{ $app_domain }}.key;
   217          include ssl.conf;
   218          {{/* if there's no app SSL cert but we have a router SSL cert, enable that instead */}}
   219          {{/* TODO (bacongobbler): wait for https://github.com/kelseyhightower/confd/issues/270 */}}
   220          {{/* so we can apply this config to just subdomains of the platform domain. */}}
   221          {{/* ref: https://github.com/deis/deis/pull/3519 */}}
   222          {{ else }}
   223          include deis.conf;
   224          {{ end }}
   225  
   226          {{/* IP Whitelisting */}}
   227          {{ $appHasWhitelist := exists (printf "/deis/config/%s/deis_whitelist" $app) }}
   228          {{ if $appHasWhitelist }}
   229          ## Only connections from the following addresses are allowed
   230          {{ $whitelist := getv (printf "/deis/config/%s/deis_whitelist" $app) }}
   231          {{ range $whitelist_entry := split $whitelist "," }}
   232          {{ $whitelist_detail := split $whitelist_entry ":" }}
   233          allow {{index $whitelist_detail 0}};{{if eq (len $whitelist_detail) 2}}  # {{index $whitelist_detail 1}}{{ end }}
   234          {{ end }}
   235          {{ end }}
   236          {{ if or (eq $enforceWhitelist "true") $appHasWhitelist}}
   237          deny all;
   238          {{ end }}
   239  
   240          {{ if ne $appContainerLen 0 }}
   241          location / {
   242              {{ if eq $useFirewall "true" }}include                     /opt/nginx/firewall/active-mode.rules;{{ end }}
   243              proxy_buffering             off;
   244              proxy_set_header            Host $host;
   245              set $access_ssl 'off';
   246              set $access_port '80';
   247              if ($access_scheme ~ https) {
   248                  set $access_ssl 'on';
   249                  set $access_port '443';
   250              }
   251              proxy_set_header            X-Forwarded-Port  $access_port;
   252              proxy_set_header            X-Forwarded-Proto $access_scheme;
   253              {{ if ne $useProxyProtocol "false" }}
   254              proxy_set_header            X-Forwarded-For   $proxy_protocol_addr;
   255              {{ else }}
   256              proxy_set_header            X-Forwarded-For   $proxy_add_x_forwarded_for;
   257              {{ end }}
   258              proxy_set_header            X-Forwarded-Ssl   $access_ssl;
   259              proxy_redirect              off;
   260              proxy_connect_timeout       30s;
   261              proxy_send_timeout          {{ $defaultTimeout }}s;
   262              proxy_read_timeout          {{ $defaultTimeout }}s;
   263              proxy_http_version          1.1;
   264              proxy_set_header            Upgrade           $http_upgrade;
   265              proxy_set_header            Connection        $connection_upgrade;
   266  
   267              proxy_next_upstream         error timeout http_502 http_503 http_504;
   268  
   269              {{ if eq $enforceHTTPS "true" }}
   270              if ($access_scheme != "https") {
   271                return 301 https://$host$request_uri;
   272              }
   273              {{ end }}
   274  
   275              {{ if eq $enableHSTS "true" }}
   276              add_header Strict-Transport-Security $sts always;
   277              {{ end }}
   278  
   279              ## workaround for nginx hashing empty string bug http://trac.nginx.org/nginx/ticket/765
   280              {{ if exists "/deis/router/affinityArg" }}
   281              set_random $prng 0 99;
   282              set_if_empty $arg_{{ getv "/deis/router/affinityArg" }} $prng;
   283              {{ end }}
   284  
   285              proxy_pass                  http://{{ $app }};
   286          }
   287          {{ else }}
   288          location / {
   289              return 503;
   290          }
   291          {{ end }}
   292          {{ if eq $useFirewall "true" }}location /RequestDenied {
   293              return {{ $firewallErrorCode }};
   294          }
   295          {{ end }}
   296      }{{ end }}{{ end }}
   297      ## end entries for custom domains
   298  
   299      server {
   300          server_name ~^{{ $app }}\.(?<domain>.+)$;
   301          include deis.conf;
   302  
   303          {{/* IP Whitelisting */}}
   304          {{ $appHasWhitelist := exists (printf "/deis/config/%s/deis_whitelist" $app) }}
   305          {{ if $appHasWhitelist }}
   306          ## Only connections from the following addresses are allowed
   307          {{ $whitelist := getv (printf "/deis/config/%s/deis_whitelist" $app) }}
   308          {{ range $whitelist_entry := split $whitelist "," }}
   309          {{ $whitelist_detail := split $whitelist_entry ":" }}
   310          allow {{index $whitelist_detail 0}};{{if eq (len $whitelist_detail) 2}}  # {{index $whitelist_detail 1}}{{ end }}
   311          {{ end }}
   312          {{ end }}
   313          {{ if or (eq $enforceWhitelist "true") $appHasWhitelist}}
   314          deny all;
   315          {{ end }}
   316  
   317          {{ if ne $appContainerLen 0 }}
   318          location / {
   319              {{ if eq $useFirewall "true" }}include                     /opt/nginx/firewall/active-mode.rules;{{ end }}
   320              proxy_buffering             off;
   321              proxy_set_header            Host $host;
   322              set $access_ssl 'off';
   323              set $access_port '80';
   324              if ($access_scheme ~ https) {
   325                  set $access_ssl 'on';
   326                  set $access_port '443';
   327              }
   328              proxy_set_header            X-Forwarded-Port  $access_port;
   329              proxy_set_header            X-Forwarded-Proto $access_scheme;
   330              {{ if ne $useProxyProtocol "false" }}
   331              proxy_set_header            X-Forwarded-For   $proxy_protocol_addr;
   332              {{ else }}
   333              proxy_set_header            X-Forwarded-For   $proxy_add_x_forwarded_for;
   334              {{ end }}
   335              proxy_set_header            X-Forwarded-Ssl   $access_ssl;
   336              proxy_redirect              off;
   337              proxy_connect_timeout       30s;
   338              proxy_send_timeout          {{ $defaultTimeout }}s;
   339              proxy_read_timeout          {{ $defaultTimeout }}s;
   340              proxy_http_version          1.1;
   341              proxy_set_header            Upgrade           $http_upgrade;
   342              proxy_set_header            Connection        $connection_upgrade;
   343  
   344              proxy_next_upstream         error timeout http_502 http_503 http_504;
   345  
   346              {{ if eq $enforceHTTPS "true" }}
   347              if ($access_scheme != "https") {
   348                return 301 https://$host$request_uri;
   349              }
   350              {{ end }}
   351  
   352              {{ if eq $enableHSTS "true" }}
   353              add_header Strict-Transport-Security $sts always;
   354              {{ end }}
   355  
   356              proxy_pass                  http://{{ $app }};
   357          }
   358          {{ else }}
   359          location / {
   360              return 503;
   361          }
   362          {{ end }}
   363          {{ if eq $useFirewall "true" }}location /RequestDenied {
   364              return {{ $firewallErrorCode }};
   365          }
   366          {{ end }}
   367      }{{ end }}
   368      ## end service definitions for each application
   369  
   370      {{ $scheduler := or (getv "/deis/controller/schedulerModule") "fleet" }}
   371  
   372      # default server, including "classic" healthcheck
   373      server {
   374          listen 80 default_server reuseport{{ if ne $useProxyProtocol "false" }} proxy_protocol{{ end }};
   375          location /health-check {
   376              access_log off;
   377              {{ if eq $scheduler "k8s" }}
   378              proxy_pass http://{{ getenv "HOST" }}:10249/healthz;
   379              {{ else }}
   380              default_type 'text/plain';
   381              return 200;
   382              {{ end }}
   383          }
   384          location /router-nginx-status {
   385              vhost_traffic_status_display;
   386              vhost_traffic_status_display_format html;
   387          }
   388          location / {
   389              return 404;
   390          }
   391      }
   392  
   393      # healthcheck on 9090 -- never uses proxy_protocol
   394      server {
   395          listen 9090 default_server;
   396          location /health-check {
   397              access_log off;
   398              {{ if eq $scheduler "k8s" }}
   399              proxy_pass http://{{ getenv "HOST" }}:10249/healthz;
   400              {{ else }}
   401              default_type 'text/plain';
   402              return 200;
   403              {{ end }}
   404          }
   405          location / {
   406              return 404;
   407          }
   408      }
   409  
   410      #start k8s apps
   411      {{ range $k8namespace := lsdir "/registry/services/specs/" }}
   412      {{ $k8appdir := printf "/registry/services/specs/%s/" $k8namespace}}{{ range $kapp := ls $k8appdir }}
   413      {{ $k8appPath := printf "/registry/services/specs/%s/%s" $k8namespace $kapp}}{{ $k8Svc := json (getv $k8appPath) }}
   414      {{ $upstreams := printf "/registry/services/specs/%s/%s" $k8namespace $kapp}}
   415      upstream {{ if $k8Svc.metadata.labels.name }}{{ $k8Svc.metadata.labels.name }}{{ else }}{{ $k8Svc.metadata.name }}{{ end }} {
   416          {{ if exists "/deis/router/affinityArg" }}
   417          hash $arg_{{ getv "/deis/router/affinityArg" }} consistent;
   418          {{ end }}
   419          server {{ $k8Svc.spec.clusterIP }}:80;
   420      }
   421      {{ $appContainers := gets $upstreams }}{{ $appContainerLen := len $appContainers }}
   422      {{ $k8sappname := or $k8Svc.metadata.labels.name $k8Svc.metadata.name }}
   423      ## server entries for custom domains
   424      {{ range $app_domain := $domains }}{{ if eq $k8sappname (getv (printf "/deis/domains/%s" $app_domain)) }}
   425      server {
   426          server_name {{ $app_domain }};
   427          {{/* if a SSL certificate is installed for this domain, use SSL */}}
   428          {{/* NOTE (bacongobbler): domains are separate from the default platform domain, */}}
   429          {{/* so we can't rely on deis.conf as each domain is an island */}}
   430          {{ if exists (printf "/deis/certs/%s/cert" $app_domain) }}
   431          server_name_in_redirect off;
   432          port_in_redirect off;
   433          listen 80{{ if ne $useProxyProtocol "false" }} proxy_protocol{{ end }};
   434          listen 443 ssl http2{{ if ne $useProxyProtocol "false" }} proxy_protocol{{ end }};
   435          ssl_certificate /etc/ssl/deis/certs/{{ $app_domain }}.cert;
   436          ssl_certificate_key /etc/ssl/deis/keys/{{ $app_domain }}.key;
   437          include ssl.conf;
   438          {{/* if there's no app SSL cert but we have a router SSL cert, enable that instead */}}
   439          {{/* TODO (bacongobbler): wait for https://github.com/kelseyhightower/confd/issues/270 */}}
   440          {{/* so we can apply this config to just subdomains of the platform domain. */}}
   441          {{/* ref: https://github.com/deis/deis/pull/3519 */}}
   442          {{ else }}
   443          include deis.conf;
   444          {{ end }}
   445          {{ if ne $appContainerLen 0 }}
   446          location / {
   447              {{ if eq $useFirewall "true" }}include                     /opt/nginx/firewall/active-mode.rules;{{ end }}
   448              proxy_buffering             off;
   449              proxy_set_header            Host $host;
   450              set $access_ssl 'off';
   451              set $access_port '80';
   452              if ($access_scheme ~ https) {
   453                  set $access_ssl 'on';
   454                  set $access_port '443';
   455              }
   456              proxy_set_header            X-Forwarded-Port  $access_port;
   457              proxy_set_header            X-Forwarded-Proto $access_scheme;
   458              {{ if ne $useProxyProtocol "false" }}
   459              proxy_set_header            X-Forwarded-For   $proxy_protocol_addr;
   460              {{ else }}
   461              proxy_set_header            X-Forwarded-For   $proxy_add_x_forwarded_for;
   462              {{ end }}
   463              proxy_set_header            X-Forwarded-Ssl   $access_ssl;
   464              proxy_redirect              off;
   465              proxy_connect_timeout       30s;
   466              proxy_send_timeout          {{ $defaultTimeout }}s;
   467              proxy_read_timeout          {{ $defaultTimeout }}s;
   468              proxy_http_version          1.1;
   469              proxy_set_header            Upgrade           $http_upgrade;
   470              proxy_set_header            Connection        $connection_upgrade;
   471  
   472              proxy_next_upstream         error timeout http_502 http_503 http_504;
   473  
   474              {{ if eq $enforceHTTPS "true" }}
   475              if ($access_scheme != "https") {
   476                return 301 https://$host$request_uri;
   477              }
   478              {{ end }}
   479  
   480              {{ if eq $enableHSTS "true" }}
   481              add_header Strict-Transport-Security $sts always;
   482              {{ end }}
   483  
   484              ## workaround for nginx hashing empty string bug http://trac.nginx.org/nginx/ticket/765
   485              {{ if exists "/deis/router/affinityArg" }}
   486              set_random $prng 0 99;
   487              set_if_empty $arg_{{ getv "/deis/router/affinityArg" }} $prng;
   488              {{ end }}
   489  
   490              proxy_pass                  http://{{ if $k8Svc.metadata.labels.name }}{{ $k8Svc.metadata.labels.name }}{{ else }}{{ $k8Svc.metadata.name }}{{ end }};
   491          }
   492          {{ else }}
   493          location / {
   494              return 503;
   495          }
   496          {{ end }}
   497          {{ if eq $useFirewall "true" }}location /RequestDenied {
   498              return {{ $firewallErrorCode }};
   499          }
   500          {{ end }}
   501      }{{ end }}{{ end }}
   502      ## end entries for custom domains
   503  
   504      server {
   505          server_name ~^{{ if $k8Svc.metadata.labels.name }}{{ $k8Svc.metadata.labels.name }}{{ else }}{{ $k8Svc.metadata.name }}{{ end }}\.(?<domain>.+)$;
   506          include deis.conf;
   507          {{ if ne $appContainerLen 0 }}
   508          location / {
   509              {{ if eq $useFirewall "true" }}include                     /opt/nginx/firewall/active-mode.rules;{{ end }}
   510              proxy_buffering             off;
   511              proxy_set_header            Host $host;
   512              set $access_ssl 'off';
   513              set $access_port '80';
   514              if ($access_scheme ~ https) {
   515                  set $access_ssl 'on';
   516                  set $access_port '443';
   517              }
   518              proxy_set_header            X-Forwarded-Port  $access_port;
   519              proxy_set_header            X-Forwarded-Proto $access_scheme;
   520              {{ if ne $useProxyProtocol "false" }}
   521              proxy_set_header            X-Forwarded-For   $proxy_protocol_addr;
   522              {{ else }}
   523              proxy_set_header            X-Forwarded-For   $proxy_add_x_forwarded_for;
   524              {{ end }}
   525              proxy_set_header            X-Forwarded-Ssl   $access_ssl;
   526              proxy_redirect              off;
   527              proxy_connect_timeout       30s;
   528              proxy_send_timeout          {{ $defaultTimeout }}s;
   529              proxy_read_timeout          {{ $defaultTimeout }}s;
   530              proxy_http_version          1.1;
   531              proxy_set_header            Upgrade           $http_upgrade;
   532              proxy_set_header            Connection        $connection_upgrade;
   533  
   534              proxy_next_upstream         error timeout http_502 http_503 http_504;
   535  
   536              {{ if eq $enforceHTTPS "true" }}
   537              if ($access_scheme != "https") {
   538                return 301 https://$host$request_uri;
   539              }
   540              {{ end }}
   541  
   542              {{ if eq $enableHSTS "true" }}
   543              add_header Strict-Transport-Security $sts always;
   544              {{ end }}
   545  
   546              proxy_pass                  http://{{ if $k8Svc.metadata.labels.name }}{{ $k8Svc.metadata.labels.name }}{{ else }}{{ $k8Svc.metadata.name }}{{ end }};
   547          }
   548          {{ else }}
   549          location / {
   550              return 503;
   551          }
   552          {{ end }}
   553          {{ if eq $useFirewall "true" }}location /RequestDenied {
   554              return {{ $firewallErrorCode }};
   555          }
   556          {{ end }}
   557      }{{end}}{{end}}
   558  }
   559  
   560  ## start builder
   561  {{ if exists "/deis/builder/host" }}
   562  stream {
   563  
   564      upstream builder {
   565          server {{ getv "/deis/builder/host" }}:{{ getv "/deis/builder/port" }};
   566      }
   567  
   568      server {
   569          listen 2222;
   570          proxy_connect_timeout  {{ or (getv "/deis/router/builder/timeout/connect") "10000" }};
   571          proxy_timeout          {{ or (getv "/deis/router/builder/timeout/tcp") "1200000" }};
   572          proxy_pass builder;
   573      }
   574  }{{ end }}
   575  ## end builder