Apache block IP address as Specific, Subnet, Wildcard 2023

By XiaoXin
A Bit Randomly

RDBs 1 IntroductionImplementation principleAdvantages and disadvantagesRelated configurationAOF 1 IntroductionImplementation principleSynchronization interval configurationAdvantages and disadvantagesRelated configurationP... Read Persistence of Redis

Main Contents

Apache block IP address as Specific, Subnet, Wildcard

The step-by-step tutorial on how to block IP addresses on Apache server by config listing an IP address, you can block specific, subnet, and wildcard IP with IPv4, and IPv6.

1. Create file data of blocking as /path_to_file/bad_ips_listing

// Deny access from a specific IP address:
Deny from 222.222.222.111

// Deny access from a subnet:
Deny from 192.16.0.112/28

// Deny access from a wildcard IPv4:
Deny from 192.16.0.1*
Deny from 192.16.*.*
Deny from 192.16

// Deny access from a wildcard IPv6:
Deny from fe22::8ff:fe12:6789:123a
Deny from fe22::8ff:fe12:6789:123a/10
Deny from fe22::8ff:fe12:*:*
Deny from fe22::8ff:fe12

2. Configure permission for /path_to_file/bad_ips_listing

chown www-data.www-data /path_to_file/bad_ips_listing

3. Open the config file of the website.

/etc/apache2/sites-enabled/000-default.conf

4. Add the following codes to this file config.

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteMap    hosts-deny  "txt:/path_to_file/bad_ips_listing"
RewriteCond   "${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND}" "!=NOT-FOUND" [OR]
RewriteCond   "${hosts-deny:%{REMOTE_HOST}|NOT-FOUND}" "!=NOT-FOUND"
RewriteRule .* - [F]
</VirtualHost>

5. Restart Apache 

service apache2 restart

And now the IP is in the range of the blocking list. Will return status code 403 error when access to your server. 

Please Share This Article Thank You!

Install Wget for Windows

Wget is a free software package for retrieving files, and accessing URLs using multiple methods HTTP, HTTPS, FTP, and FTPS, the most widely used Internet protocols on Linux. If you want to use Wget on Windows, this is the ...

Setup Adyen Payment for Magento 2

To integrate Adyen with Magento 2, you will need to follow these steps:Adyen is a payment processing platform that allows merchants to accept a wide range of payment methods, including credit and debit cards, mobile wallet...