(Update 19 July 2019 – updated to reflect correct boolean logic for multiple countries)
Cloudflare is an amazing web-based security and performance improvement product for website admins. If you’re running Cloudflare, even on the free version, and are looking at blocking access from countries using a whitelist-only approach, look no further.
- Log into your Cloudflare account.
- Navigate to the “Firewall” , click on “Firewall Rules“, then click on “Create a Firewall rule.”
- For one country: In the “Rule name” section, give your rule a descriptive name. Under “Field” select “Country“, under “Operator” select “does not equal“, under “Value” select your country that you want to allow, select “Block” under “Choose an action“, and finally click on “Deploy.”
- For multiple countries: change the “Operator” to “is not in” and list multiple countries in the “Value” field.
- Ensure that your rule is at the top of your list if you have multiple rules. Cloudflare processes rules in the order that they are listed.
- Lastly check your rule by visiting your site from a country that is not in your allow list. You should get a block message.