apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    user nginx;
    worker_processes auto;

    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;

    events {
      worker_connections 10240;
      accept_mutex_delay 100ms;
    }

    http {
      default_type application/octet-stream;

      log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

      sendfile on;
      open_file_cache max=100 inactive=20s;
      tcp_nopush on;

      keepalive_timeout 65;

      map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
      }

      server {
        listen 80;
        listen [::]:80;

        location / {
          try_files $uri @proxy;
        }

        location ~ ^/(emoji|packs) {
          add_header Cache-Control "immutable, max-age=31536000, public";
          try_files $uri @proxy;
        }

        location /sw.js {
          add_header Cache-Control "max-age=0";
          try_files $uri @proxy;
        }

        location @proxy {
          proxy_set_header Host $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 https;
          proxy_set_header Proxy "";
          proxy_pass_header Server;

          proxy_pass http://mastodon-web:3000;
          proxy_buffering on;
          proxy_redirect off;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;

          tcp_nodelay on;
        }

        location /healthz {
          access_log off;
          return 200 "OK\n";
        }
      }
    }
