{"id":1440,"date":"2018-10-23T07:23:21","date_gmt":"2018-10-23T07:23:21","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1440"},"modified":"2018-10-24T02:55:58","modified_gmt":"2018-10-24T02:55:58","slug":"block-countries-with-nginx-and-geoip","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/block-countries-with-nginx-and-geoip\/","title":{"rendered":"Block Countries With Nginx and GeoIP"},"content":{"rendered":"<p>You can use nginx to either allow or deny certain countries from accessing your site using the GeoIP database which maps IP addresses to the origin country.<\/p>\n<h2>Nginx GeoIP Requirements<\/h2>\n<p>Nginx Installation \u2013 Nginx must already be installed on your server if it is not yet. Please see <a href=\"https:\/\/linuxadmin.io\/nginx-compile-source\/\">How to Install Ngin<\/a>x<\/p>\n<p>Nginx must also be compiled with \u2013with-http_geoip_module<\/p>\n<p>To make sure type the following<\/p>\n<p>nginx -V 2&gt;&amp;1|grep &#8211;color=always with-http_geoip_module<\/p>\n<p>You should see the returned output contain with-http_geoip_module<\/p>\n<p>If it does not, you will need to change to the source direct copy the entire configuration line and append \u2013with-http_geoip_module to reconfigure<\/p>\n<p>.\/configure &#8211;user=nginx &#8211;group=nginx &#8211;prefix=\/etc\/nginx &#8211;sbin-path=\/usr\/sbin\/nginx &#8211;conf-path=\/etc\/nginx\/nginx.conf &#8211;pid-path=\/var\/run\/nginx.pid &#8211;lock-path=\/var\/run\/nginx.lock &#8211;error-log-path=\/var\/log\/nginx\/error.log &#8211;http-log-path=\/var\/log\/nginx\/access.log &#8211;with-http_gzip_static_module &#8211;with-http_stub_status_module &#8211;with-http_ssl_module &#8211;with-pcre &#8211;with-file-aio &#8211;with-http_realip_module &#8211;without-http_scgi_module &#8211;without-http_uwsgi_module &#8211;with-http_realip_module &#8211;with-http_geoip_module<\/p>\n<p>Then<\/p>\n<p>make &amp;&amp; make install<\/p>\n<h2>Install the GeoIP Database<\/h2>\n<p>Create a new directory for the GeoIP database to go:<\/p>\n<p>mkdir \/usr\/share\/geoip<\/p>\n<p>Change to that directory:<\/p>\n<p>cd \/usr\/share\/geoip<\/p>\n<p>Get the latest GeoIP database, this is the <a href=\"https:\/\/dev.maxmind.com\/geoip\/legacy\/geolite\/\">free \u2018lite\u2019 version<\/a>. MaxMind also offers paid versions as well.<\/p>\n<p>wget http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoLiteCountry\/GeoIP.dat.gz<\/p>\n<p>Gunzip the database:<\/p>\n<p>gunzip GeoIP.dat.gz<\/p>\n<h2>Configure Nginx<\/h2>\n<p>Nginx needs a global configuration and then to be told in each server block to restrict IP access.<\/p>\n<p>nano \/etc\/nginx\/nginx.conf<\/p>\n<p>You will want to insert the following in to the http{} block<\/p>\n<p>geoip_country \/usr\/share\/geoip\/GeoIP.dat;<br \/>\nmap $geoip_country_code $allow_country {<br \/>\ndefault yes;<br \/>\nEG no;<br \/>\nFR no;<br \/>\nFI no;<br \/>\n}<\/p>\n<p>Each country code you want to block would be indicated above. This wont create the actual block it will just create the map. Next you will want to edit the server{} block and add the following<\/p>\n<p>if ($allow_country = no) {<br \/>\nreturn 403;<br \/>\n}<\/p>\n<p>You will then save the file and restart nginx<\/p>\n<p>service nginx restart<\/p>\n<p>Now any countries you have set to \u2018no\u2019 will receive a 403 forbidden page. This could be switched to only allow certain countries, by setting the default to no and entering each country with a yes next to it that you wanted to allow.<\/p>\n<p>Jun 13, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/nginx-geoip-block-countries\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use nginx to either allow or deny certain countries from accessing your site using the GeoIP database which maps IP addresses to the origin country. Nginx GeoIP Requirements Nginx Installation \u2013 Nginx must already be installed on your server if it is not yet. Please see How to Install Nginx Nginx must also &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/block-countries-with-nginx-and-geoip\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Block Countries With Nginx and GeoIP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1440","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1440"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1440\/revisions"}],"predecessor-version":[{"id":1503,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1440\/revisions\/1503"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}