Skip to content

Instantly share code, notes, and snippets.

@ydf
Last active February 26, 2023 11:54
Show Gist options
  • Save ydf/7feea1fd651c1c1fc3dd708836a12776 to your computer and use it in GitHub Desktop.
Save ydf/7feea1fd651c1c1fc3dd708836a12776 to your computer and use it in GitHub Desktop.
nginx block country ip
# wget https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb
# http conf
geoip2 /etc/nginx/GeoLite2-Country.mmdb {
auto_reload 30m;
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country_code default=US country iso_code;
}
geoip2_proxy 1.1.1.0/24; # cdn ip
# server conf
set $block 0;
if ($geoip2_data_country_code ~ "(CN|RU)") {
set $block 1;
}
if ( $http_x_forwarded_for ~ "(white ip|ip)" ){
set $block 0;
}
if ( $block = 1 ){
return 403;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment