title: How to block visitors from particular country with nginx and GeoIP Module
body: <p>I want to block a particular country's visitors to access my website <code>www.mainwebsite.com</code> through Nginx and GeoIP Module.</p>
  <p>First I tried on <code>www.test.com</code>. What steps I followed on test website,<code>www.test.com</code>, before trying on <code>www.mainwebsite.com</code></p>
  <ol>
  <li>Installing GeoIP:</li>
  </ol>
  <pre><code>sudo apt update &amp;&amp; sudo apt-get install geoip-database
  </code></pre>
  <ol start="2">
  <li>Check GeoIP Module is installed  or not:</li>
  </ol>
  <pre><code>nginx -V 2&gt;&amp;1|grep --color=always with-http_geoip_module
  </code></pre>
  <ol start="3">
  <li>Download the GeoIP Database:</li>
  </ol>
  <pre><code>sudo mkdir /etc/nginx/GeoIP/
  </code></pre>
  <p>Placed GeoIP.dat file to /etc/nginx/GeoIP/ location.</p>
  <ol start="4">
  <li>Configure Nginx and Virtual Host.
  sudo vi /etc/nginx/nginx.conf</li>
  </ol>
  <pre><code>http{
  ##
  # Basic Settings
  ##
        geoip_country /etc/nginx/GeoIP/GeoIP.dat;
        map $geoip_country_code $allowed_country {
          default yes;
          IN no;
        }
  
  }
  </code></pre>
  <p>Save and exit.</p>
  <ol start="5">
  <li>sudo vi /etc/nginx/site-available/test.com
  Added the map line in starting outside of server{......}</li>
  </ol>
  <pre><code>map $geoip_country_code $allowed_country {
   default yes;
   IN no;
  }
  </code></pre>
  <p>After that, inside server{......} setting, add the IF condition.</p>
  <pre><code>if ($allowed_country = no) {
       return 403;
   }
  </code></pre>
  <p>Save and exit.</p>
  <ol start="6">
  <li>Reload and restart nginx</li>
  </ol>
  <pre><code>sudo service nginx reload
  sudo service nginx restart
  </code></pre>
  <p>So <code>www.test.com</code> is directly hosted on Ec2 instance <code>test-server-01</code>  with public Network/IP, Blocking worked and users were not able to access from blocked country.</p>
  <p><code>www.mainwebsite.com</code> is hosted to classic load balancer and ec2 instances are attached to classic load balancer.</p>
  <p>For testing, I created 2 replica server of <code>test-server-01</code> server and created new load balancer and attached both replica servers behind the load balancer and pointed <code>www.test.com</code> to new load balancer. But Geo Country blocking didn't work so I added 2 below lines above IF condition which (If condition) is mentioned in point 5, then blocking worked.</p>
  <pre><code>real_ip_header X-Forwarded-For;
  set_real_ip_from 0.0.0.0/0;
  </code></pre>
  <p>Now I followed the same steps 1 to 6 for <code>www.mainwebsite.com</code> and made the changes in <code>nginx.conf</code> &amp; <code>/etc/nginx/site-available/mainwebsite.com</code> but country blocking didn't work.</p>
  <p>I have a doubt here that, for <code>www.test.com</code>, the contents of <code>/etc/nginx/site-available/test.com</code> and linked file <code>/etc/nginx/site-enabled/test.com</code> are same.</p>
  <p>But for <code>www.mainwebsite.com</code>, the content of files <code>/etc/nginx/site-available/mainwebsite.com</code> and <code>/etc/nginx/site-enabled/mainwebsite.com</code> are not same.</p>
  <p><code>/etc/nginx/site-enabled/mainwebsite.com</code> has some extra contents like:
  Outside of server{} block-</p>
  <pre><code># Expires map
  map $sent_http_content_type $expires {
      default                    off;
      text/html                  epoch;
      text/css                   max;
      application/javascript     max;
     ~image/                     max;
      application/font-woff      max;
  }
  </code></pre>
  <p>and inside the server{} block.</p>
  <pre><code>    server_name  www.mainwebsite.com;
      rewrite ^/blog/blogs$ https://www.mainwebsite.com/blogs permanent;
      rewrite ^/companies https://www.mainwebsite.com.com/company permanent;
      rewrite ^/events-2/* https://www.mainwebsite.com/events permanent;
  </code></pre>
  <p>Is this actual reason that's why country blocking is not working? Or there can be other reasons? Please help me out.</p>