# Package default site (HTTPS by default, #324). The certificate is the host-local one the package creates with
# `vh setup local-tls` (self-signed; renewed automatically). `sudo vh setup nginx` regenerates this site from the
# runtime config; `--certbot` switches to a publicly trusted certificate.
# Access logs never record a share token, a file name or a query string (#327): old /share/<token> links and
# every ?query are replaced before logging, and Referer is not logged. (error_log lines can still quote a
# request line; they are written only on errors.)
map $request_uri $vaulthalla_log_uri {
    ~^/share/                "/share/[redacted]";
    ~^(?<vh_log_path>[^?]*)\?  "$vh_log_path?[redacted]";
    default                  $request_uri;
}
log_format vaulthalla '$remote_addr - $remote_user [$time_local] "$request_method $vaulthalla_log_uri $server_protocol" $status $body_bytes_sent "-" "$http_user_agent"';

server {
    access_log /var/log/nginx/access.log vaulthalla;
    listen 80;
    listen [::]:80;
    server_name _;
    return 308 https://$host$request_uri;
}

server {
    access_log /var/log/nginx/access.log vaulthalla;
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name _;

    ssl_certificate /etc/vaulthalla/tls/local.crt;
    ssl_certificate_key /etc/vaulthalla/tls/local.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;

    # Response headers (#324). No HSTS: this certificate is self-signed, and HSTS would make browsers refuse the
    # click-through. `vh setup nginx --certbot` adds it with a publicly trusted certificate.
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer" always;
    add_header X-Frame-Options "DENY" always;
    add_header Content-Security-Policy "frame-ancestors 'none'" always;

    location /ws {
        proxy_pass http://127.0.0.1:36969;
        proxy_http_version 1.1;
        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 $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location /preview {
        proxy_pass http://127.0.0.1:36970;
        proxy_http_version 1.1;
        # Decrypted bytes stream straight through; never spooled to nginx temp files.
        proxy_buffering off;
        proxy_max_temp_file_size 0;
        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 $scheme;
    }

    location /download {
        proxy_pass http://127.0.0.1:36970;
        proxy_http_version 1.1;
        # Decrypted bytes stream straight through; never spooled to nginx temp files.
        proxy_buffering off;
        proxy_max_temp_file_size 0;
        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 $scheme;
    }

    location /upload {
        proxy_pass http://127.0.0.1:36970;
        proxy_http_version 1.1;
        client_max_body_size 0;
        proxy_request_buffering off;
        proxy_buffering off;
        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 $scheme;
    }

    location / {
        proxy_pass http://127.0.0.1:36968;
        proxy_http_version 1.1;
        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 $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
