basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ConfigHomelabnginx

    Large uploads, S3/MinIO, and WebDAV in nginx: zero disk buffering, SigV4, and Immich/Nextcloud

    Line-by-line nginx configs for multi-gigabyte uploads, S3 storage (MinIO), and self-hosted cloud apps (Immich, Nextcloud): streaming without temp disk files, exact Host preservation for SigV4 signatures, long timeouts, and WebDAV redirects.

    Why a standard reverse proxy breaks large uploads

    The default config in Reverse proxy targets short JSON payloads and web pages, so multi-gigabyte uploads run straight into four built-in limits:

    • client_max_body_size 1m — any upload over 1 MB fails immediately with 413 (client_max_body_size limit).
    • proxy_request_buffering on — instead of streaming to the backend, nginx writes the entire file to /var/cache/nginx/client_temp first. A 10 GB upload eats 10 GB of temp space, doubles transfer time, and often times out on the client while nginx copies the file from disk to the backend.
    • proxy_buffering on — the same behavior in reverse: large downloads spill into /var/cache/nginx/proxy_temp whenever the client reads slower than the backend.
    • 60s timeouts — proxy_read_timeout, proxy_send_timeout, and send_timeout kill slow uploads or video processing right at the 60-second mark.
    Service Body limit Buffering Protocol specifics
    S3 / MinIO API 0 (no nginx cap) proxy_request_buffering off and proxy_buffering off Host $http_host for SigV4, ignore_invalid_headers off
    Immich (photos & 4K video) 50000m (50 GB) proxy_request_buffering off WebSocket for live feed, 600s timeouts
    Nextcloud (cloud & WebDAV) 10g or 0 proxy_request_buffering off /.well-known/carddav and /.well-known/caldav redirects

    Scenario 1: S3 storage (MinIO API and web console)

    MinIO listens on two ports: 9000 for the S3 API (rclone, Kopia, backup tools) and 9001 for the web console. Put them on separate subdomains (s3.example.org and console.s3.example.org) so you can lock the console behind an IP allowlist or VPN (Limits & access) without blocking S3 clients.

    /etc/nginx/conf.d/s3.example.org.conf
    server {
        listen 443 ssl;
        http2 on;
        server_name s3.example.org;
    
        # ssl_certificate / ssl_certificate_key / snippets/tls.conf
    
        ignore_invalid_headers off;
        client_max_body_size 0;
        proxy_buffering off;
        proxy_request_buffering off;
    Key part

    ignore_invalid_headers off lets custom S3 headers and underscore metadata names through. client_max_body_size 0 lifts nginx’s body cap (object quotas stay enforced by MinIO itself). Disabling both buffering modes streams uploads and downloads past local temp files.

    default:ignore_invalid_headers onclient_max_body_size 1mproxy_buffering onproxy_request_buffering on

        location / {
            proxy_http_version 1.1;
            proxy_set_header Host $http_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_set_header Connection "";
    
            proxy_connect_timeout 300s;
            proxy_send_timeout    600s;
            proxy_read_timeout    600s;
            chunked_transfer_encoding off;
    
            proxy_pass http://127.0.0.1:9000;
        }
    }
    Key part

    The key line for S3 is Host $http_host instead of $host: AWS SigV4 signs the client’s exact Host header including port, and any rewrite breaks verification (403 SignatureDoesNotMatch). Connection "" with proxy_http_version 1.1 keeps backend connections reusable, and MinIO recommends chunked_transfer_encoding off for compatibility with older S3 clients.

    server {
        listen 443 ssl;
        http2 on;
        server_name console.s3.example.org;
    
        # ssl_certificate / ssl_certificate_key / snippets/tls.conf
    
        ignore_invalid_headers off;
        client_max_body_size 0;
        proxy_buffering off;
        proxy_request_buffering off;
    
        location / {
            proxy_http_version 1.1;
            proxy_set_header Host $http_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_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
    
            proxy_connect_timeout 300s;
            proxy_send_timeout    600s;
            proxy_read_timeout    600s;
            proxy_pass http://127.0.0.1:9001;
        }
    }

    Dedicated block for the MinIO web console (9001): the UI uses WebSocket, so wire in $connection_upgrade from your map $http_upgrade table (Reverse proxy). In production, restrict this block with allow/deny or a VPN.

    Scenario 2: media server and personal cloud (Immich, Nextcloud, and WebDAV)

    This block covers the two most common self-hosted large-file setups: Immich photo/video library (port 2283, 4K video, and WebSocket) and containerized Nextcloud (WebDAV plus contacts/calendar sync). Drop the /.well-known/* lines for Immich; keep them for Nextcloud.

    /etc/nginx/conf.d/cloud.example.org.conf
    server {
        listen 443 ssl;
        http2 on;
        server_name cloud.example.org;
    
        # ssl_certificate / ssl_certificate_key / snippets/tls.conf
    
        client_max_body_size 50000m;
        client_body_buffer_size 1024k;
        proxy_request_buffering off;
    
        proxy_read_timeout 600s;
        proxy_send_timeout 600s;
        send_timeout       600s;
    Key part

    Raise the body size cap (Immich’s docs set 50000m; Nextcloud typically uses 10g or 0) and disable disk buffering on incoming uploads. 600s timeouts allow 10 minutes for heavy video uploads and backend responses.

    default:client_max_body_size 1mproxy_request_buffering onproxy_read_timeout 60s

        location = /.well-known/carddav { return 301 $scheme://$http_host/remote.php/dav; }
        location = /.well-known/caldav  { return 301 $scheme://$http_host/remote.php/dav; }

    Service discovery redirects for CardDAV and CalDAV clients (Nextcloud, Baikal, Radicale). Without them, phones and mail clients won’t auto-detect address books or calendars. Omit both lines if nginx fronts only Immich.

        location / {
            include snippets/proxy-headers.conf;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_max_temp_file_size 0;
            proxy_redirect off;
    
            proxy_pass http://127.0.0.1:2283;
        }
    }
    Key part

    Proxy to the container with the shared headers snippet and safe WebSocket upgrades via $connection_upgrade (Reverse proxy). proxy_max_temp_file_size 0 stops large downloads from spilling into temp disk files even while proxy_buffering stays enabled for normal pages.

    default:proxy_max_temp_file_size 1024m

    How to check

    1. Validate syntax and apply:
      sudo nginx -t && sudo systemctl reload nginx
    2. Test streaming an upload larger than the default 1m (for instance, a 5 MB test file) and confirm error.log stays free of a client request body is buffered to a temporary file:
      dd if=/dev/zero of=/tmp/test5m.bin bs=1M count=5 status=none
      curl -sS -o /dev/null -w "%{http_code}\n" -X PUT --data-binary @/tmp/test5m.bin \
        https://s3.example.org/test-bucket/test5m.bin
      rm -f /tmp/test5m.bin
      sudo grep 'buffered to a temporary file' /var/log/nginx/error.log | tail -n 5
      # empty output — the file streamed straight past disk
    3. For Nextcloud, verify the WebDAV discovery redirects:
      curl -sS -I https://cloud.example.org/.well-known/carddav | grep -iE '^(HTTP/|location:)'
      # HTTP/2 301
      # location: https://cloud.example.org/remote.php/dav

    If it doesn’t work

    An S3 client (rclone, AWS CLI, backup tool) gets 403 SignatureDoesNotMatch through nginx, while connecting directly to port 9000 works

    Cause
    AWS Signature Version 4 (SigV4) signs the exact Host header including any port the client sent. proxy_set_header Host $host; strips the port and lowercases the value, breaking the signature.
    Fix
    Forward the client’s raw Host header in the S3/MinIO block: proxy_set_header Host $http_host;.

    Uploading a 4K video or backup suddenly fills the root disk, and error.log logs a client request body is buffered to a temporary file

    Cause
    proxy_request_buffering on is the default: nginx writes the entire upload to /var/cache/nginx/client_temp before sending a single byte to the backend.
    Fix
    Enable streaming uploads in that service’s server or location block: proxy_request_buffering off;.

    Even with client_max_body_size 0; in nginx, uploads larger than 100 MB still fail with 413 Request Entity Too Large

    Cause
    Cloudflare sits in front of nginx on the Free plan (hard 100 MB per-request cap) and returns 413 before the request ever reaches your server.
    Fix
    Switch the S3 or media hostname to Cloudflare DNS-only (grey cloud), or enable chunked / multipart uploads on the client side (Nextcloud, rclone).

    A large video or archive upload cuts off at exactly one minute with 504 Gateway Timeout or 499

    Cause
    The default 60-second proxy_read_timeout or proxy_send_timeout expired while the backend was receiving the stream or processing the file.
    Fix
    Raise proxy_read_timeout 600s;, proxy_send_timeout 600s;, and send_timeout 600s; inside that service’s block.

    Mobile clients and Thunderbird fail to discover Nextcloud contacts and calendars behind nginx

    Cause
    CardDAV and CalDAV clients look up /.well-known/carddav and /.well-known/caldav first; without explicit redirects, requests never reach /remote.php/dav.
    Fix
    Add exact blocks location = /.well-known/carddav and location = /.well-known/caldav returning return 301 $scheme://$http_host/remote.php/dav;.

    An S3 client loses custom metadata headers containing underscores (such as X-Amz-Meta-Backup_Id)

    Cause
    ignore_invalid_headers on (the default) silently drops headers whose names contain underscores or non-standard characters.
    Fix
    Add ignore_invalid_headers off; inside the S3 server block.