# Local config to run orthanc and development UI on the same domain
# Uses port 9999

# `events` section is mandatory
events {
  worker_connections 1024; # Default: 1024
}

http {

  # prevent nginx sync issues on OSX
  proxy_buffering off;
  include /etc/nginx/mime.types;

  server {  # on port 9999: real time "run dev" server
    listen 9999 default_server;
    client_max_body_size 4G;
    access_log logs/nginx-access.log;
    error_log logs/nginx-error.log;

    # set $orthanc http://192.168.0.8:8042;
    set $orthanc 'http://127.0.0.1:8043';

    # Orthanc when accessed from the 'run dev' server
    location ~* ^/(patients|studies|instances|series|plugins|system|tools|statistics|modalities|dicom-web|osimis-viewer|ohif|stone-webviewer|peers|jobs|transfers|queries|auth|app|volview|changes|wsi) {

      proxy_set_header HOST $host;
      proxy_set_header X-Real-IP $remote_addr;

      proxy_pass $orthanc;
    }

    location /ui/api/ {
      proxy_set_header HOST $host;
      proxy_set_header X-Real-IP $remote_addr;

      #rewrite (.*) /ui$1 break;
      proxy_pass $orthanc;
    }

    location /ui/app/customizable/ {
      proxy_set_header HOST $host;
      proxy_set_header X-Real-IP $remote_addr;

      #rewrite (.*) /ui$1 break;
      proxy_pass $orthanc;
    }

    # Frontend development server
    location /ui/app/ {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header HOST $host;
      rewrite /ui/app(.*) $1 break;

      proxy_pass http://127.0.0.1:3000;
    }

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header HOST $host;

      proxy_pass http://127.0.0.1:3000;
    }

    location /orthanc/ {
      # URL to open in your browser: http://localhost:9999/orthanc/ui/

      proxy_pass $orthanc;
      rewrite /orthanc(.*) $1 break;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      
      proxy_request_buffering off;
      proxy_max_temp_file_size 0;
      client_max_body_size 0;
    }

  }
}
