openresty如何禁止海外ip訪問

????????前幾天,我有一個徒弟問我,如何禁止海外ip訪問他的網站系統?操作系統采用的是centos7.9,發布服務采用的是openresty。通過日志他發現,有很多類似以下數據

{"host":"172.30.7.95","clientip":"122.189.224.120","size":1818,"responsetime":2025-05-27T14:28:53,"http_host":"www.pingan.com","url":"/401.html","agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36","country_code":"CN","country_name":"China","city_name":"-","status":"401"}
{"host":"172.30.7.95","clientip":"199.204.97.58","size":154,"responsetime":2025-05-27T14:29:01,"http_host":"_","url":"-","agent":"-","country_code":"US","country_name":"United States","city_name":"-","status":"400"}
{"host":"172.30.7.95","clientip":"122.189.224.28","size":1818,"responsetime":2025-05-27T14:29:16,"http_host":"lsw-fast.lenovo.com.cn","url":"/401.html","agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36","country_code":"CN","country_name":"China","city_name":"-","status":"401"}
{"host":"172.30.7.95","clientip":"204.76.203.219","size":1818,"responsetime":2025-05-27T14:29:22,"http_host":"116.62.152.21","url":"/401.html","agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46","country_code":"NL","country_name":"The Netherlands","city_name":"-","status":"401"}
{"host":"172.30.7.95","clientip":"43.133.253.253","size":1818,"responsetime":2025-05-27T14:28:48,"http_host":"www.anxundata.com","url":"/401.html","agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1","country_code":"KR","country_name":"South Korea","city_name":"-","status":"401"}
{"host":"172.30.7.95","clientip":"43.133.253.253","size":1818,"responsetime":2025-05-27T14:28:48,"http_host":"www.anxundata.com","url":"/401.html","agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1","country_code":"KR","country_name":"South Korea","city_name":"-","status":"401"}
{"host":"172.30.7.95","clientip":"42.83.147.54","size":587,"responsetime":2025-05-27T14:33:12,"http_host":"anxundata.com","url":"/index.html","agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/74.0.3729.169 Safari/537.36","country_code":"CN","country_name":"China","city_name":"-","status":"200"}
{"host":"172.30.7.95","clientip":"45.82.255.188","size":1818,"responsetime":2025-05-27T14:36:35,"http_host":"anxundata.com","url":"/401.html","agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36","country_code":"CY","country_name":"Cyprus","city_name":"-","status":"401"}

? ? ? ? 我看過數據之后,直接給出答案使用geoip2模塊與GeoLite2的ip庫匹配禁止訪問就行。下面就是geoip2模塊與GeoLite2的ip庫安裝步驟(以下步驟是基于openresty已經安裝成功的操作手法)。

一、增加依賴

yum install -y geoip-devel libmaxminddb-devel

二、geoip2模塊安裝

wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz #下載geoip2模塊
tar -zxf ngx_http_geoip2_module-3.4.tar.gz #解壓文件包
mv ngx_http_geoip2_module-3.4/ /usr/local/src/ngx_http_geoip2_module #將文件拷貝到指定目錄

三、下載及安裝GeoLite2的ip庫

tar -zxf GeoLite2-Country_20250523.tar.gz #將文件解壓
cd GeoLite2-Country_20250523/ #進入文件
mv GeoLite2-Country.mmdb /usr/share/GeoIP/ #將IP庫復制到GeoIP文件夾中

四、重新編譯openresty

./configure --prefix=/usr/local/openresty --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-compat --with-pcre-jit --with-stream --with-http_geoip_module --add-module=/usr/local/src/ngx_http_geoip2_module
gmake && gmake install

五、修改nginx主配置

