網(wǎng)站如何通過nginx設(shè)置黑/白名單IP限制及國家城市IP訪問限制
目錄
- 一、黑/白名單IP限制訪問配置
- 1、第一種方法:allow、deny
- 2:第二種方法,ngx_http_geo_module
- 3、ngx_http_geo_module 負載均衡(擴展)
- 二、國家地區(qū)IP限制訪問
- 1:安裝ngx_http_geoip_module模塊
- 2、下載 IP 數(shù)據(jù)庫
- 3、配置nginx
- 總結(jié)
一、黑/白名單IP限制訪問配置
nginx配置黑白名單有好幾種方式,這里只介紹常用的兩種方法。
1、第一種方法:allow、deny
deny和allow指令屬于ngx_http_access_module,nginx默認(rèn)加載此模塊,所以可直接使用。
這種方式,最簡單,最直接。設(shè)置類似防火墻iptable,使用方法:
直接配置文件中添加:
#白名單設(shè)置,allow后面為可訪問IP location / { allow 123.13.123.12; allow 23.53.32.1/100; deny all; } #黑名單設(shè)置,deny后面接限制的IP,為什么不加allow all? 因為這個默認(rèn)是開啟的 location / { deny 123.13.123.12; } #白名單,特定目錄訪問限制 location /tree/list { allow 123.13.123.12; deny all; }
或者通過讀取文件IP配置白名單
location /{ include /home/whitelist.conf; #默認(rèn)位置路徑為/etc/nginx/ 下, #如直接寫include whitelist.conf,則只需要在/etc/nginx目錄下創(chuàng)建whitelist.conf deny all; }
在/home/目錄下創(chuàng)建whitelist.conf,并寫入需要加入白名單的IP,添加完成后查看如下:
cat /home/whitelist.conf #白名單IP allow 10.1.1.10; allow 10.1.1.11;
白名單設(shè)置完成,黑名單設(shè)置方法一樣。
2:第二種方法,ngx_http_geo_module
默認(rèn)情況下,一般nginx是有加該模塊的,ngx_http_geo_module:官方文檔,參數(shù)需設(shè)置在位置在http模塊中。
此模塊可設(shè)置IP限制,也可設(shè)置國家地區(qū)限制。位置在server模塊外即可。
語法示例:
配置文件直接添加
geo $ip_list { default 0; #設(shè)置默認(rèn)值為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 ) { #判斷默認(rèn)值,如果值為0,可訪問,這時上面添加的IP為黑名單。 #白名單,將設(shè)置$ip_list = 1,這時上面添加的IP為白名單。 proxy_pass http://192.168.152.100:8081; }
同樣可通過讀取文件IP配置
geo $ip_list { default 0; #設(shè)置默認(rèn)值為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,也可以設(shè)置為503,504其他值。 #建議設(shè)置503,504這樣返回的頁面不會暴露nginx相關(guān)信息,限制的IP看到的信息只顯示服務(wù)器錯誤,無法判斷真正原因。 }
在/etc/nginx目錄下創(chuàng)建ip_list.conf,添加IP完成后,查看如下:
cat /etc/nginx/ip_list.conf 192.168.152.1 1; 192.168.150.0/24 1;
設(shè)置完成,ip_list.conf的IP為白名單,不在名單中的,直接返回403頁面。黑名單設(shè)置方法相同。
3、ngx_http_geo_module 負載均衡(擴展)
ngx_http_geo_module,模塊還可以做負載均衡使用,如web集群在不同地區(qū)都有服務(wù)器,某個地區(qū)IP段,負載均衡至訪問某個地區(qū)的服務(wù)器。方式類似,IP后面加上自定義值,不僅僅數(shù)字,如US,CN等字母。
示例:
如果三臺服務(wù)器: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直接訪問此服務(wù)器 } upstream us.server { server 133.11.12.22:9090; #定義值us的IP直接訪問此服務(wù)器 } upstream default.server { server 144.11.11.33:9090; #默認(rèn)的定義值default的IP直接訪問此服務(wù)器 } server { listen 9090; server_name 144.11.11.33; location / { root /var/www/html/; index index.html index.htm; } }
然后在
二、國家地區(qū)IP限制訪問
有些第三方也提供設(shè)置,如cloudflare,設(shè)置更簡單,防火墻規(guī)則里設(shè)置。這里講講nginx的設(shè)置方法。
1:安裝ngx_http_geoip_module模塊
ngx_http_geoip_module:官方文檔,參數(shù)需設(shè)置在位置在http模塊中。
nginx默認(rèn)情況下不構(gòu)建此模塊,應(yīng)使用 --with-http_geoip_module 配置參數(shù)啟用它。
對于ubuntu系統(tǒng)來說,直接安裝 nginx-extras組件,包括幾乎所有的模塊。
sudo apt install nginx-extras
對于centos系統(tǒng),安裝模塊。
yum install nginx-module-geoip
2、下載 IP 數(shù)據(jù)庫
此模塊依賴于IP數(shù)據(jù)庫,所有數(shù)據(jù)在此數(shù)據(jù)庫中讀取,所有還需要下載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; #中國地區(qū),拒絕訪問。返回403頁面 } } }
這里,地區(qū)國家基礎(chǔ)設(shè)置就完成了。
Geoip其他參數(shù):
國家相關(guān)參數(shù):
$geoip_country_code #兩位字符的英文國家碼。如:CN, US
$geoip_country_code3 #三位字符的英文國家碼。如:CHN, USA
$geoip_country_name #國家英文全稱。如:China, United States
城市相關(guān)參數(shù):
$geoip_city_country_code #也是兩位字符的英文國家碼。
$geoip_city_country_code3 #上同
$geoip_city_country_name #上同.
$geoip_region #這個經(jīng)測試是兩位數(shù)的數(shù)字,如杭州是02, 上海是 23。但是沒有搜到相關(guān)資料,希望知道的朋友留言告之。
$geoip_city #城市的英文名稱。如:Hangzhou
$geoip_postal_code #城市的郵政編碼。經(jīng)測試,國內(nèi)這字段為空
$geoip_city_continent_code #不知什么用途,國內(nèi)好像都是AS
$geoip_latitude #緯度
$geoip_longitude #經(jīng)度
總結(jié)
到此這篇關(guān)于網(wǎng)站如何通過nginx設(shè)置黑/白名單IP限制及國家城市IP訪問限制的文章就介紹到這了,更多相關(guān)nginx黑/白名單IP限制設(shè)置內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章: