{"id":1326,"date":"2014-11-11T22:37:11","date_gmt":"2014-11-12T04:37:11","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=1326"},"modified":"2026-04-02T00:23:22","modified_gmt":"2026-04-02T06:23:22","slug":"how-to-blockallow-traffic-to-a-website-from-certain-countries","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/how-to-blockallow-traffic-to-a-website-from-certain-countries\/","title":{"rendered":"How to Block\/Allow traffic to a website from certain countries"},"content":{"rendered":"<p><span style=\"line-height: 1.5em;\">If you inspect your server&#8217;s error logs, you may see that many visitors (for the most part robots) constantly request access to things they should not be allowed to see. If you see that such access are suspicious and they are trying to spam your forum or hijack your mail program, you may need to deny access from those IP&#8217;s.<\/span><\/p>\n<p>Collecting such offending IP addresses by manually inspecting your logs across your site can become a full-time job.<\/p>\n<p>In such cases, you may block access to the site on Country basis (from which the suspicious access was observed) , given that the site have very few legitimate visitors from these regions.<\/p>\n<p>It can be done by using the <strong>mod_geoip<\/strong> apache module.<\/p>\n<p>What is Mod_geoip<br \/>\n==============<\/p>\n<p>Mod_geoip is a module for fast ip\/location lookups. It uses MaxMind GeoIP \/ GeoCity databases. If the IP was found in the database the module sets the appropriate environments variables to the request, thus making other modules\/fcgi be informed. In short, Mod_GeoIP module is used to get the geographic location of IP address of the visitor into the Apache web server. This module allows you to determine the visitor\u2019s country, organization and location.<!--more--><\/p>\n<p>Installing and configuring mod_geoip apache module.<br \/>\n======================================<\/p>\n<p>By following the below few simple steps, we can install and configure mod_geoip apache module in the server and restrict access to the websites on a country basis.<\/p>\n<p><em>#yum install GeoIP GeoIP-devel GeoIP-data zlib-devel mod_geoip<\/em><\/p>\n<p><em>#cd \/var\/lib<\/em><\/p>\n<p><em>#wget http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoLiteCountry\/GeoIP.dat.gz<\/em><\/p>\n<p><em>#gunzip GeoIP.dat.gz<\/em><\/p>\n<p>Open \/etc\/httpd\/conf.d\/geoip.conf and paste below code:<\/p>\n<p>==========<br \/>\nLoadModule geoip_module modules\/mod_geoip.so<br \/>\n&lt;IfModule mod_geoip.c&gt;<br \/>\nGeoIPEnable On<br \/>\nGeoIPDBFile \/var\/lib\/GeoIP.dat<br \/>\n&lt;\/IfModule&gt;<br \/>\n==========<\/p>\n<p><em>#\/etc\/init.d\/apache restart<\/em><\/p>\n<p>Now the Mod_geoip module is installed and enabled in the server.<\/p>\n<p>Now lets see how to block the access on country basis to the websites hosted in our server.<\/p>\n<p>For blocking two countries say CN and PK, open .htaccess file of the website and paste the codes:<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nSetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry<br \/>\nSetEnvIf GEOIP_COUNTRY_CODE PK BlockCountry<br \/>\nDeny from env=BlockCountry<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>7.For allowing a country, say India, paste the below code in htaccess :<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nSetEnvIf GEOIP_COUNTRY_CODE IN is_allowed<br \/>\nDeny from all<br \/>\nAllow from env=is_allowed<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Thats all \ud83d\ude42<\/p>\n<p>If you require help, c<a href=\"https:\/\/www.supportpro.com\/requestquote.php\">ontact SupportPRO Server Admin<\/a><\/p>\n<p style=\"text-align: center;\"><!--HubSpot Call-to-Action Code --><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><!-- [if lte IE 8]><\/p>\n\n\n\n\n\n<div id=\"hs-cta-ie-element\"><\/div>\n\n\n<![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b\"><img decoding=\"async\" id=\"hs-cta-img-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-img\" style=\"border-width: 0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b.png\" alt=\"Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value\" \/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '9d590242-d641-4383-94b4-8cfd62f0af6b', {}); <\/script><\/span><!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you inspect your server&#8217;s error logs, you may see that many visitors (for the most part robots) constantly request access to things they should not be allowed to see.&hellip;<\/p>\n","protected":false},"author":5,"featured_media":16876,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,6,4],"tags":[],"class_list":["post-1326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general-topics","category-linux-basics","category-server-security"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=1326"}],"version-history":[{"count":2,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1326\/revisions"}],"predecessor-version":[{"id":3112,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1326\/revisions\/3112"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media\/16876"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=1326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=1326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=1326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}