Skip to content

Engineering

Proxy Trust Fixed False 429s for 60 Ankik Users

Ankik rate limiter blocked real users behind Caddy and Cloudflare. One trust proxy line fixed client IP and cut false 429s to zero.

Dhanji Bhagat

Dhanji Bhagat

Founder & Principal Engineer

6 min read
NestJSrate limitingCaddyCloudflareAnkik

Our Ankik rate limiter grouped every shop behind our proxy into one bucket. In a live test of 60 distinct forwarded clients, 11 got HTTP 429 without doing anything wrong.

This post records the cause, the one line fix, and the live verification. The work shipped in Ankik as fix for issue 55, PR 96.

What false 429s mean

A 429 response tells a client to slow down. A false 429 tells an innocent client to slow down because another client used the shared quota.

In Ankik, the global ThrottlerGuard allows 50 requests per minute per IP. Behind Caddy and Cloudflare, req.ip always resolved to the proxy address. All users shared one bucket. Eleven legitimate clients got 429s in our test window of 60 distinct forwarded clients.

When this reaches production, founders feel it as support tickets during peak hours. Accountants cannot save invoices. Staff retry and hit the limit again. Trust drops fast.

My first instinct was wrong

I first suspected the frontend. I checked retry logic and debounce timers in the web client. The client code looked clean.

I then checked Cloudflare rules. I thought a WAF rule blocked the API path. The WAF logs showed no blocks.

The actual cause sat in our Nest bootstrap. We never told Express to trust the proxy hop. The fix took one line. The search took most of the morning.

The fix

We trust the first proxy hop in the Nest bootstrap:

app.set('trust proxy', 1);

We apply it with NestExpressApplication typing so req.ip resolves from the forwarding chain. The value 1 means trust one hop, which matches our Caddy in front of Node. We did not trust all hops. That keeps spoofed headers from bypassing limits.

After this change, req.ip returns the client address from X-Forwarded-For. The ThrottlerGuard buckets each shop separately. Audit and security logs also record the true client IP again.

CheckBeforeAfter
req.ip behind CaddyProxy addressClient address from forwarded chain
60 distinct forwarded clients11 got HTTP 4290 got HTTP 429
Direct traffic with no headerThrottled normallyThrottled normally
Audit log IPProxy IPClient IP

How we verified it

We sent 60 requests with distinct X-Forwarded-For values through the production path. Before the fix, 11 responses returned 429. After the fix, 0 returned 429.

We also sent direct traffic with no forwarding header. That traffic still hit the limiter at the expected threshold. The guard still works. It now keys on the right value.

The change is small, so review stayed small. The commit touches bootstrap only. No controller logic changed. No client update was needed.

What we kept and what we gave up

We kept the global 50 per minute limit. Shops never hit it in normal billing. The limit still blocks scripts that poll in a loop.

We gave up the comfort of default Express settings. Defaults assume Node faces the internet directly. That is false behind Caddy and Cloudflare. Every Nest app behind a proxy needs this line on day one.

One open edge remains. PUT style presigned flows and header spoofing need separate review when more than one proxy sits in the chain. We set trust to exactly one hop for now. If we add another layer, we will revisit the count.

FAQ

Why did all users share one rate limit bucket?

Express returned the proxy IP for every request. The limiter keyed on that single IP. Each shop consumed the same 50 per minute quota.

Does trust proxy open the app to IP spoofing?

It can if the count is too high. We set it to 1 because one Caddy hop sits in front of Node. Direct traffic with no header still throttles as before.

How do I check my own Nest app?

Log req.ip behind your production proxy. Send two requests with different X-Forwarded-For values. If both log the same IP, you miss the trust setting.

Did this change audit logs too?

Yes. Security logs recorded the proxy IP before. They now record the client IP from the forwarding chain.

Next step

If you run Nest behind Caddy or Cloudflare, check your bootstrap today. Log the IP. Run the 60 client test. The fix is one line and the verification takes ten minutes.

For context on our ledger work, read the Ankik multi tenancy note on row level security. For launch telemetry, read the Day 29 problem note. To discuss your MVP rate limits, contact us through the build sprint page.

BOOK A CALL

Ready to turn your idea into a live product?

Schedule a 15-minute scoping call with Dhanji below. We'll discuss your scope, timeline, and tech strategy honestly.