Docs / Web Servers / Nginx Rate Limiting for API Protection

Nginx Rate Limiting for API Protection

By Admin · Apr 5, 2026 · Updated Apr 23, 2026 · 4 views · 3 min read

This guide covers how to set up and configure nginx on a Linux VPS. Whether you're running a production environment or a development setup, these instructions will help you get started quickly and securely.

Prerequisites

  • A VPS running Ubuntu 22.04 or later (2GB+ RAM recommended)
  • Basic familiarity with the Linux command line
  • A registered domain name (for public-facing services)
  • Root or sudo access to the server
  • SSL certificate or Certbot for Let's Encrypt

Server Installation

After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.


# /etc/nginx/sites-available/nginx.conf
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

The configuration above sets the recommended values for a VPS with 2-4GB of RAM. Adjust the memory-related settings proportionally if your server has different specifications.

Important Notes

Security should be a primary consideration when configuring nginx. Always use strong passwords, keep software updated, and restrict network access to only the necessary ports and IP addresses.

  • Enable automatic security updates for critical patches
  • Keep your system packages updated regularly
  • Review log files weekly for anomalies
  • Monitor disk space usage and set up alerts

Virtual Host Configuration

The default configuration works well for development environments, but production servers require additional tuning. Pay particular attention to connection limits, timeout values, and logging settings.


# Test and reload Nginx configuration
sudo nginx -t
sudo systemctl reload nginx

# Check access and error logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.

Performance Considerations

Performance benchmarks show that properly tuned nginx can handle significantly more concurrent connections than the default configuration. The key improvements come from adjusting worker processes and connection pooling.

SSL/TLS Setup

The default configuration works well for development environments, but production servers require additional tuning. Pay particular attention to connection limits, timeout values, and logging settings.


# Install Nginx with modules
sudo apt update
sudo apt install -y nginx

# Enable the site
sudo ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx

This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.

Summary

You've successfully configured nginx on your VPS. Remember to monitor performance, keep your software updated, and maintain regular backups. If you run into issues, consult the official documentation or open a support ticket for assistance.

Was this article helpful?