国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

如何通過openresty 限制國外Ip訪問

這篇具有很好參考價值的文章主要介紹了如何通過openresty 限制國外Ip訪問。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

參考代碼

https://gitee.com/xiaoyun461/blocking-external-networks

首先 需要的依賴:

libmaxminddb

https://github.com/maxmind/libmaxminddb

maxmind-geoip

https://github.com/Dreamacro/maxmind-geoip

libmaxminddb 需要gcc編譯,可用 Dockerfile 里面編譯生成so文件,然后復(fù)制到 openresty/openresty:centos-rpm 鏡像中,然后把 Country.mmdb(Ip數(shù)據(jù)庫) 也復(fù)制到鏡像中

Dockerfile 如下:

############## 構(gòu)建 libmaxminddb #####################
FROM gcc:9 AS libmaxminddb-build
ADD lib/libmaxminddb-1.8.0.tar.gz /
WORKDIR /libmaxminddb-1.8.0
RUN ./configure && make && make install && ldconfig -v && ls -f  /usr/local/lib/libmaxminddb*

############## 構(gòu)建 openresty #####################
FROM openresty/openresty:centos-rpm
ENV TZ Asia/Shanghai
COPY --from=libmaxminddb-build   /usr/local/lib/libmaxminddb.so.0.0.7 /lib64
COPY  geoip/20231212/Country.mmdb /etc/nginx/mmdb/Country.mmdb

RUN  ln -snf /usr/share/zoneinfo/"$TZ" /etc/localtime && echo "$TZ" > /etc/timezone \
     && opm get anjia0532/lua-resty-maxminddb \
     && ln -s /lib64/libmaxminddb.so.0.0.7 /lib64/libmaxminddb.so \
     && ldconfig -v

然后配置lua腳本,OpenResty(也稱為 ngx_openresty)是一個基于 Nginx 與 Lua 的高性能 Web 平臺,

lua腳本如下:

local function get_client_ip()
    local headers = ngx.req.get_headers()
    local clientIP = headers["x-forwarded-for"]
    if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then
        clientIP = headers["Proxy-Client-IP"]
    end
    if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then
        clientIP = headers["WL-Proxy-Client-IP"]
    end
    if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then
        clientIP = ngx.var.remote_addr
    end
    -- 對于通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照','分割
    if clientIP ~= nil and string.len(clientIP) > 15 then
        local pos = string.find(clientIP, ",", 1)
        clientIP = string.sub(clientIP, 1, pos - 1)
    end
    return clientIP;
end

local function check_cn(ip)
    local geo = require 'resty.maxminddb'
    if not geo.initted() then
        geo.init("/etc/nginx/mmdb/Country.mmdb")
    end
    local res, err = geo.lookup(ip)
    if not res then
        ngx.log(ngx.ERR, ' failed to lookup by ip , reason :', err)
    else
        for k, v in pairs(res) do
            if (k == "country") then
                for key, item in pairs(v) do
                    if (key == "iso_code") then
                        if item == "CN" then
                            ngx.log(ngx.INFO, ' this counrty: ', item)
                            return 1;
                        else
                            ngx.log(ngx.ERR, ' this counrty: ', item)
                            return 0;
                        end
                    end
                end
            end
        end
    end
end



-- 獲取nginx 本地緩存
local cache_ngx = ngx.shared.dis_cache;
-- 獲取 請求IP
local clientIP = get_client_ip();


--根據(jù)IP 獲取本地黑名單緩存數(shù)據(jù)
local banIpCache = cache_ngx:get('ban_ip_' .. clientIP);
if banIpCache == 1 then
    ngx.log(ngx.ERR, "cache_black_ip:", clientIP)
    ngx.exit(403)
else
    -- 判斷是否是國外IP, 直接設(shè)為黑名單,并且返回403
    local flag = check_cn(clientIP);
    if flag == 0 then
        -- 本地緩存黑名單 時間1小時
        ngx.log(ngx.ERR, "set_local_black_ip:", clientIP)
        cache_ngx:set('ban_ip_' .. clientIP, 1, 60 * 60);
        ngx.exit(403)
    end
end




nginx.conf 配置文件 也要加入 lua腳本校驗,以及 增加128M的本地緩存,方便過濾

nginx.conf如下:

pcre_jit on;



#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

error_log  logs/error.log  warn;

#pid        logs/nginx.pid;


worker_processes auto;

