# Load-balanced servers
upstream visaweb {
  server visa-primary.server.com:443;
  server visa-secondary.server.com:443;
}

server {
  # HTTP configuration
  listen 80;

  server_name visa.server.com;

  location / {
    return 301 https://$host$request_uri;
  }
}

server {
  # HTTPS configuration
  listen 443 ssl;

  server_name visa.server.com;

  ssl on;
  ssl_certificate /path/to/visa-cert.crt;
  ssl_certificate_key /path/to/visa-key.key;
  # Logs
  access_log /var/log/nginx/access-visa.log;
  error_log /var/log/nginx/error-visa.log;
  
  location / {
    proxy_pass https://visaweb/;
  }

  gzip on;
  gzip_proxied any;
  gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
  gzip_vary on;

  # Remote desktop connections
  location /ws/vdi {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass https://visaweb/ws/vdi;
    proxy_cache off;
  }

  # Rest API Server
  location /api {
    proxy_pass       https://visaweb/api;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
  }

  # Jupyter HTTP requests
  location /jupyter {
    client_max_body_size 2M;
    proxy_pass       https://visaweb/jupyter;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
  }

  # Jupyter websocket requests
  location ~* /jupyter/(\d+)/(api/kernels/[^/]+/(channels|iopub|shell|stdin)|terminals/websocket)/? {
    proxy_set_header Upgrade "websocket";
    proxy_set_header Connection "Upgrade";
    proxy_http_version 1.1;
    proxy_redirect   off;
    proxy_pass       https://visaweb;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_cache      off;
  }

}