{# NB(dhruv/nikhil): We intentionally turn logs off since they've caused issues at volume with certain nodes. #}
access_log off;
error_log /dev/null;

{# NB(dhruv/nikhil): /dev/shm is memory backed at runtime in our deployed environments, less dependencies on overlayfs #}
client_body_temp_path /dev/shm/nginx_client_temp;
proxy_temp_path /dev/shm/nginx_proxy_temp;
fastcgi_temp_path /dev/shm/nginx_fastcgi_temp;
uwsgi_temp_path /dev/shm/nginx_uwsgi_temp;
scgi_temp_path /dev/shm/nginx_scgi_temp;

{# NB(nikhil): Increase the buffer size to 4Mb, to reduce the chances of spilling over to disk #}
client_body_buffer_size 4M;

map $http_upgrade $upgrade_header {
    default        $http_upgrade;
    ''             '';
}

map $http_connection $connection_header {
    default        $http_connection;
    ''             '';
}

server {
    # We use the proxy_read_timeout directive here (instead of proxy_send_timeout) as it sets the timeout for reading a response from the proxied server vs. setting a timeout for sending a request to the proxied server.

    {% if transport_kind != "grpc" %}
    listen 8080;
    {% else %}
    {# Use deprecated http2 setting on listen for gRPC. #}
    {# TODO Unfortunately it seems the proper 'http2 on' does not work with our use-case, likely because NGINX is confused that we don't have SSL enabled. #}
    listen 8080 http2;
    {% endif %}

    client_max_body_size {{client_max_body_size}};
    proxy_buffering off;

    # Liveness endpoint override
    location = / {
        proxy_redirect off;
        proxy_read_timeout 18030s;
        proxy_http_version 1.1;

        rewrite ^/$ {{liveness_endpoint}} break;

        proxy_pass http://127.0.0.1:{{server_port}};
    }
    # Readiness endpoint override
    location ~ ^/v1/models/model$ {
        proxy_redirect off;
        proxy_read_timeout 18000s;
        proxy_http_version 1.1;

        rewrite ^/v1/models/model$ {{readiness_endpoint}} break;

        proxy_pass http://127.0.0.1:{{server_port}};
    }
    # Predict
    location ~ ^/v1/models/model:predict$ {
        proxy_redirect off;
        proxy_read_timeout 18030s;
        proxy_http_version 1.1;

        rewrite ^/v1/models/model:predict$ {{server_endpoint}} break;

        proxy_pass http://127.0.0.1:{{server_port}};
    }

    location ~ ^/v1/websocket$ {
        proxy_redirect off;
        proxy_read_timeout 18030s;
        proxy_http_version 1.1;

        proxy_set_header Upgrade $upgrade_header;
        proxy_set_header Connection $connection_header;

        rewrite ^/v1/websocket$ {{server_endpoint}} break;

        proxy_pass http://127.0.0.1:{{server_port}};
    }

    # Forward all other paths
    location / {
        proxy_redirect off;
        proxy_read_timeout 18030s;

        {% if transport_kind != "grpc" %}
        {# gRPC requires HTTP2, other protocols will expect 1.1. #}
        proxy_http_version 1.1;
        {% endif %}

        proxy_set_header Upgrade $upgrade_header;
        proxy_set_header Connection $connection_header;

        {% if transport_kind != "grpc" %}
        proxy_pass http://127.0.0.1:{{server_port}};
        {% else %}
        grpc_pass grpc://127.0.0.1:{{server_port}};
        {% endif %}
    }
}
