Score:0

Unable to Set Custom Error Page 403 for GeoIP

mg flag

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.

Score:0
in flag

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.

Muhammad Usman avatar
mg flag
Do you mean in /etc/nginx/conf.d/www.test.com?
Score:0
mg flag

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.

Score:0
br flag

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;
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.