NGINX broke my rate limiting logic (KTOR)
Rate limiting your APIs is very important to prevent DDoS attacks. It prevents users from sending too many requests within a certain time interval.
My logic was simple. For a configurable time window, I allow a certain number of requests per API. If the threshold is exceeded, I return a 429 status, not allowing users to use the API.
fun Application.configureRateLimiter(
maxRequests: Int = 60,
windowMs: Long = 60_000,
excludedPaths: Set<String> = emptySet() // pass paths you want to exclude
) {
val requests = ConcurrentHashMap<String, RequestInfo>()
val mutex = Mutex()
intercept(ApplicationCallPipeline.Setup) {
val path = call.request.path()
// skip rate limiting for excluded paths
if (excludedPaths.any { path.contains(it) }) return@intercept
val ip = call.request.origin.remoteHost
val now = Instant.now().toEpochMilli()
mutex.withLock {
val info = requests.getOrPut(ip) { RequestInfo(0, now) }
// reset if window expired
if (now - info.timestamp > windowMs) {
info.count = 0
info.timestamp = now
}
info.count++
if (info.count > maxRequests) {
println("Rate limited for IP $ip API: $path")
call.respond(
HttpStatusCode.TooManyRequests,
"Rate limit exceeded. Try again later."
)
finish()
}
}
}
}
Everything seemed to be working until one day I noticed a problem. I sent push notifications to all users, encouraging them to open the mobile app. The push notification arrived on my device as well. I opened the app and saw a message saying “Server Error” on the home screen. WHAT A MESS!!! Imagine encouraging users to open the app, and the app is not working.
I immediately checked server resource monitoring in Google Cloud; everything was fine. The server CPU and RAM usage were not even exceeding 50%. Then I checked the Nginx error logs—nothing was there.
Next, I opened the application code and tried to think about what might have gone wrong. The only possible reason seemed to be my rate-limiting logic. I started thinking that my server might be receiving the same IP address from all users in Uzbekistan, because we have an entry point via UZTELECOM.
To check this assumption, I added logs to the rate-limiting logic and discovered a very strange result. It did not matter which device was used to access the API—the rate-limiting logic was always receiving the IP address 0.0.0.0, which is localhost. That meant if 60–100 people opened the app at the same time, all APIs were blocked for subsequent requests, and users would see a strange error screen when opening the app.
After careful investigation, I found out that Nginx provides this localhost IP if you do not configure X-Forwarded headers properly. To fix this correctly, I needed to install the plugin and add it to the application as shown below:
// build.gradle
implementation("io.ktor:ktor-server-forwarded-header")
// application code
install(XForwardedHeaders)
After this fix, I started receiving the real IP addresses of users.
This experience taught me that rate limiting can completely break in production if proxy headers are not configured correctly. Even a small misconfiguration can cause a perfectly working system to fail at the worst possible moment.