events {
    worker_connections  20480;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    # Enables or disables the use of underscores in client request header fields.
    # When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.
    # underscores_in_headers off;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /dev/stdout  main;



#         Log in JSON Format
        log_format nginxlog_json escape=json '{ "timestamp": "$time_local", '
        '"remote_addr": "$remote_addr", '
        '"remote_user": "remote_user", '
         '"body_bytes_sent": $body_bytes_sent, '
         '"request_time": $request_time, '
         '"response_status": $status, '
         '"request": "$request", '
         '"request_method": "$request_method", '
         '"host": "$host",'
         '"upstream_addr": "$upstream_addr",'
         '"upstream_host": "$upstream_http_host",'
         '"upstream_resp_time": "$upstream_response_time",'
         '"http_x_forwarded_for": "$http_x_forwarded_for",'
         '"http_referrer": "$http_referer", '
         '"http_user_agent": "$http_user_agent", '
         '"http_version": "$server_protocol" ';

	map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default                       'date-not-found';
    }

    access_log logs/access_json-$logdate.log  nginxlog_json;

    # See Move default writable paths to a dedicated directory (#119)
    # https://github.com/openresty/docker-openresty/issues/119
    client_body_temp_path /var/run/openresty/nginx-client-body;
    proxy_temp_path       /var/run/openresty/nginx-proxy;
    fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;
    uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;
    scgi_temp_path        /var/run/openresty/nginx-scgi;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    client_max_body_size 150m;

