一、黑/白名單IP限制訪問配置
nginx配置黑白名單有好幾種方式,這里只介紹常用的兩種方法。
1、第一種方法:allow、deny
deny和allow指令屬于ngx_http_access_module,nginx默認加載此模塊,所以可直接使用。
這種方式,最簡單,最直接。設置類似防火墻iptable,使用方法:
直接配置文件中添加:
#白名單設置,allow后面為可訪問IP? location?/?{ ?????allow?123.13.123.12; ?????allow?23.53.32.1/100; ?????deny??all; } #黑名單設置,deny后面接限制的IP,為什么不加allow?all??因為這個默認是開啟的? location?/?{ ?????deny?123.13.123.12; } #白名單,特定目錄訪問限制 location?/tree/list?{ ?????allow?123.13.123.12; ?????deny??all; }
或者通過讀取文件IP配置白名單
location?/{ ????include?/home/whitelist.conf; ????#默認位置路徑為/etc/nginx/?下, ????#如直接寫include?whitelist.conf,則只需要在/etc/nginx目錄下創建whitelist.conf ????deny?all; }
在/home/目錄下創建whitelist.conf,并寫入需要加入白名單的IP,添加完成后查看如下:
cat?/home/whitelist.conf #白名單IP allow?10.1.1.10; allow?10.1.1.11;
白名單設置完成,黑名單設置方法一樣。
2:第二種方法,ngx_http_geo_module
默認情況下,一般nginx是有加該模塊的,ngx_http_geo_module:官方文檔,參數需設置在位置在http模塊中。
此模塊可設置IP限制,也可設置國家地區限制。位置在server模塊外即可。
語法示例:
配置文件直接添加
geo?$ip_list?{ ????default?0; ????#設置默認值為0 ????192.168.1.0/24?1; ????10.1.0.0/16????1; } server?{ ????listen???????8081; ????server_name??192.168.152.100; ???? ????location?/?{ ????????root???/var/www/test; index??index.html?index.htm?index.php; if?(?$ip_list?=?0?)?{ #判斷默認值,如果值為0,可訪問,這時上面添加的IP為黑名單。 #白名單,將設置$ip_list?=?1,這時上面添加的IP為白名單。 proxy_pass?http://192.168.152.100:8081; ????}
同樣可通過讀取文件IP配置
geo?$ip_list?{ ????default?0; ????#設置默認值為0 ????include?ip_white.conf; } server?{ ????listen???????8081; ????server_name??192.168.152.100; ???? ????location?/?{ ????????root???/var/www/test; index??index.html?index.htm?index.php; if?(?$ip_list?=?0?)?{ return?403; #限制的IP返回值為403,也可以設置為503,504其他值。 #建議設置503,504這樣返回的頁面不會暴露nginx相關信息,限制的IP看到的信息只顯示服務器錯誤,無法判斷真正原因。 ????}
在/etc/nginx目錄下創建ip_list.conf,添加IP完成后,查看如下:
cat?/etc/nginx/ip_list.conf 192.168.152.1?1; 192.168.150.0/24?1;
當設置完成后,IP列表文件 ip_list.conf 將作為白名單,若請求的 IP 不在名單中,則會直接返回403頁面。黑名單設置方法相同。
3、ngx_http_geo_module 負載均衡(擴展)
ngx_http_geo_module,模塊還可以做負載均衡使用,如web集群在不同地區都有服務器,某個地區IP段,負載均衡至訪問某個地區的服務器。類似的方式是在IP后面添加自定義值,這些值不僅限于數字,還可以使用字母,例如US、CN等。
示例:
如果三臺服務器:122.11.11.11,133.11.12.22,144.11.11.33
geo?$country?{ ????default?default; ????111.11.11.0/24???uk; ????#IP段定義值uk ????111.11.12.0/24???us; ????#IP段定義值us ????} upstream??uk.server?{ ????erver?122.11.11.11:9090; ????#定義值uk的IP直接訪問此服務器 }? upstream??us.server?{ ????server?133.11.12.22:9090; ????#定義值us的IP直接訪問此服務器 } upstream??default.server?{ ????server?144.11.11.33:9090; ????#默認的定義值default的IP直接訪問此服務器 } ? server?{ ????listen????9090; ????server_name?144.11.11.33; ????location?/?{ ??????root??/var/www/html/; ??????index?index.html?index.htm; ?????} ?}
然后在
二、國家地區IP限制訪問
一些第三方服務例如cloudflare也提供設置選項,使防火墻規則的設置更加方便。這里講講nginx的設置方法。
1:安裝ngx_http_geoip_module模塊
ngx_http_geoip_module:官方文檔,參數需設置在位置在http模塊中。
nginx默認情況下不構建此模塊,應使用 –with-http_geoip_module 配置參數啟用它。
對于ubuntu系統來說,直接安裝 nginx-extras組件,包括幾乎所有的模塊。
sudo?apt?install?nginx-extras
對于centos系統,安裝模塊。
yum?install?nginx-module-geoip
2、下載 IP 數據庫
此模塊依賴于IP數據庫,所有數據在此數據庫中讀取,所有還需要下載ip庫(dat格式)。
MaxMind 提供了免費的 IP 地域數據庫,壞消息是MaxMind 官方已經停止支持dat格式的ip庫。
在其他地方可以找到dat格式的文件,或者老版本的,當然數據不可能最新,多少有誤差。
下載同時包括Ipv4和Ipv6的country、city版本。
#下載國家IP庫,解壓并移動到nginx配置文件目錄, sudo?wget?https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz gunzip?maxmind.dat.gz sudo?mv?maxmind.dat?/etc/nginx/GeoCountry.dat sudo?wget?https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz gunzip?maxmind.dat.gz sudo?mv?maxmind.dat?/etc/nginx/GeoCity.dat
3、配置nginx
示例:
geoip_country?/etc/nginx/GeoCountry.dat; geoip_city?/etc/nginx/GeoCity.dat; server?{ ????listen??80; ????server_name?144.11.11.33; ????location?/?{ ??????root??/var/www/html/; ??????index?index.html?index.htm; ??????if?($geoip_country_code?=?CN)?{ ?? return?403; ? #中國地區,拒絕訪問。返回403頁面 } ?? } ?}
這里,地區國家基礎設置就完成了。
Geoip其他參數:
國家相關參數:$geoip_country_code #兩位字符的英文國家碼。如:CN, US$geoip_country_code3 #三位字符的英文國家碼。如:CHN, USA$geoip_country_name #國家英文全稱。如:China, United States城市相關參數:$geoip_city_country_code #也是兩位字符的英文國家碼。$geoip_city_country_code3 #上同$geoip_city_country_name #上同.$geoip_region #這個經測試是兩位數的數字,如杭州是02, 上海是 23。但是沒有搜到相關資料,希望知道的朋友留言告之。$geoip_city #城市的英文名稱。如:Hangzhou$geoip_postal_code #城市的郵政編碼。經測試,國內這字段為空$geoip_city_continent_code #不知什么用途,國內好像都是AS$geoip_latitude #緯度$geoip_longitude #經度