Block Country Cloudflare? Yes, Here’s How
- Average speed
- km/h
- Result
- Finish time
- 50 min
- You entered
- Distance
- 10 km
- You entered
Yes, you can block a country using Cloudflare, and it is one of the most straightforward security features the service offers. You configure a firewall rule or a WAF custom rule that matches the visitor’s country code and sets the action to Block. This article explains exactly how to do it, what the limitations are, and what to consider before you flip the switch.
Why Would You Block a Country?
Website owners block entire countries for a few common reasons. The most frequent is stopping automated attacks—brute force login attempts, credential stuffing, or scraping bots that originate from specific regions. Another reason is to comply with licensing agreements that restrict content to certain geographies, or to reduce spam from regions that generate a high volume of junk traffic. Some sites also block countries to protect against DDoS attacks that often come from botnets concentrated in particular areas.
It is important to note that country blocking is a blunt instrument. It blocks all visitors from that country, including legitimate users, customers, or partners. Before you enable it, weigh the benefit of stopping unwanted traffic against the cost of losing real visitors.
How Cloudflare Determines a Visitor’s Country
Cloudflare does not use the visitor’s IP address directly to guess their location. Instead, it uses the country code that appears in the CF-IPCountry header, which is added to every HTTP request that passes through Cloudflare’s network. This header is populated based on the IP address of the connecting client, as mapped by Cloudflare’s own geolocation database. The database is updated regularly and is generally accurate at the country level, though it can be wrong for users behind corporate proxies, VPNs, or Tor exit nodes that are located in a different country than the user.
When you create a firewall rule that uses the ip.country field, Cloudflare compares the value of CF-IPCountry against the country codes you specify. The country codes are ISO 3166-1 alpha-2, such as US for the United States, GB for the United Kingdom, CN for China, and RU for Russia. You can also use T1 for Tor exit nodes, which is a special code Cloudflare assigns to traffic that comes from the Tor network.
Step-by-Step: Creating a Country Block Rule
Here is how to block a country using the Cloudflare dashboard. These steps apply to the current version of the dashboard as of 2025.
- Log in to your Cloudflare account and select the domain (website) you want to manage.
- In the left sidebar, click Security → WAF → Custom rules.
- Click Create rule.
- Give the rule a descriptive name, such as “Block traffic from Country X�.
- In the If incoming request matches section, use the dropdown to select Country (the field is
ip.country). - Choose equals and enter the two-letter country code (e.g.,
XXfor a placeholder). - To block multiple countries, click Or to add additional conditions, or use the in operator with a list of codes.
- In the Then take action section, select Block.
- Click Deploy to activate the rule.
If you prefer to use the API, you can create a rule with a similar structure. The exact API endpoints and payloads change over time, so consult Cloudflare’s official API documentation for the current schema.
What the Block Actually Does
When a request matches a block rule, Cloudflare returns a 403 Forbidden response to the client. The user sees a standard Cloudflare error page that says “Sorry, you have been blocked� (or a custom page if you have configured one). The request never reaches your origin server, which is the main benefit: it saves your server resources and prevents the unwanted traffic from consuming bandwidth.
It is worth noting that the block applies to all HTTP requests, including static assets like images, CSS, and JavaScript. If you block a country, visitors from that country will not be able to load any part of your site, even if they have a direct link to a file. This is usually what you want, but it is something to keep in mind if you only intended to block certain paths.
Limitations and Common Pitfalls
Country blocking is not a security silver bullet. Here are the main limitations:
- VPNs and proxies: A determined user can easily use a VPN or a proxy in an allowed country to bypass the block. The block only works if the IP address is geolocated to the blocked country.
- IP geolocation errors: Some IP addresses are misclassified, especially for mobile carriers or satellite providers. You may inadvertently block legitimate users.
- Tor exit nodes: Tor traffic is a special case. You can block
T1separately, but doing so also blocks users who are not from the blocked country but are using Tor for privacy. - IPv6: Make sure your rule applies to both IPv4 and IPv6 traffic. Cloudflare’s firewall rules apply to both by default, but if you have separate rules, double-check.
- Rule order: If you have other firewall rules that allow or challenge traffic, the order matters. Cloudflare evaluates rules in order, and the first rule that matches determines the action. Make sure your country block rule is placed before any allow rules that might match the same traffic.
Alternatives to Blocking an Entire Country
If blocking a whole country feels too aggressive, consider these alternatives:
- Challenge: Instead of blocking, set the action to Managed Challenge or JS Challenge. This forces the visitor to solve a CAPTCHA or run a JavaScript challenge, which can deter bots while still allowing humans through.
- Rate limiting: If the problem is a high volume of requests, create a rate limiting rule that applies only to the problematic country, allowing a certain number of requests per minute before blocking.
- Block specific paths: Use a rule that blocks only certain URL paths (e.g.,
/wp-login.php) for the country, rather than the entire site. - Bot fight mode: Cloudflare’s Bot Fight Mode can detect and block automated traffic without you having to specify countries.
Cost and Plan Considerations
Country blocking is available on all Cloudflare plans, including the free tier. However, the exact features you can use may vary. The free plan includes a limited number of firewall rules (currently 5), while paid plans (Pro, Business, Enterprise) allow more rules and additional features like rate limiting and advanced WAF rules. If you need to block many countries, you may need to upgrade to a plan that supports more rules.
There is no extra cost for the country blocking feature itself—it is included in the plan. However, if you use the API to manage rules, you may incur API request costs if you exceed the free API call limit, but that is separate.
Frequently Asked Questions
Can I block a country without using Cloudflare?
Yes, you can block by country at the server level using firewall software like iptables or by using a web server module such as mod_geoip for Apache or ngx_http_geoip_module for Nginx. These methods rely on your server’s own IP geolocation database, which may be less accurate than Cloudflare’s and requires you to update the database regularly. Cloudflare simplifies the process and offloads the blocking to its edge network, so the traffic never reaches your server.
Does country blocking affect SEO?
If you block a country that you are targeting for SEO, it can hurt your rankings in that country’s search results. Google and other search engines crawl from various locations, but if their crawlers are blocked, they may not index your site properly. If you block a country where you do not have a business presence, the impact is usually minimal. However, if you block a large region, you might lose international traffic that could have been valuable.
Can I allow specific IPs from a blocked country?
Yes, you can create a rule that allows specific IP addresses or IP ranges before the country block rule. For example, if you have a partner in a blocked country, you can add an allow rule for their IP. The order of rules matters: the allow rule must come before the block rule, otherwise the block will take precedence. Cloudflare evaluates rules in the order they appear in the list, so place the allow rule above the block rule.
How do I test if the block is working?
You can test by using a VPN or a proxy that exits in the blocked country and visiting your site. You should see the Cloudflare block page. Alternatively, you can use a service that checks your site from different locations, such as a website availability checker that lets you choose a location. Keep in mind that some testing services may not use real IPs from the country, so the result may not be accurate.
What is the difference between blocking and challenging?
Blocking returns a 403 Forbidden response and stops the request entirely. Challenging shows a CAPTCHA or a JavaScript challenge that the visitor must pass to proceed. If they pass, they can access the site; if they fail or are a bot, they are blocked. Challenging is less aggressive and allows legitimate users through, but it can still stop many bots.
Related
Formula used
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.