vi /usr/local/openresty/nginx/conf/nginx.conf
user www;
worker_processes 2;
worker_cpu_affinity auto;
error_log /data/nginx/error.log;
worker_rlimit_nofile 65535;
pid  /data/nginx/nginx.pid;events {worker_connections  10240;use epoll;multi_accept on;
}http {include       mime.types;geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {auto_reload 60m; #每隔60分鐘自動重載數據庫$geoip2_data_country_code country iso_code; #IP所在-國家的編碼$geoip2_data_country_name country names en; #IP所在-國家名$geoip2_data_city_name city names; #IP所在-城市名}default_type  application/octet-stream;map $time_iso8601 $logdate {'~^(?<ymd>\\d{4}-\\d{2}-\\d{2})' $ymd;default                       'date-not-found';}map $time_iso8601 $request_times {'~^(?<ymdhms>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})' $ymdhms;default                       'date-not-found';}map $geoip2_data_country_code $allowed_country {default no; #默認禁止所有訪問CN yes; #只針對中國開放訪問權限}log_format access-upstream '{"host":"$server_addr",''"clientip":"$remote_addr",''"size":$body_bytes_sent,''"responsetime":$request_times,''"http_host":"$host",''"url":"$uri",''"agent":"$http_user_agent",''"country_code":"$geoip2_data_country_code",''"country_name":"$geoip2_data_country_name",''"city_name":"$geoip2_data_city_name",''"status":"$status"}';sendfile on;tcp_nopush on;tcp_nodelay on;server_names_hash_bucket_size 128;server_names_hash_max_size 512;keepalive_timeout  1000s;client_header_timeout 15s;client_body_timeout 15s;send_timeout 1000s;proxy_cache_path /data/nginx levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;client_body_buffer_size 512k;client_header_buffer_size 256k;client_max_body_size 1024m;large_client_header_buffers 2 8k;proxy_connect_timeout 1000s;proxy_send_timeout 1000s;proxy_read_timeout 1000s;proxy_buffer_size 128k;proxy_buffers 8 256k;proxy_busy_buffers_size 256k;proxy_temp_file_write_size 256k;proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;fastcgi_intercept_errors on;fastcgi_ignore_client_abort on;fastcgi_connect_timeout 1000s;fastcgi_send_timeout 1000s;fastcgi_read_timeout 1000s;fastcgi_buffer_size 128k;fastcgi_buffers 8 256k;fastcgi_busy_buffers_size 256k;fastcgi_temp_file_write_size 256k;gzip on;gzip_min_length 1k;gzip_buffers 4 32k;gzip_http_version 1.1;gzip_comp_level 6;gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;gzip_vary on;gzip_disable "MSIE [1-6].";include vhost/*.conf;
}

六、修改具體域名配置

location / {if ($allowed_country = no) {return 401;}index /data/wwwroot/index.html;
}

下一期將帶來openresty+lua+redis把非正常訪問的域名加入黑名單

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/82535.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/82535.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/82535.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

理解 Redis 事務-20 (MULTI、EXEC、DISCARD)

理解 Redis 事務&#xff1a;MULTI、EXEC、DISCARD Redis 事務允許你將一組命令作為一個單一的原子操作來執行。這意味著事務中的所有命令要么全部執行&#xff0c;要么全部不執行。這對于在需要一起執行多個操作時保持數據完整性至關重要。本課程將涵蓋 Redis 事務的基礎知識…

Milvus分區-分片-段結構詳解與最佳實踐

導讀&#xff1a;在構建大規模向量數據庫應用時&#xff0c;數據組織架構的設計往往決定了系統的性能上限。Milvus作為主流向量數據庫&#xff0c;其獨特的三層架構設計——分區、分片、段&#xff0c;為海量向量數據的高效存儲和檢索提供了堅實基礎。 本文通過圖書館管理系統的…

Kettle 遠程mysql 表導入到 hadoop hive

kettle 遠程mysql 表導入到 hadoop hive &#xff08;教學用 &#xff09; 文章目錄 kettle 遠程mysql 表導入到 hadoop hive創建 對象 執行 SQL 語句 -mysql 導出 CSV格式CSV 文件遠程上傳到 HDFS運行 SSH 命令遠程登錄 run SSH 并執行 hadoop fs -put 建表和加載數據總結 創…

Linux輸出命令——echo解析

摘要 全面解析Linux echo命令核心功能&#xff0c;涵蓋文本輸出、變量解析、格式控制及高級技巧&#xff0c;助力提升Shell腳本開發與終端操作效率。 一、核心功能與定位 作為Shell腳本開發的基礎工具&#xff0c;echo命令承擔著信息輸出與數據傳遞的重要角色。其主要功能包…

Windows系統下 NVM 安裝 Node.js 及版本切換實戰指南

以下是 Windows 11 系統下使用 NVM 安裝 Node.js 并實現版本自由切換的詳細步驟&#xff1a; 一、安裝 NVM&#xff08;Node Version Manager&#xff09; 1. 卸載已有 Node.js 如果已安裝 Node.js&#xff0c;請先卸載&#xff1a; 控制面板 ? 程序與功能 ? 找到 Node.js…

【leetcode】977. 有序數組的平方

有序數組的平方 題目代碼1. 使用sorted2. 雙指針 題目 977. 有序數組的平方 給你一個按 非遞減順序 排序的整數數組 nums&#xff0c;返回 每個數字的平方 組成的新數組&#xff0c;要求也按 非遞減順序 排序。 示例 1&#xff1a; 輸入&#xff1a;nums [-4,-1,0,3,10] 輸…

Obsidian 數據可視化深度實踐:用 DataviewJS 與 Charts 插件構建智能日報系統

Obsidian 數據可視化深度實踐&#xff1a;用 DataviewJS 與 Charts 插件構建智能日報系統 一、核心架構解析 本系統基于 Obsidian 的 DataviewJS 和 Charts 插件&#xff0c;實現日報數據的自動采集、可視化分析及智能回溯功能&#xff08;系統架構原理見&#xff09;。其技術…

深入解析Spring Boot與Kafka集成:構建高效消息驅動應用

深入解析Spring Boot與Kafka集成&#xff1a;構建高效消息驅動應用 引言 在現代分布式系統中&#xff0c;消息隊列是實現異步通信和解耦的關鍵技術之一。Apache Kafka作為一款高性能、分布式的消息隊列系統&#xff0c;廣泛應用于大數據和實時數據處理場景。本文將詳細介紹如…

Rust 學習筆記:關于生命周期的練習題

Rust 學習筆記&#xff1a;關于生命周期的練習題 Rust 學習筆記&#xff1a;關于生命周期的練習題生命周期旨在防止哪種編程錯誤&#xff1f;以下代碼能否通過編譯&#xff1f;若能&#xff0c;輸出是&#xff1f;如果一個引用的生命周期是 static&#xff0c;這意味著什么&…

word解決不同文檔同樣的字體段落設置下看起來行距不同的問題

問題&#xff1a; 有時候我們照著模板修改文檔格式&#xff0c;明明字體和段落設置一模一樣&#xff0c;但是看起來行距不一樣。 解決辦法&#xff1a; 一般照著模板修改文檔內容&#xff0c;要注意以下幾點&#xff0c;如果以下幾點與模板設置相同時就可解決上述問題 1、紙…

Jenkins實踐(9):配置“構建歷史的顯示名稱,加上包名等信息“

Jenkins實踐(9):配置“構建歷史的顯示名稱,加上包名等信息“ 版本:Jenkins 4.262.2 需求:想要在構建歷史中展示,本次運行的是哪個版本或哪個包 操作步驟: 1、先安裝插件Build Name and Description Setter 2、Set Build Name 3、構建歷史處查看展示 插件特性說明 安裝依賴…

matIo庫及.mat數據格式介紹

一.概述 1..mat數據格式 &#xff08;1&#xff09;.mat 是 MATLAB 軟件的標準二進制數據存儲格式&#xff0c;用于保存變量、矩陣、數組、結構體等數據類型。其名稱源于 “MATLAB Data” 的縮寫&#xff0c;最初設計為高效存儲和加載 MATLAB 環境中的數據&#xff0c;后來逐…

企業級調度器LVS (面試版)

1. 什么是 LVS?有什么作?? LVS ( Linux Virtual Server )是?個基于 Linux 內核實現的?性能、可擴展和可靠的負載均衡。它將多個服務器組成?個?可?、?性能和?可靠的虛擬服務器集群,通過將客戶端的請求轉發到不同的后端服務器,實現負載均衡和?可?性。 2.什么是 …

用python制作一個簡易的聊天室軟件

文章目錄 效果圖python源碼使用說明效果圖 只需要一百多行的python代碼,就能制作一個簡易的聊天室軟件。效果如下: 操作說明: 1、先運行server.py啟動服務器; 2、每運行一次client.py可以創建一個聊天用戶(需要輸入用戶昵稱); 3、輸入對方的昵稱即可與其聊天,輸入“a…

Android13 開機時間優化

前言 在實際應用場景中&#xff0c;特定領域對 Android 系統的啟動時間有著極為嚴苛的要求&#xff0c;車載領域便是典型代表。想象一下&#xff0c;當車輛已經行駛出數公里之遙&#xff0c;車內的信息娛樂系統&#xff08;IVI&#xff09;卻仍未完成啟動&#xff0c;這無疑會…

08SpringBoot高級--自動化配置

目錄 Spring Boot Starter 依賴管理解釋 一、核心概念 二、工作原理 依賴傳遞&#xff1a; 自動配置&#xff1a; 版本管理&#xff1a; 三、核心流程 四、常用 Starter 示例 五、自定義 Starter 步驟 創建配置類&#xff1a; 配置屬性&#xff1a; 注冊自動配置&a…

基于cornerstone3D的dicom影像瀏覽器 第二十四章 顯示方位、坐標系、vr輪廓線

系列文章目錄 文章目錄 系列文章目錄前言一、工具欄修改二、切片窗口顯示方位文字1. 修改mprvr.js&#xff0c;添加函數getOrientationMarkers2. 修改DisplayerArea3D.vue 三、vr窗口顯示坐標系1. 修改mprvr.js 添加OrientationMarkerTool2. view3d.vue中響應工具欄事件3. 修改…

【C/C++】線程局部存儲:原理與應用詳解

文章目錄 1 基礎概念1.1 定義1.2 初始化規則1.3 全局TLS vs 局部靜態TLS 2 內存布局2.1 實現機制2.2 典型內存結構2.3 性能特點 3 使用場景/用途3.1 場景3.2 用途 4 注意事項5 對比其他技術6 示例代碼7 建議7.1 調試7.2 優化 8 學習資料9 總結 在 C 多線程編程中&#xff0c;線…

【圖像大模型】IP-Adapter:圖像提示適配器的技術解析與實踐指南

IP-Adapter&#xff1a;圖像提示適配器的技術解析與實踐指南 一、項目背景與技術價值1.1 圖像生成中的個性化控制需求1.2 IP-Adapter的核心貢獻 二、技術原理深度解析2.1 整體架構設計2.2 圖像特征編碼器2.3 訓練策略 三、項目部署與實戰指南3.1 環境配置3.2 模型下載3.3 基礎生…

MySQL-5.7 修改密碼和連接訪問權限

一、MySQL-5.7 修改密碼和連接權限設置 修改密碼語法 注意&#xff1a;rootlocalhost 和 root192.168.56.% 是兩個不同的用戶。在修改密碼時&#xff0c;兩個用戶的密碼是各自分別保存&#xff0c;如果兩個用戶密碼設置不一樣則登陸時注意登陸密碼 GRANT ALL PRIVILEGES ON …