alexazhou / VeryNginx

A very powerful and friendly nginx base on lua-nginx-module( openresty ) which provide WAF, Control Panel, and Dashboards.
GNU Lesser General Public License v3.0
5.96k stars 956 forks source link

failed to run balancer_by_lua #108

Open jim3ma opened 7 years ago

jim3ma commented 7 years ago

同样的问题 #74 failed to run balancer_by_lua*: /opt/verynginx/verynginx/lua_script/on_banlance.lua:9: module 'ngx.balancer' not found ngx.balancer 这个在lua-nginx-module里面是有的,在这里 我这里提供一下完整的Dockerfile和另外两个配置文件,可以保证重现问题。 Dockerfile

FROM alpine:3.4

MAINTAINER NGINX Docker Maintainers "docker-maint@nginx.com"

ENV NGINX_VERSION=1.11.5 NGINX_DAV_VERSION=0.0.3 VERYNGINX_VERSION=0.3.3 LUAJIT_VERSION=2.1.0-beta2 NGX_DEVEL_KIT_VERSION=0.3.0 NGX_LUA_VERSION=0.10.7
ENV LUAJIT_LIB=/usr/local/lib LUAJIT_INC=/usr/local/include/luajit-2.1/

RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \
    && CONFIG="\
        --prefix=/etc/nginx \
        --sbin-path=/usr/sbin/nginx \
        --modules-path=/usr/lib/nginx/modules \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/run/nginx.lock \
        --http-client-body-temp-path=/var/cache/nginx/client_temp \
        --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
        --user=nginx \
        --group=nginx \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_addition_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_random_index_module \
        --with-http_secure_link_module \
        --with-http_stub_status_module \
        --with-http_auth_request_module \
        --with-http_xslt_module=dynamic \
        --with-http_image_filter_module=dynamic \
        --with-http_geoip_module=dynamic \
        --with-http_perl_module=dynamic \
        --with-threads \
        --with-stream \
        --with-stream_ssl_module \
        --with-stream_ssl_preread_module \
        --with-stream_realip_module \
        --with-stream_geoip_module=dynamic \
        --with-http_slice_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-compat \
        --with-file-aio \
        --with-http_v2_module \
        --add-module=/usr/src/nginx-dav-ext-module \
        --add-module=/usr/src/ngx_devel_kit-${NGX_DEVEL_KIT_VERSION} \
        --add-module=/usr/src/lua-nginx-module-${NGX_LUA_VERSION} \
    " \
    && addgroup -S nginx \
    && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
    && apk add --no-cache --virtual .build-deps \
        git \
        gcc \
        libc-dev \
        make \
        openssl-dev \
        pcre-dev \
        zlib-dev \
        linux-headers \
        curl \
        gnupg \
        libxslt-dev \
        gd-dev \
        geoip-dev \
        perl-dev \
        expat-dev \
        python \
        ca-certificates \
        wget \
        openssl \
        musl-dev \
    && curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \
    #&& curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz.asc -o nginx.tar.gz.asc \
    && curl -fSL https://github.com/alexazhou/VeryNginx/archive/v${VERYNGINX_VERSION}.zip -o VeryNginx.zip \
    && curl -fSL http://luajit.org/download/LuaJIT-${LUAJIT_VERSION}.zip -o LuaJIT.zip \
    && curl -fSL https://github.com/simpl/ngx_devel_kit/archive/v${NGX_DEVEL_KIT_VERSION}.zip -o ngx_devel_kit.zip \
    && curl -fSL https://github.com/openresty/lua-nginx-module/archive/v${NGX_LUA_VERSION}.zip -o ngx_lua.zip \
    #&& export GNUPGHOME="$(mktemp -d)" \
    #&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEYS" \
    #&& gpg --batch --verify nginx.tar.gz.asc nginx.tar.gz \
    #&& rm -r "$GNUPGHOME" nginx.tar.gz.asc \
    && mkdir -p /usr/src \
        && (cd /usr/src; git clone -b v${NGINX_DAV_VERSION} https://github.com/arut/nginx-dav-ext-module.git) \
    && tar -zxC /usr/src -f nginx.tar.gz \
    && rm nginx.tar.gz \
    #############################
    && unzip VeryNginx.zip -d /usr/src \
    && (cd /usr/src/VeryNginx-${VERYNGINX_VERSION}/; python install.py install verynginx) \
    && unzip LuaJIT.zip -d /usr/src \
    && unzip ngx_devel_kit.zip -d /usr/src \
    && unzip ngx_lua.zip -d /usr/src \
    && rm VeryNginx.zip \
    && rm LuaJIT.zip \
    && rm ngx_devel_kit.zip \
    && rm ngx_lua.zip \
    && cd /usr/src/LuaJIT-${LUAJIT_VERSION} \
    && make \
    && make install \
    && ln -s /usr/local/lib/libluajit-5.1.so /usr/local/lib/libluajit-5.1.so.2 \
    #############################
    && cd /usr/src/nginx-$NGINX_VERSION \
    && ./configure $CONFIG --with-debug \
    && make -j$(getconf _NPROCESSORS_ONLN) \
    && mv objs/nginx objs/nginx-debug \
    && mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \
    && mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \
    && mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \
    && mv objs/ngx_http_perl_module.so objs/ngx_http_perl_module-debug.so \
    && mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \
    && ./configure $CONFIG \
    && make -j$(getconf _NPROCESSORS_ONLN) \
    && make install \
    && rm -rf /etc/nginx/html/ \
    && mkdir /etc/nginx/conf.d/ \
    && mkdir -p /usr/share/nginx/html/ \
    && install -m644 html/index.html /usr/share/nginx/html/ \
    && install -m644 html/50x.html /usr/share/nginx/html/ \
    && install -m755 objs/nginx-debug /usr/sbin/nginx-debug \
    && install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \
    && install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \
    && install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \
    && install -m755 objs/ngx_http_perl_module-debug.so /usr/lib/nginx/modules/ngx_http_perl_module-debug.so \
    && install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \
    && ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \
    && strip /usr/sbin/nginx* \
    && strip /usr/lib/nginx/modules/*.so \
    && rm -rf /usr/src/nginx-$NGINX_VERSION \
    && rm -rf /usr/src/nginx-dav-ext-module \
    && rm -rf /usr/src/LuaJIT-${LUAJIT_VERSION} \
    && rm -rf /usr/src/ngx_devel_kit-${NGX_DEVEL_KIT_VERSION} \
    && rm -rf /usr/src/lua-nginx-module-${NGX_LUA_VERSION} \
    && rm -rf /usr/src/VeryNginx-${VERYNGINX_VERSION} \
    \
    # Bring in gettext so we can get `envsubst`, then throw
    # the rest away. To do this, we need to install `gettext`
    # then move `envsubst` out of the way so `gettext` can
    # be deleted completely, then move `envsubst` back.
    && apk add --no-cache --virtual .gettext gettext \
    && mv /usr/bin/envsubst /tmp/ \
    \
    && runDeps="libgcc pcre $( \
        scanelf --needed --nobanner /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
            | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
            | sort -u \
            | xargs -r apk info --installed \
            | sort -u \
    )" \
    && apk add --no-cache --virtual .nginx-rundeps $runDeps \
    && apk del .build-deps \
    && apk del .gettext \
    && mv /tmp/envsubst /usr/local/bin/ \
    \
    # forward request and error logs to docker log collector
    && ln -sf /dev/stdout /var/log/nginx/access.log

COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

nginx.conf


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

include /opt/verynginx/verynginx/nginx_conf/in_external.conf;
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /opt/verynginx/verynginx/nginx_conf/in_http_block.conf;
    include /etc/nginx/conf.d/*.conf;
}

nginx.vh.default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    include /opt/verynginx/verynginx/nginx_conf/in_server_block.conf;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

运行如下命令:

docker build -t vn .
docker run -d vn

然后在浏览器里操作,添加proxy_pass,即可重现问题了,nginx的log在/var/log/nginx/目录里。

alexazhou commented 7 years ago

多谢反馈。

我看到你是自己编译了 nginx 以及相关的模块,设置了 NGX_LUA_VERSION=0.10.7 ,这个版本的 lua-nginx-module 模块应该是可以支持 balance 的,就是我不确定里面是否包含了 balance 的代码,balance 的代码 在这里:

https://github.com/openresty/lua-resty-core

安装这个库(或者吧 balance 的代码补上)应该能解决问题

我有空了会查一下这个问题,就是会比较慢T_T,你也可以自己动手哈

jim3ma commented 7 years ago

@alexazhou 的确是lua-resty-core没有安装的问题,将lua-resty-core/lib/下的所有文件复制到/usr/local/lib/lua/5.1/目录下就可以了,当然可以是任意lua能识别的目录就可以了,如下的目录都是可以的

/opt/verynginx/verynginx/lua_script/
/opt/verynginx/verynginx/lua_script/module/
/usr/local/share/luajit-2.1.0-beta2/
/usr/local/share/lua/5.1/
...

看来wiki和readme需要完善一下了哦。

alexazhou commented 7 years ago

你说的对,是该完善啦。

发自我的 iPhone

在 2016年12月20日,下午3:22,Jim Ma notifications@github.com 写道:

@alexazhou 的确是lua-resty-core没有安装的问题,将lua-resty-core/lib/下的所有文件复制到/usr/local/lib/lua/5.1/目录下就可以了,当然可以是任意lua能识别的目录就可以了,如下的目录都是可以的

/opt/verynginx/verynginx/lua_script/ /opt/verynginx/verynginx/lua_script/module/ ... 看来wiki和readme需要完善一下了哦。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

0akarma commented 4 years ago

@alexazhou 的确是lua-resty-core没有安装的问题,将lua-resty-core/lib/下的所有文件复制到/usr/local/lib/lua/5.1/目录下就可以了,当然可以是任意lua能识别的目录就可以了,如下的目录都是可以的

/opt/verynginx/verynginx/lua_script/
/opt/verynginx/verynginx/lua_script/module/
/usr/local/share/luajit-2.1.0-beta2/
/usr/local/share/lua/5.1/
...

看来wiki和readme需要完善一下了哦。

我这边尝试把lua-resty-core/lib/下的文件复制到/opt/verynginx/verynginx/lua_script/下,会出现如下报错:

[error] 17#17: *282 failed to run balancer_by_lua*: /opt/verynginx/lua_script/on_banlance.lua:9: loop or previous error loading module 'ngx.balancer'