load_module modules/ngx_http_module.so;

worker_processes 1;
daemon off;

error_log stderr;
events {
    worker_connections 1024;
}

pid /tmp/nginx.pid;

http {
    opentracing on;

    opentracing_load_tracer instana/libinstana_sensor.so instana/instana-config.json;

    charset utf-8;
    log_format cloudfoundry 'NginxLog "$request" $status $body_bytes_sent';
    access_log /dev/stdout cloudfoundry;
    default_type application/octet-stream;
    sendfile on;

    tcp_nopush on;
    keepalive_timeout 30;
    port_in_redirect off;

    # Calculate CF app names reusing the CF apps domain as seen by the client
    map $server_name $domain {
        ~^[^\.]*(.*)$ $1;
    }

    server {
        listen       {{port}};
        server_name ~^(?<name>\w+)\.(?<domain>.*)$;

        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   /usr/share/nginx/html;
#        }

        location ~ ^/api/([a-zA-Z]+)/(.+)$ {
            # We need a resolver, since we need to use variables, so we need to specify a DNS server.
            # Google, HALP!
            resolver 8.8.8.8;

            proxy_pass https://$1.$domain/$2;

            opentracing_trace_locations off;
            opentracing_propagate_context;
            opentracing_tag "resource.name" $1/$2;
            opentracing_tag "endpoint" $1;
        }

        location /nginx_status {
            stub_status on;
            access_log off;
        }

        location / {
            root   static;
            index  index.html;
            ssi    on;

            include mime.types;

            opentracing_trace_locations off;
            opentracing_propagate_context;
            opentracing_tag "resource.name" "static_resources";

            add_header "server-timing" "intid;desc=${opentracing_context_x_instana_t}";
        }

    }
}