user nginx;
worker_processes  1;

events {
  worker_connections  1024;
}

http {
  log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name $host to: $upstream_addr: $request $status upstream_response_time $upstream_response_time msec $msec request_time $request_time';
  include /etc/nginx/mime.types;
  client_max_body_size 100m;

  proxy_read_timeout 300;
  proxy_connect_timeout 300;
  proxy_send_timeout 300;
  proxy_buffer_size 8m;
  proxy_busy_buffers_size 12m;
  proxy_buffers 16 1m;
  uwsgi_buffers 16 1m;
  uwsgi_buffer_size 8m;
  uwsgi_busy_buffers_size 12m;

  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  upstream backend {
    server backend:8000;
  }
  upstream batcher {
    server batcher:8080;
  }
  upstream rabbitmq {
    server rabbitmq:15672;
  }
  upstream bot {
    server bot:7313;
  }

  server {
    access_log /var/log/nginx/access.log upstreamlog;
    error_log /var/log/nginx/error.log;
    listen 80;
    charset utf-8;

    root /dist/;
    index index.html;

    # frontend
    location / {
      try_files $uri $uri/ @rewrites;
    }

    location @rewrites {
      rewrite ^(.+)$ /index.html last;
    }

    # batcher
    location ^~ /api/v1/batcher {
      rewrite ^(/api/v1)/batcher/(.+)$ $1/$2 break;
      proxy_pass http://batcher;
    }

    location ^~ /api/internal/ {
        deny all;
    }

    # backend
    location ~ ^/(admin|api) {
      proxy_http_version 1.1;
      proxy_pass http://backend;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Upgrade $http_upgrade;
      proxy_cache_bypass $http_upgrade;
    }

    # bot
    location ^~ /bot {
      proxy_http_version 1.1;
      proxy_pass http://bot;
    }

    # backend static
    location ~ ^/(static|media)/(.*)$ {
      alias /$1/$2;
    }


    # Some basic cache-control for static files to be sent to the browser
    location ~* \.(?:ico|css|js|gif|jpe?g|png|webp)$ {
      expires max;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location ~ ^/rabbitmq/api/(.*?)/(.*) {
        proxy_pass http://rabbitmq/api/$1/%2F/$2?$query_string;
        proxy_buffering                    off;
        proxy_set_header Host              $http_host;
        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;
    }

    location ^~ /rabbitmq {
        rewrite ^/rabbitmq/(.*)$ /$1 break;
        proxy_pass http://rabbitmq;
        proxy_buffering                    off;
        proxy_set_header Host              $http_host;
        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;
    }
  }
}
