简介
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。
docker 下部署安装OpenResty以及基本使用
首先拉取OpenResty的官方镜像
docker pull openresty/openresty
创建配置文件存放目录,例如:
mkdir -p /mysoft/openresty/conf.d mkdir -p /mysoft/openresty/lua
运行容器
docker run -d -p 9098:80 -v /mysoft/openresty/conf.d:/etc/nginx/conf.d -v /mysoft/openresty/lua:/etc/nginx/lua --name openresty openresty/openresty
编写hello.conf 放到 /mysoft/openresty/conf.d 目录下
server {
listen 80;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>Hello, World!</p>")
';
}
}
也可使用 lua 文件加载配置,如下,在 /mysoft/openresty/lua 下创建 hello.lua 脚本
ngx.say("hello, world")
hello.conf 改为如下:
server {
listen 80;
default_type text/html;
location / {
content_by_lua_file /etc/nginx/lua/hello.lua;
}
}
OpenResty配置动态的反向代理
作者:玩玩风行啦
链接:https://www.jianshu.com/p/1d56d5e1fc43
来源:OpenResty动态更新后端地址
实现思路:
在OpenResty中定义共享Dict,将后端IP保存在其中
设置/add和/delete的location,通过Web调用增删后端IP
设置proxy_pass代理,代理的地址从共享Dict中获取实现请求
hello.conf 改为如下配置:
lua_shared_dict ups 10m;
server {
listen 80;
default_type text/html;
location /add {
content_by_lua_block {
local ups = ngx.shared.ups
local ip = ngx.req.get_uri_args()["ip"]
local port = ngx.req.get_uri_args()["port"]
local no = ngx.req.get_uri_args()["no"]
if ip == nil or port == nil or no == nil then
ngx.say("usage: /add?ip=x.x.x.x&port=xx&no=xx")
return
end
ups:set("host-"..no, ip)
ups:set("port-"..no, port)
-- ups["host-"..no] = ip
-- ups["port-"..no] = port
local count = 0
for k in pairs(ups:get_keys(0)) do count = count + 1 end
ups:set("servers", count)
ngx.say("OK")
}
}
location /get {
content_by_lua_block {
local ups = ngx.shared.ups
local no = ngx.req.get_uri_args()["no"]
if no == nil then
ngx.say("usage: /get?no=xx")
return
end
if ups:get("host-" .. no) == nil then
ngx.say("the no server is vaild: " .. no)
return
end
local count = 0
for k in pairs(ups:get_keys(0)) do count = count + 1 end
ups:set("servers", count)
ngx.say("ip: " .. ups:get("host-" .. no))
ngx.say("port: " .. ups:get("port-" .. no))
ngx.say("servers: " .. math.floor(ups:get("servers")/2))
}
}
location /delete {
content_by_lua_block {
local ups = ngx.shared.ups
local no = ngx.req.get_uri_args()["no"]
if no == nil then
ngx.say("usage: /get?no=xx")
return
end
if ups:get("host-" .. no) == nil then
ngx.say("the no server is vaild: " .. no)
return
end
ups:delete("host-" .. no)
ups:delete("port-" .. no)
ngx.say("OK")
}
}
location / {
set_by_lua_block $cur_ups {
local ups = ngx.shared.ups
local items = ups:get("servers")
local n = math.random(math.floor(items/2))
local ss = string.gsub(ups:get_keys(0)[n], "%a+-", "")
s = ups:get("host-"..ss) .. ":" .. ups:get("port-"..ss)
return s
}
proxy_next_upstream off;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass $scheme://$cur_ups;
}
}
配置好后,重启 docker 容器。我们便可以调用接口来实现动态配置nginx 反向代理的ip
127.0.0.1:9098/add?no=1&ip=127.0.0.1&port=8081 127.0.0.1:9098/delete?no=1
在我们系统应用就是,客户的公网ip 重启后变化,又没法使用 DDNS,我们这边的代理就不生效了,这样我们监测到客户ip变化后,程序自动获取新的公网ip,调用上面的远程配置动态代理的接口就实现自动代理的功能了。