apiVersion: v1
kind: ConfigMap
metadata:
  name: query-node-proxy-config
  namespace: default
data:
  nginx.conf: |
    # Increase the number of connections per worker. The default of
    # 512 causes a lot of 502 HTTP errors, and leads nginx to complain
    # that '512 worker_connections are not enough'
    #
    # We now allow 16k connections which is 1/4 of the available port space
    events {
      worker_connections  16384;
    }

    http {
      # See http://nginx.org/en/docs/http/websocket.html
      map $http_upgrade $connection_upgrade {
          default upgrade;
          ''      close;
      }

      # Tune nginx keepalives to work with the GCP HTTP(S) Load Balancer,
      # see point 3 in this blog post for why
      # https://blog.percy.io/tuning-nginx-behind-google-cloud-platform-http-s-load-balancer-305982ddb340
      keepalive_timeout 650;
      keepalive_requests 10000;

      server {
        location ^~ /index-node/ {
          # Remove the /index-node/ again
          rewrite ^/index-node/(.*)$ /$1 break;

          # Proxy configuration.
          proxy_pass http://127.0.0.1:8030;
          proxy_http_version 1.1;
          proxy_set_header Connection $connection_upgrade;
          proxy_set_header Host $host;
          proxy_set_header Upgrade $http_upgrade;
          proxy_cache_bypass $http_upgrade;

          # Gateway timeout.
          proxy_read_timeout 30s;
          proxy_send_timeout 30s;        }

        location /nginx_status {
          stub_status;
          allow 127.0.0.1;
          deny all;
        }

        location / {
          # Move WebSocket and HTTP requests into /ws/ and /http/ prefixes;
          # this allows us to forward both types of requests to different
          # query node ports
          if ( $connection_upgrade = "upgrade" ) {
            rewrite ^(.*)$ /ws/$1;
          }

          if ( $connection_upgrade != "upgrade" ) {
            rewrite ^(.*)$ /http/$1;
          }

          location /http/ {
            # Remove the /http/ again
            rewrite ^/http/(.*)$ $1 break;

            # Proxy configuration.
            proxy_pass http://127.0.0.1:8000;
            proxy_http_version 1.1;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;

            proxy_next_upstream error invalid_header http_502;

            # Gateway timeout.
            proxy_read_timeout 30s;
            proxy_send_timeout 30s;
          }

          location /ws {
            # Remove the /ws/ again
            rewrite ^/ws/(.*)$ $1 break;

            # Proxy configuration.
            proxy_pass http://127.0.0.1:8001;
            proxy_http_version 1.1;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;

            # Gateway timeout.
            proxy_read_timeout 1800s;
            proxy_send_timeout 1800s;
          }
        }
      }

      log_format timed '$remote_addr (forwarded for $http_x_forwarded_for) '
            ' - $remote_user [$time_local] '
            '"$request" $status $request_completion $body_bytes_sent '
            '"$http_referer" "$http_user_agent" '
            '$request_time $upstream_response_time $pipe';

      access_log /var/log/nginx/access.log timed;
      error_log /var/log/nginx/error.log warn;
    }
    stream {}
