安装OpenResty(Linux-Docker)

文章目录

直接在linux中安装请参考博客 https://blog.csdn.net/shall_zhao/article/details/142070389
这篇博客讲解如何在docker中安装

创建挂载目录的配置文件和日志

这个主要是为了我们后面对配置文件进行一定的修改方便,最好配置一下:

sh 复制代码
mkdir -p /home/docker/openresty/nginx
mkdir -p /home/docker/openresty/nginx/lua
mkdir -p /home/docker/openresty/nginx/logs

拉镜像

sh 复制代码
docker pull openresty/openresty

先运行一下OpenResty容器,把他的配置文件先拷贝出来,用于宿主机挂载

sh 复制代码
# 运行openresty容器
docker run -it --name openresty -p 7000:80 openresty/openresty

这时候我们使用的是-it参数,所以这个指令运行以后不会关闭,你也可以修改-it-d,让他在后台运行,如果使用的是-it参数的话,需要再开一个远程窗口进行文件拷贝的操作。

sh 复制代码
# 拷贝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

拷贝完成后就可以停下这个容器,并且删除

sh 复制代码
docker stop openresty
docker rm openresty

然后我们正式启动这个容器,指定挂载路径

sh 复制代码
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

访问一下欢迎页面: http://192.168.10.88:7000/

我这是自己的路由,根据你的虚拟机地址修改自己的域名

打开html目录下的index.html添加一个内容

sh 复制代码
vi /home/docker/openresty/nginx/html/index.html

重启openresty查看效果

java 复制代码
docker exec openresty /usr/local/openresty/nginx/sbin/nginx -s reload

通过lua脚本实现通过请求头动态路由

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

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

修改index2.html的内容

sh 复制代码
vi index2.html

编写Lua脚本

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

lua 复制代码
-- router.lua
local upstreams = {
    V1 = "https://192.168.10.88:7000/index.html",
    V2 = "https://192.168.10.88: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]

配置nginx.conf

sh 复制代码
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 模块,而不仅仅是加载指定的用户自定义目录。当你需要指定自定义的搜索路径时,只需在 ;; 前添加你自己的路径即可

sh 复制代码
lua_package_path "/usr/local/openresty/nginx/lua/?.lua;;";

配置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 目录添加自己的配置就能加载到。

sh 复制代码
vi /home/docker/openresty/nginx/conf.d/my.conf
sh 复制代码
    server {
        listen 8000;

        set $target_server '';

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

重新加载配置

sh 复制代码
docker exec openresty /usr/local/openresty/nginx/sbin/nginx -s reload

测试动态路由功能

调用V1版本

sh 复制代码
curl -H "Api-Version: V1" http://127.0.0.1:8000

调用V2版本

sh 复制代码
curl -H "Api-Version: V2" http://127.0.0.1:8000
相关推荐
likfishdn11 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean30 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=32 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
Dontla1 小时前
Docker run --add-host参数解析(在容器启动时向/etc/hosts文件中添加自定义的主机名与IP映射)(适用于临时调试或测试)
tcp/ip·docker·容器
西西小飞龙1 小时前
Docker和K8S中pod、services、container的介绍和关系
docker·容器·kubernetes
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
kngines1 小时前
【实战 ES】实战 Elasticsearch:快速上手与深度实践-1.3.1单节点安装(Docker与手动部署)
大数据·elasticsearch·docker
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos