Is there a way to Geofence a Site / Restrict Access by Location?

I’m looking for a way to restrict access to my site by location. For examples:

  1. A prospective user does not have an IP address that is located within 160 Km of a certain city, then netlify should not serve the site.

  2. A prospective user does not live within a certain country, then netlify should not serve the site.

  3. A prospective user lives within 160 Km of a certain city, then netlify should serve the site.

Or any other way to control where prospective users are coming from while my site is active on netlify would be of help.

1 Like

Hi there,

to the best of my knowledge it is not possible to restrict access the way you are describing on our service.

I’m scratching my head as to how we could help you, and one thing we do have is country based redirects:

I know this is unlikely to be as precise as you are looking for, but maybe it is helpful?

Hi @MS00-GitIt - I have a similar requirement to restrict the users accessing the site from certain countries and I was wondering if you managed to find a solution to the problem? We were considering using some Country DB of IP Addresses to restrict access.

@MS00-GitIt similarly, I am looking to build an eCommerce site but will not be selling outside the U.S. and given GDPR’s restrictions, I’d prefer to limit it to U.S. visitors only, to save potential headaches.

Hi @jcoyle37,

This is still not possible unfortunately. The team is working on a possibility that might be available sometime in the future, but at the moment, such option doesn’t exist, at least not with a fool-proof method.

Any updates on this feature? Need to have the ability to restrict access to certain countries due to regulatory issues

Hi @redhair

Definitely possible now with Edge Functions

Here’s the example that may be useful to your case:

Hope this helps!

2 Likes

This worked perfectly. Thank you for the quick reply. Here’s the gist to block anyone in the United States or Cuba:

export default async function geoBlock(req, { geo }) {
  if (
    geo.country.code !== 'US' || 
    geo.country.code !== 'CU'
  ) {
    return;
  }

  const html = `
    <!DOCTYPE html>
    <html lang="en">
      <body>
        Sorry, we are unavailable in ${geo.country.name}
      </body>
    </html>
  `;

  return new Response(html, {
    headers: { 'content-type': 'text/html' },
  });
}
1 Like

Great! Thanks for sharing that @redhair

Glad was able to help!