π Protect your web servers against abuse and ensure optimal performance with automated rate limiting configurations. This project generates and manages rate limit rules for multiple web server platforms, making it easy to implement robust protection against excessive requests.
- βοΈ Multi-Web Server Support: Generates rate limiting configurations for Apache (ModSecurity), Nginx, Traefik, and HAProxy.
- β±οΈ Centralized Configuration: Uses a single
config.yamlfile to define global and path-specific rate limits, as well as IP whitelisting/blacklisting. - π Automated Updates: GitHub Actions automatically fetch the latest configuration and generate new rules daily.
- π‘οΈ Flexible Rate Limiting: Supports limiting by IP address, User-Agent, or custom headers.
- β Easy Integration: Clear instructions and example configurations are provided to quickly integrate rate limiting into your servers.
- ποΈ Granular Control: Configure rate limits at both global and path-specific levels for detailed control.
- π΅ Nginx
- π Apache (ModSecurity)
- π£ Traefik
- π΄ HAProxy
Note
If you use Caddy please check the caddy-waf project.
limits/
βββ rate_limit_rules/ # π§ Generated rate limit config files
β βββ nginx/ # Nginx rate limit configs
β βββ apache/ # Apache rate limit configs (ModSecurity)
β βββ traefik/ # Traefik rate limit configs
β βββ haproxy/ # HAProxy rate limit configs
βββ import_apache_rate_limit.py
βββ import_haproxy_rate_limit.py
βββ import_nginx_rate_limit.py
βββ import_traefik_rate_limit.py
βββ ratelimit.py # βοΈ Main Script to load and validate rate limits config
βββ ratelimit2nginx.py # π Convert rate limit config to Nginx
βββ ratelimit2apache.py # π Convert rate limit config to Apache ModSecurity
βββ ratelimit2traefik.py # π Convert rate limit config to Traefik
βββ ratelimit2haproxy.py # π Convert rate limit config to HAProxy
βββ config.yaml # π Configuration file to define rate limits
βββ requirements.txt # π Required dependencies
βββ CONTRIBUTING.md # π€ Contribution guidelines
βββ CHANGELOG.md # π Project changelog
βββ .github/workflows/ # π€ GitHub Actions for automation
βββ update_limits.py
- The
config.yamlfile allows you to configure your desired rate limits, including global settings, path-specific settings, whitelists, blacklists and advanced options.
# config.yaml
global:
enabled: true
requests_per_minute: 60
burst: 20
window: 1m
limit_by: ip
# limit_by_header: custom_header
paths:
/login:
enabled: true
requests_per_minute: 10
burst: 5
window: 1m
limit_by: ip
/api:
enabled: true
requests_per_minute: 120
burst: 40
window: 1m
limit_by: ip
'/search/(.*)':
enabled: true
requests_per_minute: 100
burst: 20
window: 1m
limit_by: ip
whitelist:
enabled: false
ips:
- 192.168.1.10
- 192.168.1.11/32
- 2001:0db8::/32
blacklist:
enabled: false
ips:
- 192.168.1.20
- 192.168.1.22/32
advanced:
log_level: info- The
ratelimit.pyscript loads and validates the configurations fromconfig.yaml. ratelimit2nginx.pygenerates Nginx configurationratelimit2apache.pygenerates Apache ModSecurity configurationratelimit2traefik.pygenerates Traefik configurationratelimit2haproxy.pygenerates HAProxy configuration
- GitHub Actions automatically generate rate limiting configurations daily.
- Modified configuration files are automatically committed and pushed to the repository.
Before you begin, ensure you have the following installed on your system:
- Python 3.7+: Required to run the rate limit generation scripts
- pip: Python package manager (usually comes with Python)
- Git: For cloning the repository
- A supported web server: At least one of the following:
- Nginx
- Apache with ModSecurity
- Traefik
- HAProxy
-
Clone the Repository:
git clone https://github.com/fabriziosalmi/limits.git cd limits -
Install Python Dependencies:
pip install -r requirements.txt
-
Configure
config.yaml:- Edit the
config.yamlfile to define your specific rate limiting requirements. - Configure global settings, path-specific rules, and whitelist/blacklist as needed.
- Edit the
- Generate Configuration:
- The rate limit configuration files will be generated automatically by github actions.
- Integrate configuration with your webserver
- Copy
rate_limit_rules/nginx/nginx_rate_limit.confto your server. - Include the configuration in your nginx configuration file (
nginx.conf)
http {
include /path/to/nginx_rate_limit.conf;
...
}- Copy
rate_limit_rules/apache/apache_rate_limit.confto your server. - Include the configuration in your apache virtualhost configuration file or inside a
.htaccessfile.
<VirtualHost *:80>
...
Include /path/to/apache_rate_limit.conf
...
</VirtualHost>-
Copy the content of
rate_limit_rules/traefik/traefik_rate_limit.confto your traefik configuration file (traefik.yml)# traefik.yml ... http: middlewares: # Insert content of traefik_rate_limit.conf here routers: # Add the rate limit middlewares to the routes ...
* Copy `rate_limit_rules/haproxy/haproxy_rate_limit.conf` to your server.
* Include the configuration in your HAProxy configuration file (`haproxy.cfg`)
frontend http-in
# Insert the content of haproxy_rate_limit.conf here
...
Before deploying to production, it's important to test your rate limit configuration:
For Nginx:
nginx -tFor Apache:
apachectl configtestFor HAProxy:
haproxy -c -f /etc/haproxy/haproxy.cfgYou can use tools like curl or ab (Apache Bench) to test rate limiting:
# Send multiple rapid requests to test rate limiting
for i in {1..100}; do curl -s http://localhost/api; done
# Use Apache Bench for load testing
ab -n 100 -c 10 http://localhost/apiCheck your web server logs to verify that rate limiting is working:
Nginx:
tail -f /var/log/nginx/error.logApache:
tail -f /var/log/apache2/error.logHAProxy:
tail -f /var/log/haproxy.log- Daily Updates: GitHub Actions fetches new rate limit configurations daily at midnight UTC.
- Auto Deployment: Pushes new configuration files directly to
rate_limit_rules/. - Manual Trigger: Updates can also be triggered manually.
Issue: "Error: config file not found"
- Solution: Ensure
config.yamlexists in the root directory of the project.
Issue: "Error parsing YAML"
- Solution: Check that your
config.yamlfile has valid YAML syntax. Use a YAML validator if needed.
Issue: Rate limits not working after configuration
- Solution:
- Verify that the configuration file is correctly included in your web server's configuration.
- Restart your web server after applying the configuration.
- Check your web server's error logs for any configuration errors.
Issue: Generated configuration files are empty
- Solution: Run the generation scripts manually to check for errors:
python ratelimit2nginx.py python ratelimit2apache.py python ratelimit2traefik.py python ratelimit2haproxy.py
Issue: Import scripts fail with "environment variable not set"
- Solution: Set the appropriate environment variable before running the import script:
export NGINX_RATE_LIMIT_FILE=/path/to/nginx/conf.d/rate_limit.conf python import_nginx_rate_limit.py
We welcome contributions from the community! Here's how you can help:
- Fork the Repository: Click the "Fork" button at the top right of the repository page.
- Clone Your Fork:
git clone https://github.com/YOUR_USERNAME/limits.git cd limits - Create a Feature Branch: Use a descriptive name for your branch.
git checkout -b feature/your-feature-name
- Make Your Changes: Implement your feature or bug fix.
- Test Your Changes: Ensure the scripts run correctly:
python ratelimit.py python ratelimit2nginx.py python ratelimit2apache.py python ratelimit2traefik.py python ratelimit2haproxy.py
- Commit Your Changes: Write clear, concise commit messages.
git add . git commit -m "feat: add new feature description"
- Push to Your Fork:
git push origin feature/your-feature-name
- Open a Pull Request: Go to the original repository and click "New Pull Request".
- Follow the existing code style and conventions.
- Add comments to explain complex logic.
- Update documentation if you change functionality.
- Test your changes thoroughly before submitting.
- Keep pull requests focused on a single feature or fix.
For more detailed contribution guidelines, see CONTRIBUTING.md.
When implementing rate limiting, keep these security best practices in mind:
Rate limiting is one layer of defense. Implement additional security measures:
- Input validation and sanitization
- Authentication and authorization
- HTTPS/TLS encryption
- Web Application Firewall (WAF)
- Regular security updates
- Too restrictive: May block legitimate users
- Too lenient: May not prevent abuse effectively
- Monitor your traffic patterns and adjust accordingly
- Only whitelist IPs you fully trust (e.g., monitoring services, trusted partners)
- Regularly review and update your whitelist
- Use CIDR notation to specify IP ranges precisely
Apply stricter rate limits to sensitive endpoints:
- Login pages (
/login,/auth) - API endpoints (
/api/*) - Password reset (
/reset-password) - Search functionality (
/search)
- Enable logging to track rate limit violations
- Set up alerts for unusual patterns
- Regularly review logs for potential attacks
If running multiple server instances:
- Use a shared storage for rate limit counters (Redis, Memcached)
- Ensure rate limits are synchronized across all instances
- Consider using a centralized rate limiting solution
This project is licensed under the MIT License.
See the LICENSE file for details.
- Issues? Open a ticket in the Issues tab.