安装OpenResty(Linux-Docker)


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

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

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

mkdir -p /home/docker/openresty/nginx
mkdir -p /home/docker/openresty/nginx/lua
mkdir -p /home/docker/openresty/nginx/logs

拉镜像

docker pull openresty/openresty

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

# 运行openresty容器
docker run -it --name openresty -p 7000:80 openresty/openresty

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

# 拷贝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

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

docker stop openresty
docker rm 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

在这里插入图片描述
访问一下欢迎页面: http://192.168.10.88:7000/
我这是自己的路由,根据你的虚拟机地址修改自己的域名

在这里插入图片描述
打开html目录下的index.html添加一个内容

vi /home/docker/openresty/nginx/html/index.html

在这里插入图片描述
重启openresty查看效果

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

在这里插入图片描述

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

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

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

修改index2.html的内容

vi index2.html

编写Lua脚本
在/home/docker/openresty/nginx/lua目录创建一个router.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

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;;";

配置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;
        }
    }

重新加载配置

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

测试动态路由功能
调用V1版本

curl -H "Api-Version: V1" http://127.0.0.1:8000

调用V2版本

curl -H "Api-Version: V2" http://127.0.0.1:8000
### 使用 Docker Compose 构建并安装 OpenResty 要通过 `docker-compose` 文件从源码构建并安装 OpenResty,可以按照以下方法实现。以下是详细的说明: #### 创建 Dockerfile 首先需要创建一个 `Dockerfile` 来定义如何从源码构建 OpenResty。此文件应包含下载、解压和编译 OpenResty 所需的指令。 ```dockerfile # 基础镜像选择 Ubuntu 或者其他适合的 Linux 发行版 FROM ubuntu:20.04 AS build-env # 设置环境变量以避免交互式配置提示 ENV DEBIAN_FRONTEND=noninteractive # 更新包管理器并安装必要的工具和依赖项 RUN apt-get update && \ apt-get install -y --no-install-recommends \ curl \ make \ gcc \ libc-dev \ libpcre3-dev \ zlib1g-dev \ openssl \ libssl-dev # 下载 OpenResty 源码并解压 WORKDIR /usr/src/openresty ARG OPENRESTY_VERSION=1.21.4.1 RUN curl -fSL https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz -o openresty.tar.gz && \ tar -xzvf openresty.tar.gz && \ rm openresty.tar.gz && \ mv openresty-* openresty-source # 编译 OpenResty WORKDIR /usr/src/openresty/openresty-source RUN ./configure --prefix=/usr/local/openresty && \ make -j$(nproc) && \ make install # 清理临时文件 RUN apt-get clean && \ rm -rf /var/lib/apt/lists/* # 运行时镜像 FROM ubuntu:20.04 # 复制已编译好的 OpenResty 到运行时镜像中 COPY --from=build-env /usr/local/openresty /usr/local/openresty # 配置工作目录 WORKDIR /app # 将默认用户设置为 nobody (如果需要的话) USER nobody # 启动命令 CMD ["bash"] ``` 上述 `Dockerfile` 中包含了从源码构建 OpenResty 的所有必要步骤,并将其复制到最终的轻量级运行时镜像中[^4]。 --- #### 创建 docker-compose.yml 文件 接下来,创建一个 `docker-compose.yml` 文件来管理和启动容器。 ```yaml version: '3' services: openresty: container_name: openresty-container build: context: . args: OPENRESTY_VERSION: 1.21.4.1 volumes: - ./openresty-test:/app:Z # 映射主机上的工作目录至容器内的 /app 路径 - ./openresty-test/logs:/usr/local/openresty/nginx/logs:Z # 日志路径映射 - ./openresty-test/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro,Z # 自定义 nginx 配置文件映射 ports: - "8080:80" networks: - kong-net networks: kong-net: external: true ``` 在此 `docker-compose.yml` 文件中: - 定义了一个名为 `openresty` 的服务。 - 使用本地上下文中的 `Dockerfile` 构建镜像。 - 提供了参数化版本号支持以便灵活调整 OpenResty 版本。 - 映射了主机的工作目录 `/openresty-test` 至容器内的 `/app` 路径,便于开发调试。 - 映射了日志和配置文件路径以满足实际需求[^2]。 - 绑定了端口 `8080` 到容器内部的 `80` 端口。 - 加入外部网络 `kong-net` 以与其他容器通信[^1]。 --- #### 初始化项目结构 在宿主机上初始化所需的目录结构如下所示: ```bash $ mkdir -p openresty-test/{logs,conf} $ echo "worker_processes auto;" > openresty-test/conf/nginx.conf ``` 这一步骤确保了容器能够正常加载配置文件和写入日志数据。 --- #### 构建与运行 执行以下命令完成构建和启动过程: ```bash docker-compose up --build ``` 该命令会自动拉取基础镜像、构建自定义镜像并启动容器[^3]。 --- ### 注意事项 - 如果目标平台不是标准 Linux,则可能需要额外适配特定于 NAS 设备或其他硬件架构的交叉编译选项。 - 对于生产环境建议优化镜像大小并通过多阶段构建移除不必要的中间产物。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值