# This Nginx config file is optional.
# If you don't have this file, the Nginx config from the workspace-full docker image will be taken.

worker_processes auto;
pid /var/run/nginx/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

env GITPOD_REPO_ROOT;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 3;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;

    gzip_types text/plain text/css text/javascript application/javascript application/json application/transit+json;

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

    # include /etc/nginx/sites-enabled/*;

    server {
        set_by_lua $gitpod_repo_root        'return os.getenv("GITPOD_REPO_ROOT")';

        listen 0.0.0.0:3449 default_server;
        server_name _;

        client_max_body_size 5M;
        charset utf-8;

        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        resolver 8.8.8.8;

        etag off;

        root $gitpod_repo_root/frontend/resources/public;

        location @handle_redirect {
            set $redirect_uri "$upstream_http_location";
            set $redirect_host "$upstream_http_x_host";
            set $redirect_cache_control "$upstream_http_cache_control";

            proxy_buffering off;

            proxy_set_header Host "$redirect_host";
            proxy_hide_header etag;
            proxy_hide_header x-amz-id-2;
            proxy_hide_header x-amz-request-id;
            proxy_hide_header x-amz-meta-server-side-encryption;
            proxy_hide_header x-amz-server-side-encryption;
            proxy_pass $redirect_uri;

            add_header x-internal-redirect "$redirect_uri";
            add_header x-cache-control "$redirect_cache_control";
            add_header cache-control "$redirect_cache_control";
        }

        location /assets {
            proxy_pass http://127.0.0.1:6060/assets;
            recursive_error_pages on;
            proxy_intercept_errors on;
            error_page 301 302 307 = @handle_redirect;
        }

        location /internal/assets {
            internal;
            alias $gitpod_repo_root/backend/assets;
            add_header x-internal-redirect "$upstream_http_x_accel_redirect";
        }

        location /api {
            proxy_pass http://127.0.0.1:6060/api;
        }

        location /webhooks {
            proxy_pass http://127.0.0.1:6060/webhooks;
        }

        location /dbg {
            proxy_pass http://127.0.0.1:6060/dbg;
        }

        location /export {
            proxy_pass http://127.0.0.1:6061;
        }

        location /playground {
            alias $gitpod_repo_root/experiments/;
            add_header Cache-Control "no-cache, max-age=0";
            autoindex on;
        }

        location /ws/notifications {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_pass http://127.0.0.1:6060/ws/notifications;
        }

        location / {
            add_header Cache-Control "no-cache, max-age=0";

            location ~ ^/(/|css|fonts|images|js|wasm) {
            }
            
            location ~ ^/[^/]+/(.*)$ {
                return 301 " /404";
            }

            try_files $uri /index.html$is_args$args =404;
        }
    }
}
