運維工程師必看!Nginx防火牆這樣配:IP自動封禁+CC攻擊防護,看完直呼內行

連結:https://www.cnblogs.com/hahaha111122222/p/18702355

在 Web 伺服器中,Nginx 提供了一種簡單而強大的方法來控制訪問,允許管理員基於 IP 地址來設定黑名單(禁止訪問)或白名單(僅允許特定 IP 訪問)。本教程將介紹如何在 Nginx 配置黑白名單,並提供具體示例。

allow、deny

deny和allow指令屬於ngx_http_access_module,nginx預設載入此模組,所以可直接使用。
在Nginx配置檔案中定義允許訪問系統的IP地址。假設您的Nginx配置檔案位於 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/default。

直接配置檔案中新增

server {    listen 80;    server_name your_domain_or_ip;# 設定白名單    location / {        allow 192.168.1.1;  # 允許特定IP訪問        allow 192.168.1.2;        allow 192.168.1.3;        allow 192.168.1.4;        allow 192.168.1.5;        allow 192.168.1.6;        deny all;  # 拒絕所有其他IP訪問    }# 設定最高許可權的執行維護IP    location /admin {        allow 192.168.1.7;  # 最高許可權的執行維護IP        deny all;    }# 設定有限許可權的維護IP    location /limited {        allow 192.168.1.8;  # 有限許可權的維護IP1        allow 192.168.1.9;  # 有限許可權的維護IP2        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#白名單IPallow 10.1.1.10;allow 10.1.1.11;

ngx_http_geo_module

預設情況下,一般nginx是有加該模組的,ngx_http_geo_module,引數需設定在位置在http模組中。此模組可設定IP限制,也可設定國家地區限制。位置在server模組外即可。

配置檔案直接新增

geo $ip_list {    default 0;#設定預設值為0192.168.1.0/241;10.1.0.0/161;}server {listen8081;    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 {default0;#設定預設值為0include 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 ) {return403;#限制的IP返回值為403,也可以設定為503,504其他值。#建議設定503,504這樣返回的頁面不會暴露nginx相關資訊,限制的IP看到的資訊只顯示伺服器錯誤,無法判斷真正原因。      }    }}

國家地區IP限制訪問

安裝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
下載 IP 資料庫
ngx_http_geoip_module模組依賴於IP資料庫,所有資料在此資料庫中讀取,需要下載ip庫(dat格式)。下載同時包括Ipv4和Ipv6的country、city版本。
#下載國家IP庫,解壓並移動到nginx配置檔案目錄,sudo wget https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gzgunzip maxmind.dat.gzsudo mv maxmind.dat /etc/nginx/GeoCountry.datsudo wget https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gzgunzip maxmind.dat.gzsudo mv maxmind.dat /etc/nginx/GeoCity.dat
配置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_country_code:兩位字元的英文國家碼。例如:CN, US$geoip_country_code3:三位字元的英文國家碼。例如:CHN, USA$geoip_country_name:國家英文全稱。例如:China, United States
城市相關引數
$geoip_city_country_code:兩位字元的英文國家碼。例如:CN, US$geoip_city_country_code3:三位字元的英文國家碼。例如:CHN, USA$geoip_city_country_name:國家英文全稱。例如:China, United States$geoip_region:地區程式碼,通常是兩位數的數字。例如:杭州是02, 上海是23$geoip_city:城市的英文名稱。例如:Hangzhou$geoip_postal_code:城市的郵政編碼。國內這欄位可能為空$geoip_city_continent_code:大洲程式碼。國內通常是AS$geoip_latitude:緯度$geoip_longitude:經度
END
官方站點:www.linuxprobe.com
 Linux命令大全:www.linuxcool.com

劉遄老師QQ:5604215
Linux技術交流群:2636170
(新群,火熱加群中……)
想要學習Linux系統的讀者可以點選"閱讀原文"按鈕來了解書籍《Linux就該這麼學》,同時也非常適合專業的運維人員閱讀,成為輔助您工作的高價值工具書!


相關文章