apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    # https://github.com/KyleAMathews/docker-nginx/blob/master/nginx.conf
    # https://linode.com/docs/web-servers/nginx/configure-nginx-for-optimized-performance/
    # https://docs.gunicorn.org/en/stable/deploy.html

    worker_processes 1;

    events {
      worker_connections 2000; # increase if you have lots of clients
      accept_mutex off; # set to 'on' if nginx worker_processes > 1
      use epoll; # Enable epoll for Linux 2.6+
      # 'use kqueue;' to enable for FreeBSD, OSX
    }

    http {
        include mime.types;
        # fallback in case we can't determine a type
        default_type application/octet-stream;
        sendfile on;

        upstream app_server {
          # ip_hash; # For load-balancing
          #
          # fail_timeout=0 means we always retry an upstream even if it failed
          # to return a good HTTP response
          # server unix:/nginx/gunicorn.socket fail_timeout=0;

          # for a TCP configuration
          server localhost:8888 fail_timeout=0;
          keepalive 32;
        }

        server {
            access_log off;
            listen 8000 deferred;
            charset utf-8;
            keepalive_timeout 75s;

            # https://thoughts.t37.net/nginx-optimization-understanding-sendfile-tcp-nodelay-and-tcp-nopush-c55cdd276765
            # tcp_nopush on;
            # tcp_nodelay on;

            gzip             on;
            gzip_min_length 1000;
            gzip_comp_level  2;
            # text/html is always included by default
            gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
            gzip_disable "MSIE [1-6]\.";

            location /static {
                alias /nginx/staticfiles;
                expires 365d;
            }

            location / {
                proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
                proxy_set_header    X-Forwarded-Proto   $scheme;
                proxy_set_header    Host                $host;
                # we don't want nginx trying to do something clever with
                # redirects, we set the Host: header above already.
                proxy_redirect off;
                proxy_pass http://app_server/;

                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
                add_header              Front-End-Https   on;
            }
        }
    }
