apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: gateway
data:
  nginx.conf: |
    user  nginx;
    worker_processes  auto;

    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;


    events {
        worker_connections  1024;
    }

    http {
        # include       /etc/nginx/mime.types;
        default_type  application/json;

        # log_format main '$remote_addr $host $remote_user [$time_local] "$request" '
        #                 '$status $body_bytes_sent "$http_referer" '
        #                 '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';

        log_format json escape=json '{'
            '"time": "$time_local",'
            '"remote_addr": "$remote_addr",'
            '"host": "$host",'
            '"remote_user": "$remote_user",'
            '"status": "$status",'
            '"server_protocol": "$server_protocol",'
            '"request_method": "$request_method",'
            '"request_uri": "$request_uri",'
            '"request": "$request",'
            '"body_bytes_sent": "$body_bytes_sent",'
            '"request_time": "$request_time",'
            '"upstream_response_time": "$upstream_response_time",'
            '"http_referer": "$http_referer", '
            '"http_user_agent": "$http_user_agent",'
            '"http_x_forwarded_for": "$http_x_forwarded_for",'
            '"http_x_forwarded_proto": "$http_x_forwarded_proto"'
        '}';

        # access_log /var/log/nginx/access.log main;
        access_log /var/log/nginx/access.log json;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }
  default.conf: |
    resolver kube-dns.kube-system.svc.cluster.local valid=60s ipv6=off;

    client_max_body_size    4M;
    client_body_buffer_size 4M;

    server {
      listen            80;
      server_name       0.0.0.0;
      keepalive_timeout 15;

      return 301 https://$host$request_uri;
    }

    server {
      listen            443 ssl;
      server_name       0.0.0.0;
      keepalive_timeout 60;

      ssl_protocols              TLSv1.2;
      ssl_prefer_server_ciphers  on;
      ssl_certificate            /etc/tls/cert.pem;
      ssl_certificate_key        /etc/tls/privkey.pem;

      location / {
        set $upsteam http://native-gateway.default.svc.cluster.local:8080;

        proxy_pass       $upsteam;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Request-ID $request_id;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
      }

      location /admin {
        set $upsteam http://admin-gateway.default.svc.cluster.local:8080;

        proxy_pass       $upsteam;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Request-ID $request_id;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        rewrite /admin/(.*)$ /$1 break;
      }

      location /health {
        access_log off;
        return     200;
      }
    }
