server {
  listen       80;
  server_name  localhost 127.0.0.1;

  root /usr/share/nginx/html;
  index index.html;

  resolver $NAMESERVER;

  # We must do this indirection to prevent Nginx from forever caching the IP for our API endpoint.
  # See https://distinctplace.com/2017/04/19/nginx-resolver-explained/ .

  set $robot_info_endpoint https://ki-robot-info.zooxlabs.com;
  location /api/robot-info/ {
    rewrite ^/api/robot-info/(.*) /$1 break;
    proxy_pass $robot_info_endpoint;
  }

  set $fleetdash_gateway_endpoint $FLEETDASH_GATEWAY_API_ENDPOINT;
  location /api/fleetdash-gateway/ {
    rewrite ^/api/fleetdash-gateway/(.*) /$1 break;
    proxy_pass $fleetdash_gateway_endpoint;
  }

  set $admin_gateway_beta https://admin-gateway-beta.zooxlabs.com;
  location /api/admin-gateway-beta/ {
    rewrite ^/api/admin-gateway-beta/(.*) /$1 break;
    proxy_pass $admin_gateway_beta;
  }

  set $admin_gateway_dev https://admin-gateway.dev.maestro.zooxlabs.com;
  location /api/admin-gateway-dev/ {
    rewrite ^/api/admin-gateway-dev/(.*) /$1 break;
    proxy_pass $admin_gateway_dev;
  }

  set $admin_gateway_stage https://admin-gateway-stage.zooxlabs.com;
  location /api/admin-gateway-stage/ {
    rewrite ^/api/admin-gateway-stage/(.*) /$1 break;
    proxy_pass $admin_gateway_stage;
  }

  set $admin_gateway_zip https://admin-gateway-zip.zooxlabs.com;
  location /api/admin-gateway-zip/ {
    rewrite ^/api/admin-gateway-zip/(.*) /$1 break;
    proxy_pass $admin_gateway_zip;
  }

  set $samsara_endpoint https://api.samsara.com;
  location /api/samsara/ {
    proxy_ssl_server_name on;
    rewrite ^/api/samsara/(.*) /$1 break;
    proxy_pass $samsara_endpoint;
  }

  set $zrn_overlay_api https://zrn-overlay-api.zooxlabs.com;
  location /api/zrn-overlay/ {
    rewrite ^/api/zrn-overlay/(.*) /$1 break;
    proxy_pass $zrn_overlay_api;
  }

  set $health_api $HEALTH_API;
  location /api/health/ {
    rewrite ^/api/health/(.*) /$1 break;
    proxy_pass $health_api;
  }

  set $allocation_api $ALLOCATION_API_ENDPOINT;
  location /api/allocation/ {
    rewrite ^/api/allocation/(.*) /$1 break;
    proxy_pass $allocation_api;
  }

  set $ralph_api https://ralph.zooxlabs.com;
  location /api/ralph/ {
    rewrite ^/api/ralph/(.*) /$1 break;
    proxy_pass $ralph_api;
  }

  set $predefined_missions_api https://predefined-missions.zooxlabs.com;
  location /api/predefined-missions/ {
    rewrite ^/api/predefined-missions/(.*) /$1 break;
    proxy_pass $predefined_missions_api;
  }

  location / {
    try_files $uri $uri/ /index.html;
  }

  location = /index.html {
    # Must revalidate cache.
    add_header Cache-Control max-age=0;
  }

  location ~ ^/(css|js)/ {
    # Do not try_files for /index.html.
    # Cache as long as possible.
    add_header Cache-Control "public, max-age=31536000";
  }

  access_log /dev/stdout;
}
