log_format  datasetsserver  escape=json
  '{'
    '"message":"$remote_addr - $remote_user [$time_local]'
                  ' \\"$request_method $scheme://$host$request_uri $server_protocol\\" '
                  '$status $body_bytes_sent \\"$http_referer\\" '
                  '\\"$http_user_agent\\" \\"$http_x_forwarded_for\\"",'
    '"remote_addr":"$remote_addr",'
    '"remote_user":"$remote_user",'
    '"time_local":"$time_local",'
    '"request_method":"$request_method",'
    '"scheme":"$scheme",'
    '"host":"$host",'
    '"request_uri":"$request_uri",'
    '"server_protocol":"$server_protocol",'
    '"status":"$status",'
    '"body_bytes_sent":"$body_bytes_sent",'
    '"http_referer":"$http_referer",'
    '"http_user_agent":"$http_user_agent",'
    '"http_x_forwarded_for":"$http_x_forwarded_for",'
    '"request_body":"$request_body"'
  '}';

server {
  listen ${PORT};
  listen [::]:${PORT};
  server_name ${HOST};

  location /openapi.json {
    alias /static-files/openapi.json;
  }

  location /assets/ {
    alias ${ASSETS_DIRECTORY}/;
  }

  location /cached-assets/ {
    alias ${CACHED_ASSETS_DIRECTORY}/;
  }

  location /admin/ {
    # note the trailing slash, to remove the /admin/ prefix
    proxy_pass ${URL_ADMIN}/;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_http_version 1.1;
  }

  location /rows {
    proxy_pass ${URL_ROWS}/rows;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_http_version 1.1;
  }

  location / {
    proxy_pass ${URL_API};
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_http_version 1.1;
  }

  error_page 404 /404.html;
  location = /404.html {
    root /error-pages;
    internal;
  }

  location ~* ^(/admin)?/metrics$ {
    return 404;
  }

  access_log /var/log/nginx/access.log datasetsserver;
}
