How do I add rate limiting in Nginx?
Define a limit_req_zone in the http block (limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s), then add limit_req zone=api burst=20 nodelay inside the location block. This allows 10 requests per second with a burst of 20.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Configuring Nginx as a Reverse Proxy on EC2 Routing Traffic to Node.js
Nginx handles port management (80/443 while Node.js uses 3000), SSL termination, static file serving, load balancing, WebSocket proxying for Socket.io, and gzip compression. Node.js doesn't need to run as root or handle SSL directly.
Add a location /socket.io/ block with proxy_pass, proxy_http_version 1.1, and the Upgrade and Connection headers set to 'upgrade'. These headers are critical without them, WebSocket connections will fail.
They pass the client's real IP address to the Node.js app. Without them, Node.js sees Nginx's IP (127.0.0.1) instead of the actual client IP, which breaks IP-based features like rate limiting and logging.
Still have questions?
Browse all our FAQs or reach out to our support team
