---
apiVersion: v1
kind: ConfigMap
metadata:
  name: seafile-nginx-config
  namespace: default
data:
  nginx.conf: |
    daemon off;
    user www-data;
    worker_processes auto;

    events {
        worker_connections 768;
    }

    http {
        include /etc/nginx/mime.types;
        server_names_hash_bucket_size 256;
        server_names_hash_max_size 1024;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        log_format seafileformat '$http_x_forwarded_for $remote_addr [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $upstream_response_time';

        access_log /var/log/nginx/access.log seafileformat;
        error_log /var/log/nginx/error.log info;

        gzip on;
        gzip_types  text/plain text/css application/javascript application/json text/javascript;

        include /etc/nginx/conf.d/*.conf;

        server {
          listen 80;

          client_max_body_size 10m;

          location / {
              proxy_pass http://127.0.0.1:8000/;
              proxy_read_timeout 310s;
              proxy_set_header Host $host;
              proxy_set_header Forwarded "for=$remote_addr;proto=$scheme";
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header X-Forwarded-Proto $scheme;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header Connection "";
              proxy_http_version 1.1;

              client_max_body_size 0;
              access_log      /var/log/nginx/seahub.access.log seafileformat;
              error_log       /var/log/nginx/seahub.error.log;
          }

          location /seafhttp {
              rewrite ^/seafhttp(.*)$ $1 break;
              proxy_pass http://127.0.0.1:8082;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              client_max_body_size 0;
              proxy_connect_timeout  36000s;
              proxy_read_timeout  36000s;
              proxy_request_buffering off;
              access_log      /var/log/nginx/seafhttp.access.log seafileformat;
              error_log       /var/log/nginx/seafhttp.error.log;
          }

          location /seafdav {
              proxy_pass         http://127.0.0.1:8080;
              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-Host $server_name;
              proxy_set_header   X-Forwarded-Proto $scheme;
              proxy_read_timeout  1200s;
              client_max_body_size 0;

              access_log      /var/log/nginx/seafdav.access.log seafileformat;
              error_log       /var/log/nginx/seafdav.error.log;

              # Fix for wsgidav https://github.com/mar10/wsgidav/issues/183
              set $destination $http_destination;
              if ($destination ~* ^https(.+)$) {
                set $destination http$1;
              }
              proxy_set_header Destination $destination;
          }

          location /media {
              root /opt/seafile/seafile-server-latest/seahub;
          }

          # For letsencrypt
          location /.well-known/acme-challenge/ {
              alias /var/www/challenges/;
              try_files $uri =404;
          }
        }

        server {
            listen 80;
            location / {
                return 444;
            }
        }
    }