map $http_x_forwarded_for $real_ip {
    ~^(\d+\.\d+\.\d+\.\d+) $1;
    default $remote_addr;
}
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}
map $http_x_forwarded_proto $origin_proto {
  default $http_x_forwarded_proto;
  '' $scheme;
}
server {
    listen 80;

    location ~ ^/(mobs|sessions-assets|frontend|static|sourcemaps|ios-images|uxtesting-records)/ {
      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;
      # Default is HTTP/1, keepalive is only enabled in HTTP/1.1
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      chunked_transfer_encoding off;

      proxy_pass http://minio:9000;
    }

    location /minio/ {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host $host;
      proxy_pass http://minio:9000;
    }
    location /ingest/ {
      rewrite ^/ingest/(.*) /$1 break;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header X-Forwarded-For $real_ip;
      proxy_set_header X-Forwarded-Host $real_ip;
      proxy_set_header X-Real-IP $real_ip;
      proxy_set_header Host $host;
      proxy_pass http://http-openreplay:8080;
      proxy_read_timeout 300;
      proxy_connect_timeout 120;
      proxy_send_timeout 300;
      # CORS Headers
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'POST';
      add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization,Content-Encoding';
      add_header 'Access-Control-Expose-Headers' 'Content-Length';
    }

    location /api/ {
      rewrite ^/api/(.*) /$1 break;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Proto $origin_proto;
      proxy_pass http://chalice-openreplay:8000;
    }

    location /assist/ {
      rewrite ^/assist/(.*) /$1 break;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host $host;
      proxy_pass http://peers-openreplay:9000;
    }
    location /ws-assist/ {
      rewrite ^/ws-assist/(.*) /$1 break;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host $host;
      proxy_pass http://assist-openreplay:9001;
    }
    location / {
      index /index.html;
      rewrite ^((?!.(js|css|png|svg|jpg|woff|woff2)).)*$ /index.html break;
      proxy_set_header Host $http_host;
      proxy_pass http://frontend-openreplay:8080;
      proxy_intercept_errors on; # see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
      error_page 404 =200 /index.html;
    }
  }
