server {
    listen 80;
    server_name _;
    absolute_redirect off;

    client_max_body_size 2g;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript application/xml image/svg+xml;

    location = /__auth {
        internal;
        proxy_pass http://skg-marketing-api:4291/auth/check;
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Original-URI $request_uri;
        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;
    }

    location /api/auth/ {
        proxy_pass http://skg-marketing-api:4291/auth/;
        proxy_http_version 1.1;
        proxy_request_buffering off;
        proxy_buffering off;
        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_read_timeout 60s;
        proxy_send_timeout 60s;
        proxy_connect_timeout 60s;
    }

    location = /api/auth/login {
        proxy_pass http://skg-marketing-api:4291/auth/login;
        proxy_http_version 1.1;
        proxy_request_buffering off;
        proxy_buffering off;
        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_read_timeout 60s;
        proxy_send_timeout 60s;
        proxy_connect_timeout 60s;
    }

    location = /api/auth/logout {
        proxy_pass http://skg-marketing-api:4291/auth/logout;
        proxy_http_version 1.1;
        proxy_request_buffering off;
        proxy_buffering off;
        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_read_timeout 60s;
        proxy_send_timeout 60s;
        proxy_connect_timeout 60s;
    }

    location @api_unauthorized {
        default_type application/json;
        return 401 '{"error":"unauthorized"}';
    }

    location = /api {
        return 308 /api/;
    }

    location /api/ {
        auth_request /__auth;
        error_page 401 = @api_unauthorized;
        proxy_pass http://skg-marketing-api:4291/;
        proxy_http_version 1.1;
        proxy_request_buffering off;
        proxy_buffering off;
        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_read_timeout 1800s;
        proxy_send_timeout 1800s;
        proxy_connect_timeout 60s;
    }

    location = /login {
        return 308 /login/;
    }

    location /login/ {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /login/index.html;
    }

    location /_next/ {
        root /usr/share/nginx/html;
        try_files $uri =404;
    }

    location /assets/ {
        root /usr/share/nginx/html;
        try_files $uri =404;
    }

    location = /canvas {
        return 308 /;
    }

    location ~ ^/canvas/(.*)$ {
        return 308 /$1$is_args$args;
    }

    location = /skg-logo-black.svg {
        root /usr/share/nginx/html;
        try_files $uri =404;
    }

    location /oasis-source/ {
        root /usr/share/nginx/html;
        try_files $uri =404;
    }

    location ~* ^/(icon|apple-icon|favicon|manifest|placeholder).* {
        root /usr/share/nginx/html;
        try_files $uri =404;
    }

    location @login_redirect {
        return 302 /login/?next=$request_uri;
    }

    location / {
        auth_request /__auth;
        error_page 401 = @login_redirect;
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
    }
}
