# ERR_TOO_MANY_REDIRECTS and Operator Checks

## Screenshot error: "explorer.d-bis.org redirected you too many times"

**URL:** `https://explorer.d-bis.org/snap/`  
**Browser:** ERR_TOO_MANY_REDIRECTS

### Root cause

1. **NPMplus** (reverse proxy) terminates HTTPS and forwards traffic to the explorer VM (VMID 5000) on **port 80 (HTTP)**.
2. The VM’s nginx had **"redirect all HTTP → HTTPS"** for `location /`.
3. So: Browser (HTTPS) → NPMplus → VM (HTTP) → nginx returns **301 to HTTPS** → browser requests HTTPS again → same path → loop.

So the VM was always telling the client "use HTTPS" even when the client was already using HTTPS via the proxy.

### Fix (already in repo)

Nginx was updated to **not** redirect to HTTPS when the request is already marked as HTTPS by the proxy:

- **`X-Forwarded-Proto: https`** → treat as HTTPS and **do not** send 301.

Scripts that apply the fix (run **inside VMID 5000** or via Proxmox host):

- **Custom frontend config:** `scripts/fix-nginx-serve-custom-frontend.sh`
- **Conflicts / proxy-everything config:** `scripts/fix-nginx-conflicts-vmid5000.sh`

Both now:

- Only redirect HTTP→HTTPS when `X-Forwarded-Proto` is not `https`.
- Serve **`/snap/`** from `/var/www/html/snap/` (so the Snap companion page doesn’t get proxied to Blockscout and cause 404/redirects).

**Apply from repo (Proxmox host or with EXPLORER_VM_HOST):**

```bash
cd explorer-monorepo
bash scripts/apply-nginx-explorer-fix.sh
```

Or, inside VMID 5000:

```bash
bash /path/to/fix-nginx-serve-custom-frontend.sh
# or
nginx -t && systemctl reload nginx
```

Ensure **`/var/www/html/snap/`** exists and contains the Snap companion app (e.g. from `metamask-integration/chain138-snap` build). If the directory is missing, `/snap/` will 404 but will no longer redirect in a loop.

### Deploy Snap companion site to VMID 5000

1. **Build** (from repo root; requires Node 18+ and pnpm):

   ```bash
   cd metamask-integration/chain138-snap
   bash scripts/build-snap-site-for-explorer.sh
   ```

   Output: `packages/site/public/`. Build uses `GATSBY_SNAP_API_BASE_URL=https://explorer.d-bis.org` and `GATSBY_PATH_PREFIX=/snap`.

2. **Deploy** (from repo root). Either:

   - **From the Proxmox host** that has VMID 5000:

     ```bash
     bash explorer-monorepo/scripts/deploy-snap-site-to-vmid5000.sh
     ```

   - **From your dev machine** with SSH key access to the Proxmox node:

     ```bash
     EXPLORER_VM_HOST=root@192.168.11.12 bash explorer-monorepo/scripts/deploy-snap-site-to-vmid5000.sh
     ```

   The script tars `metamask-integration/chain138-snap/packages/site/public/` and deploys it to `/var/www/html/snap/` in VMID 5000. To build and deploy in one go: `BUILD_FIRST=1 bash explorer-monorepo/scripts/deploy-snap-site-to-vmid5000.sh` (from repo root).

---

## Disk space (all VMIDs)

From the **Proxmox host** (or a machine with SSH to the hosts):

```bash
./scripts/maintenance/check-disk-all-vmids.sh
```

Optional CSV:

```bash
./scripts/maintenance/check-disk-all-vmids.sh --csv
```

Requires SSH key access to the Proxmox hosts (see `config/ip-addresses.conf`).  
VMID 5000 (explorer) specifically:

```bash
pct exec 5000 -- df -h /
```

Or via SSH to the node that runs VMID 5000:

```bash
ssh root@192.168.11.12 'pct exec 5000 -- df -h /'
```

---

## Links and health checks

- **Explorer API:**  
  `curl -sS -o /dev/null -w "%{http_code}" https://explorer.d-bis.org/api/v2/stats`  
  Expect **200** (502 = Blockscout not running; see [WHY_INFO_NOT_LOADING.md](./WHY_INFO_NOT_LOADING.md)).

- **Explorer root:**  
  `curl -sS -o /dev/null -w "%{http_code}" https://explorer.d-bis.org/`  
  Expect **200**.

- **Explorer /snap/:**  
  `curl -sS -o /dev/null -w "%{http_code}" -L https://explorer.d-bis.org/snap/`  
  After the nginx fix, expect **200** (or 404 if `/var/www/html/snap/` is not deployed).

- **Doc link checker (internal docs):**  
  `node scripts/check-doc-links.mjs`  
  (from repo root; checks `docs/` internal links.)

---

## Summary

| Issue | Cause | Fix |
|-------|--------|-----|
| ERR_TOO_MANY_REDIRECTS on /snap/ (or any path) | VM nginx always redirects HTTP→HTTPS while NPMplus forwards as HTTP | Use nginx config that skips redirect when `X-Forwarded-Proto: https`; reload nginx |
| /snap/ 404 or wrong content | /snap/ proxied to Blockscout or not served | Serve `/snap/` from `/var/www/html/snap/` (location /snap/ in nginx) |
| No stats/blocks (502 on /api/) | Blockscout not running on port 4000 | See [WHY_INFO_NOT_LOADING.md](./WHY_INFO_NOT_LOADING.md), run `scripts/fix-502-blockscout.sh` |
