openresty获取国家代码

1. 如果你没有安装OpenResty,则按照下面的步骤进行安装

  1. 拉取镜像
    docker pull openresty/openresty
  2. 拷贝容器中文件到宿主机目录
docker cp openresty:/usr/local/openresty/nginx/conf/nginx.conf /www/openresty/conf
docker cp openresty:/usr/local/openresty/lualib  /www/openresty/
docker cp openresty:/usr/local/openresty/cert  /www/openresty/
  1. 安装完成之后做目录映射并启动
docker run  --name openresty -v /E/www/openresty/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf -v /E/www/openresty/conf/vhost/default.conf:/usr/local/openresty/nginx/conf/vhost/default.conf  -v /E/www/openresty/logs/:/usr/local/openresty/nginx/logs -v /E/www/openresty/cert/:/usr/local/openresty/nginx/cert -v /E/www/openresty/lua/:/usr/local/openresty/nginx/lua -v /E/www/openresty/lualib/:/usr/local/openresty/lualib -v /E/www/openresty/html/:/usr/local/openresty/nginx/html -v /E/www/openresty/module/:/usr/local/openresty/nginx/module  -p 80:80 -p 443:443 -d openresty/openresty
  1. 如果需要测试的ssl证书,需要下载
https://slproweb.com/products/Win32OpenSSL.html
生成命令可以参考https://www.jianshu.com/p/3790c2d29b6c
也可以
以下是在 Windows 上使用 OpenSSL 工具生成自签名 SSL 证书和私钥的步骤:
安装 OpenSSL 工具:
首先,您需要从 OpenSSL 官方网站下载适用于 Windows 的 OpenSSL 工具。然后,按照安装程序的说明进行安装。安装完成后,确保将 OpenSSL 的 bin 目录添加到系统的环境变量中,这样您就可以在命令提示符中访问 OpenSSL。
生成私钥(key)文件:
打开命令提示符(cmd)并导航到您希望保存证书和密钥的目录。然后执行以下命令生成私钥文件 server.key:
openssl genrsa -out server.key 2048
生成证书签发请求(CSR)文件:
接下来,执行以下命令生成证书签发请求文件 server.csr:
openssl req -new -key server.key -out server.csr
在执行此命令时,您需要回答一些证书请求相关的问题,如国家、省份、组织、通用名等。在测试环境中,您可以将通用名设置为 localhost 或您的 IP 地址。
使用 CSR 文件生成自签名证书(CRT 文件):
最后,执行以下命令使用 CSR 文件生成自签名证书 server.crt:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
这将生成一个有效期为 365 天的自签名证书文件 server.crt。
现在,您已经成功生成了自签名的 SSL 证书 (server.crt) 和私钥 (server.key)。您可以将它们用于测试或开发目的。在生产环境中,请务必使用由受信任的 CA 签名的证书。
修改nginx.conf的配置
ssl_certificate      cert/server.crt;
ssl_certificate_key  cert/server.key;
ssl_certificate_by_lua_file conf\cert\ssl.lua;
ssl_session_cache    shared:SSL:1m;
ssl_session_timeout  5m;
ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers  on;

2. 这个镜像里面会缺少一些安装工具,可以执行一下

安装包的安装

apt update
apt install rpm

在容器内使用 apt-get 来安装必要的工具和依赖,比如wget安装

apt-get update
apt-get install -y wget tar build-essential autoconf automake libtool

3. 下载并解压 libmaxminddb-1.4.3

cd /usr/local/openresty/nginx/module
wget https://github.com/maxmind/libmaxminddb/releases/download/1.4.3/libmaxminddb-1.4.3.tar.gz
tar -xzvf libmaxminddb-1.4.3.tar.gz
cd libmaxminddb-1.4.3

4. 配置并编译 libmaxminddb

./configure
make
make install
echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
ldconfig

5. 集成 nginx geoip2模块

cd /usr/local/openresty/nginx/module
wget https://github.com/leev/ngx_http_geoip2_module/archive/3.3.tar.gz
tar xf 3.3.tar.gz
mv ngx_http_geoip2_module-3.3 ngx_http_geoip2_module

6. 安装nginx-lua-maxminddb插件

如果你没有opm包管理工具,安装方式

apt-get install  openresty-opm

集成genip2模块

apt-get update
apt-get install epel-release
apt-get install libdigest-perl-md5-perl
opm get anjia0532/lua-resty-maxminddb

7. 这时候你就可以去识别国家代码了,现在你如果只有一个国家的mmdb库,你可以这么写

local geo = require 'resty.maxminddb'
if not geo.initted() then
   ngx.log(ngx.ERR, ' 下面的地址是绝对地址')
   geo.init("Country.mmdb")
end
local res, err = geo.lookup(ip)

你打印一下res就可以看到一份json数据了,包含国家和大洲信息

参考地址:
https://blog.csdn.net/qq_41506710/article/details/135522099
https://www.jianshu.com/p/a18006052c4f
https://www.jianshu.com/p/5aeb84198404

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值