worker_processes auto;
error_log /var/logs/nginx/error.log debug;
pid /var/run/nginx.pid;

events {
  worker_connections 1024;
  use epoll;
  multi_accept on;
}

http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/logs/nginx/access.log main;

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  gzip on;
  gzip_disable "msie6";
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;

  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    client_max_body_size 0;


    # Handle /pacs requests and rewrite them to the correct dcm4chee-arc UI path
    # This allows accessing the dcm4chee-arc UI through the /pacs URL
    location /pacs {
      rewrite ^/pacs(.*)$ /dcm4chee-arc/ui2$1 break;
      proxy_pass http://arc:8080;
      proxy_http_version 1.1;
      proxy_set_header Host $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;
      proxy_buffering off;
      proxy_request_buffering off;
      expires 0;
      add_header Cache-Control private;
    }

    # Proxy all dcm4chee-arc requests
    # This block handles all API requests and general dcm4chee-arc paths
    location /dcm4chee-arc/ {
      proxy_pass http://arc:8080/dcm4chee-arc/;
      proxy_http_version 1.1;
      proxy_set_header Host $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;
      proxy_buffering off;
      proxy_request_buffering off;
    }


    location /sw.js {
      add_header Cache-Control "no-cache";
      proxy_cache_bypass $http_pragma;
      proxy_cache_revalidate on;
      expires off;
      access_log off;
    }

    location / {
      root /var/www/html;
      index index.html;
      try_files $uri $uri/ /index.html;
      add_header Cache-Control "no-store, no-cache, must-revalidate";
      add_header Cross-Origin-Opener-Policy 'same-origin' always;
      add_header Cross-Origin-Embedder-Policy 'require-corp' always;
    }
  }
}
