worker_processes 1;
error_log  /logs/error.log;
user root root;
events {
    worker_connections  1000;
    multi_accept on;
}
worker_rlimit_nofile    20000;
http {
    root /var/www/;
    upstream sproxyds {
        least_conn;
        keepalive 40;
        server 127.0.0.1:20000;
    }

    # Define a custom log format
    log_format irm '{ "time":"$now","connection":"$connection","request":"$connection_requests","hrtime":"$msec",'
                    '"httpMethod":"$request_method","httpURL":"$uri","elapsed_ms":$elapsed_ms,'
                    '"httpCode":$status,"requestLength":$request_length,"bytesSent":$bytes_sent,'
                    '"contentLength":"$content_length","sentContentLength":"$sent_http_content_length",'
                    '"contentType":"$content_type","s3Address":"$remote_addr",'
                    '"requestUserMd":"$usermd","responseUserMd":"$sentusermd",'
                    '"ringKeyVersion":"$sent_http_x_scal_version","ringStatus":"$sent_http_x_scal_ring_status",'
                    '"s3Port":"$remote_port","sproxydStatus":"$upstream_status","req_id":"$http_x_scal_request_uids",'
                    '"ifMatch":"$http_if_match","ifNoneMatch":"$http_if_none_match",'
                    '"range":"$http_range","contentRange":"$sent_http_content_range","nginxPID":$PID,'
                    '"sproxydAddress":"$upstream_addr","sproxydResponseTime_s":"$upstream_response_time" }';

    server {
        client_max_body_size 0;
        client_body_timeout 150;
        client_header_timeout 150;
        postpone_output 0;
        keepalive_requests 1100;
        keepalive_timeout 300s;
        server_tokens off;
        default_type application/octet-stream;
        gzip off;
        tcp_nodelay on;
        tcp_nopush  on;
        sendfile on;
        listen 81 reuseport;
        server_name localhost;
        rewrite ^/arc/(.*)$ /dc1/$1 permanent;
        location ~* ^/proxy/(.*)$ {
            rewrite ^/proxy/(.*)$ /$1 last;
        }
        allow 127.0.0.1;

        deny all;
        set $usermd '-';
        set $sentusermd '-';
        set $elapsed_ms '-';
        set $now '-';
        log_by_lua '
            if not(ngx.var.http_x_scal_usermd == nil) and  string.len(ngx.var.http_x_scal_usermd) > 2 then
                ngx.var.usermd = string.sub(ngx.decode_base64(ngx.var.http_x_scal_usermd),1,-3)
            end
                    if not(ngx.var.sent_http_x_scal_usermd == nil) and string.len(ngx.var.sent_http_x_scal_usermd) > 2 then
                            ngx.var.sentusermd = string.sub(ngx.decode_base64(ngx.var.sent_http_x_scal_usermd),1,-3)
                    end
            local elapsed_ms = tonumber(ngx.var.request_time)
            if not (  elapsed_ms == nil) then
                elapsed_ms = elapsed_ms * 1000
                ngx.var.elapsed_ms = tostring(elapsed_ms)
            end
            local time = tonumber(ngx.var.msec) * 1000
            ngx.var.now = time
        ';
        access_log /dev/stdout irm;
        error_log  /dev/stdout error;
        location / {
            proxy_buffering off;
            proxy_request_buffering off;
            # proxy_http_version 1.1; Option disabled as it affects performance cf citadel design
            fastcgi_request_buffering off;
            fastcgi_no_cache 1;
            fastcgi_cache_bypass 1;
            fastcgi_buffering off;
            fastcgi_ignore_client_abort on;
            fastcgi_keep_conn on;
            include /conf/fastcgi_params;
            fastcgi_pass sproxyds;
            fastcgi_next_upstream error timeout;
            fastcgi_send_timeout 285s;
            fastcgi_read_timeout 285s;
        }
    }
}

