How to Block IP by Country and Allow Uptime Monitor
In this guide, we will walk you through the steps to block traffic from specific countries using Cloudflare while allowing access to your uptime monitor IPs. We will cover how to achieve this both in Cloudflare’s Firewall Rules and on an Apache web server.
Cloudflare Firewall Rules
Step 1: Log in to Cloudflare
- Go to Cloudflare and log in to your account.
Step 2: Access Firewall Rules
-
In the Cloudflare dashboard, click on your domain.
-
Navigate to the “Firewall” section in the top menu and then click on “Firewall Rules.”
Step 3: Create a Firewall Rule
- Click on the “Create a Firewall Rule” button.
Step 4: Block Traffic by Country
-
Give your rule a descriptive name, like “Block by Country.”
-
Under “Then,” select “Block.”
-
Under “If,” choose the condition “Country.”
-
Choose “is in” and then select the countries you want to block. You can add multiple countries if needed.
Step 5: Add an Allow Rule for Uptime Monitor IPs
-
To allow access to your uptime monitor IPs, create another firewall rule.
-
Give this rule a name like “Allow Uptime Monitor.”
-
Under “Then,” select “Allow.”
-
Under “If,” choose the condition “IP Address.”
-
Specify the IP addresses of your uptime monitor service. You may need to check with your uptime monitor provider for the list of IPs they use.
Step 6: Order Your Rules
- Order your rules so that the “Allow Uptime Monitor” rule is higher in priority than the “Block by Country” rule. Rules are evaluated from top to bottom, so this ensures that the uptime monitor IPs are allowed before checking for country blocking.
Step 7: Save and Deploy
- Click “Save and Deploy” to activate your Firewall Rules.
Apache Configuration
If you’re using an Apache web server, you can also add an additional layer of protection.
Step 1: Access Apache Configuration
-
SSH into your server.
-
Navigate to the Apache configuration directory. On many Linux distributions, it’s located at
/etc/apache2/
or/etc/httpd/
.
Step 2: Edit the Apache Configuration File
-
Open the Apache configuration file for your site, usually located in the
/sites-available/
directory. -
Inside the
<VirtualHost>
section for your site, add the following lines to allow access to your uptime monitor IPs. Replacex.x.x.x
with the actual IP addresses:
|
|
Step 3: Block Traffic by Country
- To block traffic from specific countries, you can use the Apache
mod_geoip
module if it’s installed. If not, you can usemod_rewrite
as an alternative.
Using mod_geoip (if installed):
|
|
Using mod_rewrite (alternative method):
|
|
Step 4: Save and Restart Apache
-
Save the Apache configuration file and exit the editor.
-
Restart Apache to apply the changes:
|
|
These steps will block traffic from specified countries while allowing access to your uptime monitor IPs both at the Cloudflare and Apache levels, ensuring your website remains secure and available.