Block Access to Your Web Server by IP Address
This guide shows you how to block specific IP addresses from accessing your web server, which is useful for stopping malicious traffic, preventing brute-force attacks, or restricting access to certain regions. You will learn methods for common web servers (Apache, Nginx) and firewalls (iptables, UFW), and how to verify your blocks work.
What You’ll Need
- Administrative (root or sudo) access to your server
- Knowledge of the IP address(es) you want to block
- Access to your server’s command line (SSH)
- Depending on your setup: the web server config file (e.g.,
httpd.conf,nginx.conf) or firewall tool
Step-by-Step
- Identify the IP(s) to block. You can find them in server logs (e.g.,
/var/log/apache2/access.logor/var/log/nginx/access.log) or from security tools. Make a list of exact IP addresses or CIDR ranges (e.g.,203.0.113.5or203.0.113.0/24). - Choose your blocking method. You can block at the web server level (affects only that server) or at the firewall level (affects all services on that server). For most cases, firewall blocking is more efficient and secure.
- Block via UFW (Ubuntu/Debian with UFW). Run
sudo ufw deny from 203.0.113.5to block that IP. For a range, usesudo ufw deny from 203.0.113.0/24. Then reload withsudo ufw reload. - Block via iptables (any Linux). Use
sudo iptables -A INPUT -s 203.0.113.5 -j DROPto drop all traffic from that IP. For a range, usesudo iptables -A INPUT -s 203.0.113.0/24 -j DROP. To make it persistent, installiptables-persistentor save withsudo netfilter-persistent save. - Block via Apache (.htaccess or virtual host). In your
.htaccessfile or inside a<Directory>block, add:Require all granted Require not ip 203.0.113.5 Require not ip 203.0.113.0/24
Then restart Apache:
sudo systemctl restart apache2. - Block via Nginx (server block). Inside your
serverblock, add:deny 203.0.113.5; deny 203.0.113.0/24; allow all;
Then test and reload:
sudo nginx -tandsudo systemctl reload nginx. - Verify the block. From another machine, try to access your server using the blocked IP. You should see a timeout or a 403 Forbidden error. You can also check with
curl -I http://your-server.comfrom a machine with the blocked IP. - Log and monitor. Check your server logs to confirm the block is working and to see if the IP attempts other ports or services.
Common Mistakes
- Blocking your own IP: If you are testing from the same IP you are blocking, you will lock yourself out. Always test from a different IP.
- Using the wrong CIDR notation: A
/24block includes 256 addresses,/16includes 65,536. Double-check your range. - Forgetting to reload the server: Changes to config files or firewall rules often require a reload/restart to take effect.
- Blocking at the wrong level: If you block via .htaccess but your site uses a different virtual host, the rule may not apply.
- Not saving iptables rules: On reboot, iptables rules are lost unless you save them.
Tips
- Use a firewall (UFW or iptables) for IP blocks whenever possible; it is faster and more secure than web server config.
- Keep a whitelist of trusted IPs (like your office) and block everything else if you need strict access control.
- If you block a range, be careful: you might block innocent users (e.g., a shared hosting IP).
- For dynamic IPs, consider using a tool like Fail2ban to automatically block IPs that show malicious patterns.
- Document your blocks in a comment or a separate file so you can review them later.
Troubleshooting
Block doesn’t work: Check if the IP is being blocked at the correct level. If you used .htaccess, ensure AllowOverride All is set for that directory. For Nginx, ensure the deny directive is inside the correct server or location block.
You accidentally blocked yourself: If you are still connected, remove the rule immediately. If you are locked out, use a different network (e.g., mobile hotspot) or access via a console (if your provider offers it).
Firewall rules not persistent: On Ubuntu, run sudo apt install iptables-persistent and then sudo netfilter-persistent save. On CentOS, use sudo service iptables save.
Web server won’t restart: Check config syntax with sudo apache2ctl configtest or sudo nginx -t. The error message will usually point to the line with the issue.
IP still appears in logs: The block may be bypassing your server if the IP is using a proxy or VPN. Consider blocking the entire range of the proxy provider if necessary.
Frequently Asked Questions
Can I block a country instead of an IP?
Yes, but that requires GeoIP modules (like mod_geoip for Apache or ngx_http_geoip_module for Nginx) and a GeoIP database. It’s more complex but doable.
What’s the difference between DROP and REJECT in iptables?
DROP silently discards packets, making the connection time out. REJECT sends back an error (like ‘connection refused’). DROP is often preferred because it hides that the server exists.
Will blocking an IP affect other services like SSH?
If you block at the firewall level, yes, it blocks all traffic from that IP to any port. If you block at the web server level, only HTTP/HTTPS traffic is affected.
Can I block a range of IPs?
Yes, use CIDR notation (e.g., 203.0.113.0/24) in all methods. Be cautious with large ranges.
How do I unblock an IP?
For UFW: sudo ufw delete deny from 203.0.113.5. For iptables: sudo iptables -D INPUT -s 203.0.113.5 -j DROP. For Apache, remove the Require not ip line. For Nginx, remove the deny line.
Does blocking IPs affect SEO?
If you block search engine crawlers’ IPs, they may not index your site. Otherwise, blocking a few malicious IPs has no direct SEO impact.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.