user                    nginx;
error_log               /var/log/nginx/error.log debug;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    33282;

events {
  worker_connections  1024;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  proxy_set_header Host $host;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  
  map $http_upgrade $connection_upgrade {
    default     "upgrade";
  }

  server { 
    listen       10000 default_server;
    server_name  _;
    client_max_body_size 1000m;
    ignore_invalid_headers off;
    proxy_buffering off;

    location /db/ {
      proxy_pass      http://couchdb-service:5984;
      rewrite ^/db/(.*)$ /$1 break;
    }

    location ~ ^/api/(system|admin|global)/ {
      proxy_pass      http://{{ address }}:4002;
    }

    location /api/ {
      proxy_read_timeout 120s;
      proxy_connect_timeout 120s;
      proxy_send_timeout 120s; 
      proxy_pass      http://{{ address }}:4001;
    }

    location = / {
      proxy_pass      http://{{ address }}:4001;
    }

    location /app_ {
      proxy_pass      http://{{ address }}:4001;
    }

    location /app {
      proxy_pass      http://{{ address }}:4001;
    }

    location /builder {
      proxy_pass      http://{{ address }}:3000;
      rewrite ^/builder(.*)$ /builder/$1 break;
    }

    location /builder/ {
      proxy_pass      http://{{ address }}:3000;

      proxy_http_version  1.1;
      proxy_set_header    Connection          $connection_upgrade;
      proxy_set_header    Upgrade             $http_upgrade;
      proxy_set_header    X-Real-IP           $remote_addr;
      proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    }

    location / {
      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_connect_timeout 300;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      chunked_transfer_encoding off;

      proxy_pass      http://minio-service:9000;
    }

    client_header_timeout 60;
    client_body_timeout   60;
    keepalive_timeout     60;
    gzip                  off;
    gzip_comp_level       4;
  }
}
