# Wheat Harvester — nginx site.
# Serves the web admin SPA and reverse-proxies the API + uploaded files.
#
# Install:
#   sudo cp deploy/nginx.conf /etc/nginx/sites-available/myharvester
#   sudo ln -sf /etc/nginx/sites-available/myharvester /etc/nginx/sites-enabled/myharvester
#   sudo rm -f /etc/nginx/sites-enabled/default        # optional
#   sudo nginx -t && sudo systemctl reload nginx

server {
  listen 80;
  listen [::]:80;
  server_name _;            # <-- replace with your domain or EC2 public DNS

  # ---- Web admin (static SPA built to apps/web/dist) ----
  root /var/www/myharvester-web;
  index index.html;

  # Hashed build assets — safe to cache forever.
  location /assets/ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    try_files $uri =404;
  }

  # SPA fallback so client-side routes (react-router) resolve to index.html.
  location / {
    try_files $uri $uri/ /index.html;
  }

  # ---- API (NestJS, global prefix /api/v1) ----
  location /api/ {
    proxy_pass http://127.0.0.1:3000;
    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;
  }

  # ---- Uploaded bills/receipts served by the API at /uploads ----
  location /uploads/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
  }

  # Uploads are capped at 5 MB in the API; allow a little headroom.
  client_max_body_size 6M;
}
