user nginx;

events {
  worker_connections  1024;
}

http {

  map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }
    
  upstream rstudiohost {
    server gcer-docker:8787;
  }
  
  upstream shinyhost {
    server gcer-docker:3838;
  }
  
  access_log  /var/log/nginx/access.log;

  server {
    listen 80;
    
    rewrite ^/shiny$ $scheme://$http_host/shiny/ permanent;
    
    location / {
      proxy_pass http://rstudiohost;
      proxy_redirect http://rstudiohost/ $scheme://$http_host/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_read_timeout 20d;
    }
    
    location /shiny/ {
      rewrite ^/shiny/(.*)$ /$1 break;
      proxy_pass http://shinyhost;
      proxy_redirect / $scheme://$http_host/shiny/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_read_timeout 20d;
      proxy_buffering off;
    }
    
  }
}