🚫 Block VPNs, Tor & Proxies with Nginx using MyIP.casa (in 5 minutes)
🚫 Block VPNs, Tor & Proxies with Nginx using MyIP.casa
Allowing only "clean" users on your infrastructure is a real challenge. Whether you're trying to prevent spam, fraud, or large-scale scraping, filtering traffic from VPNs, proxies, or datacenter IPs is your first line of defense.
In this guide, you'll learn how to integrate the MyIP.casa API directly into your Nginx configuration to block unwanted traffic in minutes.
🔧 Understanding auth_request
Nginx provides a powerful module called ngx_http_auth_request_module. It allows you to send a subrequest to an internal API before granting access to your application.
This makes it perfect for real-time IP filtering.
🧠 Create a filtering microservice
Since you already have access to MyIP.casa Pro, you can build a simple endpoint that checks whether an IP is a VPN or not.
@app.route("/check-access")
def check_access():
ip = request.headers.get("X-Real-IP")
# Query MyIP.casa intelligence (or your cache)
is_vpn = get_vpn_status(ip)
if is_vpn:
return "Forbidden", 403
return "OK", 200
• Cache results (Redis recommended)
• Avoid calling the API on every request
⚙️ Configure Nginx
Add the following to your Nginx site file (e.g. /etc/nginx/sites-available/default):
server {
listen 80;
server_name your-site.com;
location / {
auth_request /validate-ip;
proxy_pass http://your_real_backend;
}
location = /validate-ip {
internal;
proxy_pass https://your-internal-api/check-access;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Real-IP $remote_addr;
}
error_page 403 = @blocked;
location @blocked {
return 403 "Access denied: VPN/proxy not allowed.";
}
}
⚡ Why caching is critical
Querying an API on every HTTP request will slow down your application. Cache the decision for 24 hours using:
• Redis • In-memory cache • lua_shared_dict (OpenResty)
✅ Conclusion
By combining the accuracy of MyIP.casa Pro with the flexibility of Nginx, you can enforce perimeter security automatically without impacting legitimate users. It's fast, scalable, and production-ready.
curl -H "X-API-Key: YOUR_API_KEY" https://myip.casa/api/pro/details
🚀 Start filtering VPNs in minutes
Get your free API key and secure your infrastructure today.
Get API Key →
Commentaires
Enregistrer un commentaire