#     lua cache
    lua_shared_dict dis_cache 128m;

    gzip on;
    gzip_static  on;
    gzip_min_length 1k;     # 設(shè)置允許壓縮的頁面最小字節(jié)數(shù)
    gzip_buffers 4 16k;     # 用來存儲 gzip 的壓縮結(jié)果
    gzip_http_version 1.1;  # 識別 HTTP 協(xié)議版本
    gzip_comp_level 2;      # 設(shè)置 gzip 的壓縮比 1-9。1 壓縮比最小但最快,而 9 相反
    gzip_types gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 指定壓縮類型
    gzip_proxied any;       # 無論后端服務(wù)器的 headers 頭返回什么信息,都無條件啟用壓縮

    # 限制國外ip
    access_by_lua_file /etc/nginx/lua/access_limit_open.lua;





    include /etc/nginx/conf.d/*.conf;

}


全部 樣例代碼 在

https://gitee.com/xiaoyun461/blocking-external-networks

可自行修改文章來源地址http://www.zghlxwxcb.cn/news/detail-790077.html

到了這里,關(guān)于如何通過openresty 限制國外Ip訪問的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Windows server防火墻如何設(shè)置阻止IP訪問防火墻限制ip地址訪問

    Windows server防火墻如何設(shè)置阻止IP訪問防火墻限制ip地址訪問

    無論是服務(wù)器還是本機電腦,安全都是非常重要的,一些安全軟件設(shè)置后會攔截到一些異常IP,我們可以通過防火墻將異常IP加入黑名單,禁止訪問,今天 芳芳 就和你們分享Windows防火墻如何設(shè)置IP禁止訪問,希望可以幫助到你~ 打開“控制面板”,在右上角的查看方式改為“

    2024年02月13日
    瀏覽(34)
  • 阿里云的白名單規(guī)則如何實現(xiàn)IP限制和訪問控制?

    阿里云的白名單規(guī)則如何實現(xiàn)IP限制和訪問控制?

    阿里云的白名單規(guī)則如何實現(xiàn)IP限制和訪問控制? [本文由阿里云代理商 [聚搜云] 撰寫] 隨著企業(yè)在云計算領(lǐng)域的深入應(yīng)用,網(wǎng)絡(luò)安全問題日益凸顯。阿里云提供了一種名為“白名單”的規(guī)則,幫助用戶實現(xiàn)IP限制和訪問控制。本文將詳細闡述阿里云白名單規(guī)則的原理及操作方

    2024年02月10日
    瀏覽(30)
  • iptables結(jié)合ipset禁止國外IP進行訪問

    iptables結(jié)合ipset禁止國外IP進行訪問

    0x01 ipset 可以使用 iptables 對訪問地址進行一定的限制,但是當受限地址數(shù)量過多時,維護管理起來就不太方便,而且數(shù)量過多的 iptables 條目也會影響設(shè)備的性能。此時就可以使用 ipset 工具來解決此種問題。 ipset 理解起來比較容易,理解為地址組就好,具有相同作用的地址段

    2024年02月06日
    瀏覽(31)
  • 用 Nginx 禁止國外 IP 訪問我的網(wǎng)站...

    用 Nginx 禁止國外 IP 訪問我的網(wǎng)站...

    先來說說為啥要寫這篇文章,之前看了下 Nginx 的訪問日志,發(fā)現(xiàn)每天有好多國外的 IP 地址來訪問我的網(wǎng)站,并且訪問的內(nèi)容基本上都是惡意的。因此我決定禁止國外 IP 來訪問我的網(wǎng)站。 想要實現(xiàn)這個功能有很多方法,下面我就來介紹基于 Nginx 的 ngx_http_geoip2 模塊來禁止國外

    2024年02月13日
    瀏覽(26)
  • 基于Geoip2實現(xiàn)Nginx攔截國外IP訪問網(wǎng)站

    基于Geoip2實現(xiàn)Nginx攔截國外IP訪問網(wǎng)站

    最近公司上線了一個APP,過段時間發(fā)現(xiàn)告警群總有些莫名的異常,通過排查發(fā)現(xiàn)是被攻擊了,并且攻擊IP全是國外的,基于APP業(yè)務(wù)全在國內(nèi),最簡單辦法就是屏蔽這些IP。云上雖有產(chǎn)品但收費,自己動手才是王道。 目錄 一、實現(xiàn)思路 二、配置方法? ? 2.1 軟件下載 2.1.1 Nginx下

    2024年01月16日
    瀏覽(17)
  • Internet通過TCP/IP協(xié)議可以實現(xiàn)多個網(wǎng)絡(luò)的無縫連接

    Internet通過TCP/IP(Transmission Control Protocol/Internet Protocol,傳輸控制協(xié)議/互聯(lián)網(wǎng)協(xié)議)協(xié)議實現(xiàn)多個網(wǎng)絡(luò)的無縫連接 。 TCP/IP是Internet的基礎(chǔ)通信協(xié)議套件,它定義了數(shù)據(jù)如何在不同網(wǎng)絡(luò)之間傳輸和路由,使得全球范圍內(nèi)的不同計算機和網(wǎng)絡(luò)可以互相通信。TCP/IP協(xié)議套件包括多個

    2024年02月07日
    瀏覽(24)
  • 如何通過IP訪問MySQL數(shù)據(jù)庫

    1.1 改表法 如果不從遠程登陸,可以用 localhost 。這個時候只要在 localhost 的那臺電腦,登入 mysql 后,更改 \\\"mysql\\\" 數(shù)據(jù)庫中 \\\"user\\\" 表里的 \\\"host\\\" 字段,把 \\\"localhost\\\" 改稱 \\\"%\\\" ,即可。 1.2 授權(quán)法 例如,你想用戶 myuser 使用密碼 mypassword 通過 IP 地址連接到 MySQL 服務(wù)器,使用: 如果你

    2024年02月06日
    瀏覽(22)
  • 【Azure API 管理】APIM如何實現(xiàn)對部分固定IP進行訪問次數(shù)限制呢?如60秒10次請求

    【Azure API 管理】APIM如何實現(xiàn)對部分固定IP進行訪問次數(shù)限制呢?如60秒10次請求

    使用Azure API Management, 想對一些固定的IP地址進行訪問次數(shù)的限制,如被限制的IP地址一分鐘可以訪問10次,而不被限制的IP地址則可以無限訪問? ? 最近ChatGPT爆火,所以也把這個問題讓ChatGPT來解答,然后人工驗證它的回答正確與否? 根據(jù)對APIM Policy的文檔參考, choose 和 rat

    2023年04月24日
    瀏覽(21)
  • 如何通過 IP + 端口遠程訪問服務(wù)器上的jupyter notebook

    如何通過 IP + 端口遠程訪問服務(wù)器上的jupyter notebook

    ? ? ? ? 前情需要:最近實驗室和老師要求能時刻訪問服務(wù)器上的jupyter notebook通過IP + 端口來連接,但是我弄了很久就是不能連接,始終是無法連接網(wǎng)站 但是通過ssh -L? 本地端口:localhost:服務(wù)器jupyter的端口 root@服務(wù)器IP 這樣連接是可以在本地運行的,但是沒有完成老師的要求

    2024年02月11日
    瀏覽(26)
  • 如何通過本地搭建wamp服務(wù)器并實現(xiàn)無公網(wǎng)IP遠程訪問

    如何通過本地搭建wamp服務(wù)器并實現(xiàn)無公網(wǎng)IP遠程訪問

    軟件技術(shù)的發(fā)展日新月異,各種能方便我們生活、工作和娛樂的新軟件層出不窮,但也有一些經(jīng)過時間和用戶考驗的老牌軟件屹立不倒。就一我們熟悉的網(wǎng)站集成環(huán)境來說,全球就有很多種server軟件。每個軟件都有各自的特色(雖然同質(zhì)化很嚴重),也擁有自己固定的適用群

    2024年02月12日
    瀏覽(139)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包