worker_processes  5;
daemon off;
error_log  /var/log/nginx/error.log;
worker_rlimit_nofile 8192;
pcre_jit on;

events {
  worker_connections  4096;
}

http {
  include    mime.types;
  index    index.html;
  # The JSONRPC POST body must fit inside this allocation for the method parsing to succeed.
  # https://github.com/openresty/lua-nginx-module#ngxreqread_body
  # http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size
  client_body_buffer_size 128k;
  # client_max_body_size should match client_body_buffer_size
  # Values that exceed client_body_buffer_size will be written to a temporary file, which we don't want
  # Requests above this limit will also be denied with an HTTP 413 response (entity too large)
  # http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
  client_max_body_size 128k;

  # See Move default writable paths to a dedicated directory (#119)
  # https://github.com/openresty/docker-openresty/issues/119
  client_body_temp_path /var/run/openresty/nginx-client-body;
  proxy_temp_path       /var/run/openresty/nginx-proxy;
  fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;
  uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;
  scgi_temp_path        /var/run/openresty/nginx-scgi;

  keepalive_timeout  0;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log   /var/log/nginx/access.log  main;
  sendfile     on;
  tcp_nopush   on;

  lua_shared_dict prometheus_metrics 10M;
  init_worker_by_lua_block {
    prometheus = require("prometheus").init("prometheus_metrics")
    metric_requests = prometheus:counter(
      "nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
    metric_sequencer_requests = prometheus:counter(
      "nginx_eth_sequencer_requests", "Number of requests going to the sequencer", {"method", "host", "status"})
    metric_replica_requests = prometheus:counter(
      "nginx_eth_replica_requests", "Number of requests going to the replicas", {"host", "status"})
    metric_latency = prometheus:histogram(
      "nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
    metric_connections = prometheus:gauge(
      "nginx_http_connections", "Number of HTTP connections", {"state"})
  }
  log_by_lua_block {
    metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
    metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
  }

  upstream sequencer {
    server {{env.Getenv "SEQUENCER"}};
  }

  server {  # RPC proxy server
    listen       8080;
    location = /healthz {
      return 200 'healthz';
    }
    location / {
      set $jsonrpc_whitelist {{env.Getenv "ETH_CALLS_ALLOWED"}};
      if ($request_method = POST) {
        access_by_lua_file 'eth-jsonrpc-access.lua';
      }
      proxy_pass http://sequencer;
    }
  }

  server {  # Metrics server
    listen 9145;
    location /metrics {
      content_by_lua_block {
        metric_connections:set(ngx.var.connections_reading, {"reading"})
        metric_connections:set(ngx.var.connections_waiting, {"waiting"})
        metric_connections:set(ngx.var.connections_writing, {"writing"})
        prometheus:collect()
      }
    }
  }

}