Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NGINX Rate Limiter
Implements rate limiting using Nginx
limit_reqto block high-volume or burst traffic before it reaches FastAPI, protecting the API from overloaded repeated attacks.Key Features
/items: No rate limiting/upload&/authenticate: Strict (20/min + 5 burst)nodelayfor better user experienceChanges
Nginx Configuration (
docker/nginx/nginx.conf):limit_req_zonedefinitions:general_limit: 100 requests/minutelenient_limit: 300 requests/minute (defined but reserved for future use)strict_limit: 20 requests/minuteNginx Location Blocks (
docker/nginx/conf.d/lenny.conf):/v1/api/opds*→ No rate limiting (regex location)/v1/api/items→ No rate limiting (exact match)/v1/api/(upload|authenticate)→ Strict rate limiting (20/min)/v1/api/*(all other endpoints) → General rate limiting (100/min)proxy_passwithout URI path (Nginx requirement)Rate Limiting Strategy
No Rate Limiting:
/v1/api/opdsand/v1/api/opds/{book_id}- Public catalog feeds for e-reader apps/v1/api/items- Public items listingStrict Rate Limiting (20/min + 5 burst = 25 total):
/v1/api/upload- File uploads (resource-intensive)/v1/api/authenticate- Authentication (security-sensitive)General Rate Limiting (100/min + 10 burst = 110 total):
Testing Results
OPDS endpoints: 20/20 requests passed (no rate limiting)
/items endpoint: 20/20 requests passed (no rate limiting)
General endpoints: 11 requests passed, then 503 (rate limited)
Strict endpoints: 8 requests passed, then 503 (rate limited)
Verification
Technical Details
Nginx Location Matching Order:
=)~) - in order of appearanceThis ensures OPDS and
/itemsendpoints are matched before general rate limiting is applied.Burst Handling:
burst=N: Allows N additional requests beyond the ratenodelay: Processes burst requests immediately instead of queuingNotes
lenient_limitzone (300/min) is defined but not currently used; reserved for future endpoint-specific needs$binary_remote_addras the key (client IP address)PTAL @ronibhakta1