使用Docker安装OpenResty并使用Lua脚本实现通过请求头动态路由

前言

OpenResty(又称:ngx_openresty) 是一个基于 nginx的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。
OpenResty 简单理解成 就相当于封装了nginx,并且集成了LUA脚本,开发人员只需要简单的其提供了模块就可以实现相关的逻辑,而不再像之前,还需要在nginx中自己编写lua的脚本,再进行调用了。

一、Docker安装OpenResty

1.1 前置准备

  • 1、创建挂载目录用于挂载配置文件和日志
mkdir -p /home/docker/openresty/nginx
mkdir -p /home/docker/openresty/nginx/lua
mkdir -p /home/docker/openresty/nginx/logs
  • 2、拉取镜像
docker pull openresty/openresty
  • 3、运行OpenResty容器先拷贝一些配置文件出来,用于宿主机挂载
# 运行openresty容器
docker run -it --name openresty -p 7000:80 openresty/openresty

# 拷贝conf文件夹和html文件夹内容到宿主机
docker cp openresty:/usr/local/openresty/nginx/conf /home/docker/openresty/nginx/conf
docker cp openresty:/usr/local/openresty/nginx/html /home/docker/openresty/nginx/html
docker cp openresty:/etc/nginx/conf.d /home/docker/openresty/nginx/conf.d

在这里插入图片描述

  • 4、停止并且删除openresty容器
docker stop openresty
docker rm openresty

1.2 正式部署OpenResty

1.2.1 启动OpenResty容器

docker run -itd \
--privileged=true \
--restart=always \
--name openresty \
-p 7000:80 \
-p 8000:8000 \
-v /home/docker/openresty/nginx/conf:/usr/local/openresty/nginx/conf/:rw \
-v /home/docker/openresty/nginx/conf.d:/etc/nginx/conf.d/:rw \
-v /home/docker/openresty/nginx/html:/usr/local/openresty/nginx/html/:rw \
-v /home/docker/openresty/nginx/logs:/usr/local/openresty/nginx/logs/:rw \
-v /home/docker/openresty/nginx/lua:/usr/local/openresty/nginx/lua/:rw \
openresty/openresty

在这里插入图片描述

1.2.2 访问欢迎页面

http://127.0.0.1:7000

在这里插入图片描述

1.2.3 修改欢迎页面内容查看效果

  • 打开html目录下的index.html添加一个内容
vi /home/docker/openresty/nginx/html/index.html

在这里插入图片描述

  • 重启openresty查看效果
docker exec openresty /usr/local/openresty/nginx/sbin/nginx -s reload

在这里插入图片描述
在这里插入图片描述

二、通过Lua脚本实现通过请求头动态路由

2.1 新增一个index2.html页面用于演示效果

# 进入宿主机页面文件挂载地址
cd /home/docker/openresty/nginx/html
# 复制一份index.html文件为index2.html
cp index.html index2.html

修改index2.html的内容

vi index2.html

在这里插入图片描述

2.2 编写Lua脚本

在/home/docker/openresty/nginx/lua目录创建一个router.lua文件,将下面内容写入到文件中

vi /home/docker/openresty/nginx/lua/router.lua
-- router.lua
local upstreams = {
    
    
    V1 = "https://127.0.0.1:7000/index.html",
    V2 = "https://127.0.0.1:7000/index2.html",
}

local target_server = "V1" 

local specific_header = ngx.req.get_headers()["Api-Version"]

if specific_header then
    target_server = specific_header
end

ngx.var.target_server = upstreams[target_server]

2.3 配置nginx.conf

vi /home/docker/openresty/nginx/conf/nginx.conf

在http块的default_type application/octet-stream;下添加lua_package_path,lua_package_path是我们存放lua脚本的地址?.lua代表.lua结尾的文件,;; 是为了确保 OpenResty 可以加载系统默认的 Lua 模块,而不仅仅是加载指定的用户自定义目录。当你需要指定自定义的搜索路径时,只需在 ;; 前添加你自己的路径即可

lua_package_path "/usr/local/openresty/nginx/lua/?.lua;;";

在这里插入图片描述

2.4 配置my.conf

/home/docker/openresty/nginx/conf.d目录创建一个my.conf文件,将下面内容写入到文件中,在nginx.conf默认配置了include /etc/nginx/conf.d/*.conf;,我们将宿主机的/home/docker/openresty/nginx/conf.d 目录和容器内目录进行了挂载,只要在宿主机/home/docker/openresty/nginx/conf.d 目录添加自己的配置就能加载到。

vi /home/docker/openresty/nginx/conf.d/my.conf
    server {
    
    
        listen 8000;

        set $target_server '';

        location / {
    
    
            access_by_lua_file /usr/local/openresty/nginx/lua/router.lua;
            proxy_pass $target_server;
        }
    }

2.5 重新加载配置

docker exec openresty /usr/local/openresty/nginx/sbin/nginx -s reload

2.6 测试动态路由功能

  • 调用V1版本
curl -H "Api-Version: V1" http://127.0.0.1:8000

在这里插入图片描述

  • 调用V2版本
curl -H "Api-Version: V2" http://127.0.0.1:8000

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44606481/article/details/131818345