0

Please need your help. Unable to display the custom error 403 page for geoip restriction. It is always showing default error page 403 Forbidden.

Here are my Configurations.

/etc/nginx/nginx.conf

geoip2 /usr/local/GeoLite2-Country_DB_Nginx/GeoLite2-Country.mmdb {
  auto_reload 60m;
  $geoip2_metadata_country_build metadata build_epoch;
  $geoip2_data_country_code country iso_code;
  $geoip2_data_country_name country names en;
}

map $geoip2_data_country_code $allowed_country {
  default no;
  PK yes;
  #DE yes;
  #US no;
}

geo $exclusions {
  default 0;
}

/etc/nginx/conf.d/www.test.com

error_page  403 /403.html;

location = /403.html {
  root   /var/www/html;
  allow all;
  internal;
}

if ($allowed_country = yes) {
  set $exclusions 1;
}

if ($exclusions = "0") {
  return 403;
}

I have placed the 403.html in /var/www/html and permission is 0644.

Thank you.

0

3 Answers 3

0

It looks like your location = /403.html block is not accessible. How about adding the if condition in your main location block, like this?

location ~ \.php$ {

    if ($exclusions = "0") {
      return 403;
      }
    root          /var/www/app/;
    ...
  }

Make sure you are restarting the Nginx server after making changes to the configuration files.

1
  • Do you mean in /etc/nginx/conf.d/www.test.com? Commented Feb 8, 2023 at 10:13
0

Do you mean in /etc/nginx/conf.d/www.test.com? I did these chnages.

if ($allowed_country = yes) { set $exclusions 1; }

location ~ .php$ {

if ($exclusions = "0") {
return 403;

}

root /var/www/html/; }

But it is even not blocking restricted Country now. Looking forward for quick support. Thank you in advance.

0

it's possible that the issue is caused by the allow all directive inside the location block for the custom error page.

You may want to remove the allow all directive and instead use the internal directive to ensure that the page is only accessible from within Nginx.

location = /403.html {
  root /var/www/html;
  internal;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .