Last active
February 26, 2023 11:54
-
-
Save ydf/7feea1fd651c1c1fc3dd708836a12776 to your computer and use it in GitHub Desktop.
nginx block country ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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