# 504 Gateway Time-out – nginx

A **504 Gateway Time-out** means nginx gave up waiting for a response from the upstream (Next.js or FastAPI). The default nginx proxy timeout is **60 seconds**; if the app takes longer, nginx returns 504.

**Common cause:** You use **port 80** (e.g. `http://207.154.217.161`) but the active nginx config for port 80 was the **default** site or another file **without** the proxy timeouts. The repo’s `nginx/aumentum.conf` now includes a **port 80** server block with timeouts. Deploy it so port 80 is served by that config.

## Fix: increase proxy timeouts

Inside each `location` that uses `proxy_pass`, add (or ensure you have):

```nginx
proxy_connect_timeout 120s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
```

- **proxy_connect_timeout** – time to connect to upstream (e.g. 120s).
- **proxy_send_timeout** – time to send request to upstream (e.g. 300s).
- **proxy_read_timeout** – time to wait for upstream response (e.g. 300s). This is the one that usually causes 504 if too low.

## Where to add them

1. **Next.js (e.g. `location /`)**  
   Without these, slow or cold Next.js responses can hit the 60s default and get 504.

2. **FastAPI (e.g. `location /api/`)**  
   Long-running auth or DB calls need a higher read timeout.

## Apply on the server (recommended)

Run the deploy script from the project root (it installs the config with port 80 + timeouts and disables the default site so port 80 uses it):

```bash
cd /var/www/boundary-fastapiandnextjs
sudo bash scripts/deploy_nginx_504_fix.sh
```

Or manually:

```bash
sudo cp /var/www/boundary-fastapiandnextjs/nginx/aumentum.conf /etc/nginx/sites-available/boundary
sudo ln -sf /etc/nginx/sites-available/boundary /etc/nginx/sites-enabled/boundary
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
```

If your live config is different, open the active site config (e.g. under `/etc/nginx/sites-enabled/` or `/etc/nginx/conf.d/`) and add the three `proxy_*_timeout` lines above to:

- The block that does `proxy_pass` to Next.js (e.g. `http://127.0.0.1:3000`).
- The block that does `proxy_pass` to FastAPI (e.g. `http://127.0.0.1:8001`).

Then:

```bash
sudo nginx -t && sudo systemctl reload nginx
```

## If 504 persists

- Check that the **right** nginx config is loaded (the one used for the domain/IP and port you’re hitting).
- Confirm upstream is running: `curl -I http://127.0.0.1:3000/` and `curl -I http://127.0.0.1:8001/` on the server.
- If the app is slow, consider increasing timeouts further (e.g. 600s) or fixing slow endpoints/startup.
