如何在nginx容器中将环境变量与lua一起使用

时间:2020-09-24 17:11:19

标签: docker nginx lua

我想在容器上使用Nginx,并且Nginx将读取容器的环境变量。我经过搜索发现,使用lua模块可以实现这一点,但是由于某些原因,我无法在nginx本身上加载lua模块。请帮忙,添加Dockerfile和nginx.conf
Dockerfile

FROM nginx:1.15-alpine

RUN  mkdir -p /run/nginx && \
     apk add nginx-mod-http-lua

WORKDIR /usr/src/app

COPY build /usr/src/app/build
COPY mime.types /usr/src/app

COPY nginx.conf /usr/src/app

EXPOSE 8080

CMD [ "nginx", "-c", "/usr/src/app/nginx.conf", "-g", "daemon off;" ]

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 {

}

http {
    server {
        listen 8080;
        set_by_lua $db_api 'return os.getenv("DB_API")';
        location /db/ {
            proxy_pass $db_api;
        }

        location / {
            root /usr/src/app/build;
            index index.html;
        }
    }
}

这些是我得到的错误:

2020/09/24 17:06:49 [alert] 1#1: detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
2020/09/24 17:06:49 [error] 1#1: lua_load_resty_core failed to load the resty.core module from https://github.com/openresty/lua-resty-core; ensure you are using an OpenResty release from https://openresty.org/en/download.html (rc: 2, reason: module 'resty.core' not found:
        no field package.preload['resty.core']
        no file './resty/core.lua'
        no file '/usr/share/luajit-2.1.0-beta3/resty/core.lua'
        no file '/usr/local/share/lua/5.1/resty/core.lua'
        no file '/usr/local/share/lua/5.1/resty/core/init.lua'
        no file '/usr/share/lua/5.1/resty/core.lua'
        no file '/usr/share/lua/5.1/resty/core/init.lua'
        no file '/usr/share/lua/common/resty/core.lua'
        no file '/usr/share/lua/common/resty/core/init.lua'
        no file './resty/core.so'
        no file '/usr/local/lib/lua/5.1/resty/core.so'
        no file '/usr/lib/lua/5.1/resty/core.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file './resty.so'
        no file '/usr/local/lib/lua/5.1/resty.so'
        no file '/usr/lib/lua/5.1/resty.so'
        no file '/usr/local/lib/lua/5.1/loadall.so')
nginx: [error] lua_load_resty_core failed to load the resty.core module from https://github.com/openresty/lua-resty-core; ensure you are using an OpenResty release from https://openresty.org/en/download.html (rc: 2, reason: module 'resty.core' not found:
        no field package.preload['resty.core']
        no file './resty/core.lua'
        no file '/usr/share/luajit-2.1.0-beta3/resty/core.lua'
        no file '/usr/local/share/lua/5.1/resty/core.lua'
        no file '/usr/local/share/lua/5.1/resty/core/init.lua'
        no file '/usr/share/lua/5.1/resty/core.lua'
        no file '/usr/share/lua/5.1/resty/core/init.lua'
        no file '/usr/share/lua/common/resty/core.lua'
        no file '/usr/share/lua/common/resty/core/init.lua'
        no file './resty/core.so'
        no file '/usr/local/lib/lua/5.1/resty/core.so'
        no file '/usr/lib/lua/5.1/resty/core.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file './resty.so'
        no file '/usr/local/lib/lua/5.1/resty.so'
        no file '/usr/lib/lua/5.1/resty.so'
        no file '/usr/local/lib/lua/5.1/loadall.so')

这是docker构建和运行命令:

docker build -t client:1.0.0 --no-cache .

docker run -p 80:8080 -it -e DB_API=DB_API_URL client:1.0.0

1 个答案:

答案 0 :(得分:0)

在顶部添加env指令,并与它所起作用的问题的注释一起使用

相关问题