worker_processes auto;

events {
    worker_connections 1024;
}

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

    sendfile on;
    keepalive_timeout 65;
    client_max_body_size 500m;

    gzip on;
    gzip_static on;
    gzip_vary on;
    gzip_comp_level 5;
    gzip_min_length 1024;
    gzip_proxied any;
    gzip_types
        application/javascript
        application/json
        application/xml
        application/wasm
        image/svg+xml
        text/css
        text/plain
        text/xml;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    resolver 127.0.0.11 valid=30s ipv6=off;

    server {
        listen 80;
        server_name _;

        location = / {
            return 302 /flow/;
        }

        location = /flow {
            return 301 /flow/;
        }

        location = /flow/api {
            return 301 /flow/api/;
        }

        location = /flow/storage {
            return 301 /flow/storage/;
        }

        location = /flow/api/v1/bot/ws/audio {
            set $easyflow_backend http://backend:8111;
            rewrite ^/flow/(.*)$ /$1 break;
            proxy_pass $easyflow_backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            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_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header X-Forwarded-Prefix /flow;
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
            proxy_redirect off;
        }

        location ^~ /flow/api/ {
            set $easyflow_backend http://backend:8111;
            rewrite ^/flow/api/(.*)$ /api/$1 break;
            proxy_pass $easyflow_backend;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            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_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header X-Forwarded-Prefix /flow;
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
            proxy_buffering off;
            proxy_cache off;
            proxy_redirect off;
        }

        location ^~ /flow/storage/ {
            set $easyflow_minio http://minio-shared:9000;
            rewrite ^/flow/storage/(.*)$ /easyflow/$1 break;
            proxy_pass $easyflow_minio;
            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_redirect off;
        }

        location = /flow/index.html {
            root /usr/share/nginx/html;
            add_header Cache-Control "no-cache, no-store, must-revalidate" always;
        }

        location = /flow/_app.config.js {
            root /usr/share/nginx/html;
            add_header Cache-Control "no-cache, no-store, must-revalidate" always;
        }

        location ~* ^/flow/(?:avif|css|eot|gif|ico|jpeg|jpg|js|jse|png|svg|ttf|wasm|webp|woff|woff2)/ {
            root /usr/share/nginx/html;
            try_files $uri =404;
            add_header Cache-Control "public, max-age=31536000, immutable" always;
        }

        location /flow/ {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /flow/index.html;
            add_header Cache-Control "no-cache" always;
        }
    }
}
