如何在 Alpine linux 上为 nginx 添加 Lua 模块?

Posted

技术标签:

【中文标题】如何在 Alpine linux 上为 nginx 添加 Lua 模块?【英文标题】:How do I add the Lua module for nginx on Alpine linux? 【发布时间】:2018-05-02 03:34:59 【问题描述】:

我想要一个启用 Lua 模块的 nginx 精简 Docker 映像。如何基于 Alpine linux 创建它?

【问题讨论】:

【参考方案1】:

这是Dockerfile

FROM alpine:3.6

RUN apk add --no-cache nginx-mod-http-lua

# Delete default config
RUN rm -r /etc/nginx/conf.d && rm /etc/nginx/nginx.conf

# Create folder for PID file
RUN mkdir -p /run/nginx

# Add our nginx conf
COPY ./nginx.conf /etc/nginx/nginx.conf

CMD ["nginx"]

安装nginx-mod-http-lua 包还会安装nginxluajit 等。

nginx.conf 至少应包含以下内容:

load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;

pcre_jit on;

events 
  worker_connections 1024;


daemon off;

【讨论】:

如果您不从默认nginx.conf 中删除include /etc/nginx/modules/*.conf;,则不需要这些load_module @Marian:在这种情况下我们如何安装 luarocks?我确实找到了 luajit 的包含路径。 load_module" 指令在 /etc/nginx/conf.d 中是不允许的 为什么是 --no-cache 标志? @jurl 这在***.com/a/49119046/1228491中有很好的解释【参考方案2】:

Dockerfile:

FROM nginx:1.15-alpine
RUN  mkdir -p /run/nginx
RUN  apk add --no-cache nginx-mod-http-lua
COPY nginx_conf/ /etc/nginx/ # Your nginx conf
COPY lua/ /etc/lua/          # Your lua files 

nginx conf的第一行:

load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
pcre_jit on;

【讨论】:

nginx: [emerg] 模块 "/usr/lib/nginx/modules/ndk_http_module.so" 版本 1016001 而不是 /etc/nginx/nginx.conf:1 中的 1018000 奇怪...去年没有发生。尝试使用 FROM nginx:1.15-alpine 我检查并正在工作。如果您进行研究以使其适用于最新版本,请告诉我结果。无论如何,我编辑了原始答案。感谢您的反馈。 Nginx版本与模块版本不匹配,看来apk add无法指定版本号。 当前 alpine 版本是 1.21 - 除了返回几个 nginx 版本之外还有其他解决方案吗? 可以使用标签stable-alpine(nginx版本1.20.2)。但我还有另一个错误module "/usr/lib/nginx/modules/ndk_http_module.so" is not binary compatible 。你有什么解决办法吗?【参考方案3】:

我们使用 Openresty,一个集成了 nginx 和 Lua 的平台。

在默认的 nginx 文件中,你可以这样调用 Lua:

server 
    listen 80;
    listen 443 ssl; # 'ssl' parameter tells NGINX to decrypt the traffic

    # 1
    location ~ /api/(.*) 
        resolver xxx.x.x.xx;

    rewrite_by_lua_block 
        ngx.req.set_header("x-header", "12345678901234567")

    

这里的阿尔卑斯山图片:https://github.com/openresty/docker-openresty/tree/master/

还有一个 alpine-fat 具有 makegit 和其他库,可以帮助您在 Docker 映像中构建。

【讨论】:

【参考方案4】:

参见:《nginx 官方镜像添加第三方模块》 在:https://github.com/nginxinc/docker-nginx/tree/master/modules

“可以使用 build_module.sh 帮助程序脚本按照简单的文件系统布局/语法从您自己的指令中使用第三方模块扩展主线映像,或者从 pkg-oss 回退到包源。”

$ docker build --build-arg ENABLED_MODULES="ndk lua" -t my-nginx-with-lua .

【讨论】:

【参考方案5】:

您在 Docker Hub 上查看

https://hub.docker.com/

你会发现一个基于 Alpine Linux 的 Nginx 镜像,支持 Lua

一些例子

https://hub.docker.com/r/ilagnev/alpine-nginx-lua/

https://hub.docker.com/r/firesh/nginx-lua/

查看 Dockerfile 了解更多详情

【讨论】:

我找到了这两个,但发现两者都不是最新的(最后一次推送是一年前)。此外,他们缺乏有关如何启用该模块的信息。

以上是关于如何在 Alpine linux 上为 nginx 添加 Lua 模块?的主要内容,如果未能解决你的问题,请参考以下文章