apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    worker_rlimit_nofile 65535;
    events {
      worker_connections  65535;
    }
    http {
          map $http_upgrade $connection_upgrade {
            default upgrade;
            '' close;
          }
          ssl_certificate     /etc/ssl/fullchain1.pem;
          ssl_certificate_key /etc/ssl/privkey1.pem;
          ssl_ciphers         EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
          ssl_protocols       TLSv1.2;
      server {
           listen 80 default_server;
           server_name _;
           return 301 https://$host$request_uri;
      }
      server {
           listen 443 ssl;
           server_name www.cafjs.com;
           return 301 https://www.cafjslabs.com$request_uri;
      }
      server {
           listen 443 ssl;
           server_name cafjs.com;
           return 301 https://cafjslabs.com$request_uri;
      }
      server {
           listen 443 ssl;
           server_name ~^(?<user>[^-]+)-cdn\.cafjs\.com$;
           location ~ ^\/(?<app>[^\/]+)\/(?<rest>.+)$ {
              resolver kube-dns.kube-system.svc.cluster.local;
              proxy_pass http://${user}-${app}.apps.svc.cluster.local:3000/${rest};
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header Host $host;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           }
      }
      server {
          listen 443 ssl;
          server_name ~^host(?<one>[^-]+)-(?<two>[^-]+)-(?<three>[^-]+)-(?<fourth>[^-]+)-(?<app>[^.]+)\.cafjs\.com$;
          access_log /dev/stdout;
          location / {
              resolver kube-dns.kube-system.svc.cluster.local;
              proxy_pass http://${one}-${two}-${three}-${fourth}.apps.pod.cluster.local:3000;

              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header Host $host;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_read_timeout 86400s;
              proxy_send_timeout 86400s;

              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection $connection_upgrade;
          }
      }
      server {
          listen 443 ssl;
          server_name ~^(?<service>[^.]+)\.cafjs\.com$;
          access_log /dev/stdout;
          location / {
              resolver kube-dns.kube-system.svc.cluster.local;
              proxy_pass http://${service}.apps.svc.cluster.local:3000;

              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header Host $host;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_read_timeout 86400s;
              proxy_send_timeout 86400s;

              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection $connection_upgrade;
          }
      }
      server {
          listen 443 ssl default_server;
          return 444;
      }
     }
