upstream settings {
    server 127.0.0.1:8001;
}

server {
    listen          80      default;
    listen          [::]:80 default;

    root /var/www/rtec-board;

    location / {
        index index.html;
    }
    location ~ ^/board/[a-z][a-z0-9-]+ {
        rewrite (.*) /index.html break;
    }
    location /test/ {
        index test/index.html;
    }
    location ~ ^/test/board/[a-z][a-z0-9-]+ {
        rewrite (.*) /test/index.html break;
    }
    location /st/ {
        proxy_pass https://st-api.yandex-team.ru/;
    }
    location /arcanum/ {
        proxy_pass https://a.yandex-team.ru/api/;
    }
    location ~ ^/settings(/|/board/[a-z][a-z0-9-]+) {
        rewrite_by_lua_block {
            -- request auth via tracker API
            ngx.req.set_header("Accept-Encoding", "identity")
            local res = ngx.location.capture("/st/v2/myself")
            if res then
                ngx.req.clear_header('Cookie')
                ngx.req.set_header('X-Board-Authorization', res.body)
            else
                ngx.status = 502
                return ngx.exit(502)
            end
        }
        proxy_pass "http://settings";
    }
    location /ping {
        return 200;
    }
}
